Docker下Prometheus和Grafana三部曲之一:极速体验

本文涉及的产品
可观测监控 Prometheus 版,每月50GB免费额度
可观测可视化 Grafana 版,10个用户账号 1个月
EMR Serverless StarRocks,5000CU*H 48000GB*H
简介: Docker环境下,极速体验Prometheus和Grafana监控系统

欢迎访问我的GitHub

这里分类和汇总了欣宸的全部原创(含配套源码): https://github.com/zq2599/blog_demos
  • 开源监控工具Prometheus目前广为使用,配合Grafana即可直观展现监控数据,但对于初学者来说搭建这样一个系统要花费些时间,或者有时也想要快速搭好系统使用其功能,今天的实战中,我们在Docker环境下快速搭建和体验一个典型的业务监控系统,包括prometheus、node-exporter、cadvisor、grafana、业务后台服务(springboot);
  • 本文是《Docker下Prometheus和Grafana三部曲》系列的第一篇,整个系列包含以下内容:
  1. 极速体验,也就是本文;
  2. 细说Docker相关开发,介绍在Docker环境搭建整个环境的细节;
  3. 自定义监控项的开发和展示配置;

三部曲所有文章链接

  1. 《Docker下Prometheus和Grafana三部曲之一:极速体验》
  2. 《Docker下Prometheus和Grafana三部曲之二:细说Docker编排》
  3. 《Docker下Prometheus和Grafana三部曲之三:自定义监控项开发和配置》

环境信息

  • 本次实战的环境信息如下:
  1. 操作系统:CentOS 7.6.1810
  2. Docker:17.03.2-ce
  3. node-exporter:0.17.0 (docker容器,不需要您准备)
  4. prometheus:2.8.0 (docker容器,不需要您准备)
  5. cadvisor:0.28.0 (docker容器,不需要您准备)
  6. grafana:5.4.2 (docker容器,不需要您准备)
  7. springboot:1.5.19.RELEASE (docker容器,不需要您准备)

本次实战的电脑是CentOS系统,IP地址为192.168.1.101

操作步骤简介

  • 实战由以下几步组成:
  1. 一行命令创建所有容器;
  2. 取得Grafana的API KEY,创建图表时要用到;
  3. 一行命令创建所有图表;
  4. 体验监控系统;
  • 接下来开始实战吧。

创建容器

  • 登录CentOS服务器,新建一个文件夹,在里面执行以下命令就能将所有容器创建好:
wget https://raw.githubusercontent.com/zq2599/blog_demos/master/prometheusdemo/files/prometheus.yml && \
wget https://raw.githubusercontent.com/zq2599/blog_demos/master/prometheusdemo/files/docker-compose.yml && \
docker-compose up -d
  • 上述命令会下载容器编排脚本和prometheus的配置文件,然后创建容器,控制台输出如下信息(如果本地没有相关的镜像还会显示下载镜像的信息):
[root@hedy prometheus_blog]# wget https://raw.githubusercontent.com/zq2599/blog_demos/master/prometheusdemo/files/prometheus.yml && \
> wget https://raw.githubusercontent.com/zq2599/blog_demos/master/prometheusdemo/files/docker-compose.yml && \
> docker-compose up -d
--2019-03-09 23:05:28--  https://raw.githubusercontent.com/zq2599/blog_demos/master/prometheusdemo/files/prometheus.yml
正在解析主机 raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.228.133
正在连接 raw.githubusercontent.com (raw.githubusercontent.com)|151.101.228.133|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1173 (1.1K) [text/plain]
正在保存至: “prometheus.yml”

100%[==================================================================================================================================================================>] 1,173       --.-K/s 用时 0s      

2019-03-09 23:05:28 (162 MB/s) - 已保存 “prometheus.yml” [1173/1173])

--2019-03-09 23:05:28--  https://raw.githubusercontent.com/zq2599/blog_demos/master/prometheusdemo/files/docker-compose.yml
正在解析主机 raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.228.133
正在连接 raw.githubusercontent.com (raw.githubusercontent.com)|151.101.228.133|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1727 (1.7K) [text/plain]
正在保存至: “docker-compose.yml”

100%[==================================================================================================================================================================>] 1,727       --.-K/s 用时 0s      

2019-03-09 23:05:29 (108 MB/s) - 已保存 “docker-compose.yml” [1727/1727])

Creating network "prometheus_blog_default" with the default driver
Creating node-exporter  ... done
Creating prometheusdemo ... done
Creating cadvisor       ... done
Creating prometheus     ... done
Creating grafana        ... done

取得Grafana的API KEY

  • 现在要取得Grafana的授权KEY,有了这个KEY就能通过脚本快速创建数据源和各种监控报表了,具体操作如下:
  • 浏览器访问Grafana页面:http://192.168.1.101:3000
  • 用户名admin,密码secret
  • 在左侧的设置菜单中,点击"API keys",如下图:

在这里插入图片描述

  • 如下图,点击按钮"+New API Key":

在这里插入图片描述

  • 在创建API key的页面,输入key名称admin,Role选择Admin,然后点击"Add"按钮,如下图:

在这里插入图片描述

  • 弹出的窗口会显示新建的API Key,下图红框1中的内容就是我们后面会用到的API Key,请保存下来,红框2中的命令是获取首页信息的,可以执行一下试试,成功了表示这个API Key是有效的:

在这里插入图片描述

创建监控图表

  • 执行以下命令即可创建监控图表,注意把192.168.1.101替换为您的CentOS机器的IP地址,如果是在当前机器上操作就用127.0.0.1,把xxxxxxxxx替换为前面步骤中取得的API Key,例如我的是eyJrIjoieHpHeUVoODFiMDNiZE13TDdqUGxNS1NoN0dhVGVVc04iLCJuIjoiYWRtaW4iLCJpZCI6MX0=
wget https://raw.githubusercontent.com/zq2599/blog_demos/master/prometheusdemo/files/import_dashboard.sh && \
./import_dashboard.sh 192.168.1.101 xxxxxxxxx
  • 控制台会输出如下类似信息,表示数据源和图表都创建成功:
[root@hedy 10]# wget https://raw.githubusercontent.com/zq2599/blog_demos/master/prometheusdemo/files/import_dashboard.sh && \
> chmod a+x import_dashboard.sh && \
> ./import_dashboard.sh 192.168.1.101 eyJrIjoiSEduM2g1clFpaVZRNjRXTTBSek50aUp3OFM3bXUzVUciLCJuIjoiYWRtaW4iLCJpZCI6MX0=
--2019-03-09 23:46:27--  https://raw.githubusercontent.com/zq2599/blog_demos/master/prometheusdemo/files/import_dashboard.sh
正在解析主机 raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.108.133
正在连接 raw.githubusercontent.com (raw.githubusercontent.com)|151.101.108.133|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:113606 (111K) [text/plain]
正在保存至: “import_dashboard.sh”

100%[==================================================================================================================================================================>] 113,606      468KB/s 用时 0.2s   

2019-03-09 23:46:28 (468 KB/s) - 已保存 “import_dashboard.sh” [113606/113606])

grafana host [192.168.1.101]
api key [eyJrIjoiSEduM2g1clFpaVZRNjRXTTBSek50aUp3OFM3bXUzVUciLCJuIjoiYWRtaW4iLCJpZCI6MX0=]
start create datasource
{"datasource":{"id":2,"orgId":1,"name":"Prometheus","type":"prometheus","typeLogoUrl":"","access":"proxy","url":"http://prometheus:9090","password":"","user":"","database":"","basicAuth":false,"basicAuthUser":"","basicAuthPassword":"","withCredentials":false,"isDefault":false,"secureJsonFields":{},"version":1,"readOnly":false},"id":2,"message":"Datasource added","name":"Prometheus"}
start create host dashboard
{"id":3,"slug":"1-zhu-ji-ji-chu-jian-kong-cpu-nei-cun-ci-pan-wang-luo","status":"success","uid":"h1qcOBjmz","url":"/d/h1qcOBjmz/1-zhu-ji-ji-chu-jian-kong-cpu-nei-cun-ci-pan-wang-luo","version":1}
start create jvm dashboard
{"id":4,"slug":"spring-boot-1-x","status":"success","uid":"MP3cOBjmz","url":"/d/MP3cOBjmz/spring-boot-1-x","version":1}
start create customize dashboard
{"id":5,"slug":"ye-wu-zi-ding-yi-jian-kong","status":"success","uid":"Hl3cOBCmk","url":"/d/Hl3cOBCmk/ye-wu-zi-ding-yi-jian-kong","version":1}
  • 至此,整个环境已经OK,一起来体验一下吧;

体验环境

  • 查看prometheus的web页面,看配置的监控数据源是否有效,如下图,地址是:http://192.168.1.101:9090/targets

在这里插入图片描述

  • 上图一共展示了四个数据源:

a. http://prometheusdemohost:8080/prometheus 是个springboot框架的web应用;
b. http://127.0.0.1:9090/metrics 是prometheus自身的数据;
c. http://cadvisorhost:8080/metrics 是cadvisor的数据,用来监控docker容器的;
d. http://node-exporterhost:9100/metrics 是当前宿主机的环境信息;

  • 查看cAdvisor的web页面,可以看到docker的基本情况,如下图,地址是:http://192.168.1.101:8080

在这里插入图片描述

  • 访问业务web服务,这是个springboot框架的web应用,提供了一个web接口,返回一个带了时间的字符串,地址是:http://192.168.1.101:8081/greet
  • 注意:每访问一次上述业务web接口,web应用都会上报一条记录到prometheus,所以建议您多访问几次改接口,后面可以再Grafana上看到改接口的请求量曲线图;
  • 查看Grafana的监控图表,地址是:http://192.168.1.101:3000 ,如下图,可见数据源已经配置成了prometheus(前面的shell脚本中执行的):

在这里插入图片描述

  • 如下图,点击红框1中的"manage",即可见到右侧监控列表中出现了三项:

在这里插入图片描述

  • 从第一个"1.主机基础监控(cpu,内存,磁盘,网络)"看起,如下图,服务器的各项信息都能展示出来,注意红框中的内容要选择准确,可以截图一致即可:

在这里插入图片描述

  • 第二项"Spring Boot 1.x"主要是JVM和Spring Boot环境的相关信息,如下:

在这里插入图片描述

  • 第三项是业务代码中自己上报到prometheus的数据,这些可以和具体业务做绑定,做出更多定制化的内容(具体的开发细节后面的文章会讲到),如下图:

在这里插入图片描述

欢迎关注阿里云开发者社区博客:程序员欣宸

学习路上,你不孤单,欣宸原创一路相伴...
相关实践学习
通过可观测可视化Grafana版进行数据可视化展示与分析
使用可观测可视化Grafana版进行数据可视化展示与分析。
相关文章
|
6天前
|
Prometheus Kubernetes 监控
Kubernetes(K8S) 监控 Prometheus + Grafana
Kubernetes(K8S) 监控 Prometheus + Grafana
22 2
|
11天前
|
存储 运维 Linux
运维系列.在Docker中使用Grafana(一)
运维系列.在Docker中使用Grafana(一)
19 5
|
11天前
|
存储 Linux 数据库
运维系列.在Docker中使用Grafana(二)
运维系列.在Docker中使用Grafana(二)
28 4
|
1月前
|
Kubernetes Cloud Native 持续交付
云原生架构的核心组成部分通常包括容器化(如Docker)、容器编排(如Kubernetes)、微服务架构、服务网格、持续集成/持续部署(CI/CD)、自动化运维(如Prometheus监控和Grafana可视化)等。
云原生架构的核心组成部分通常包括容器化(如Docker)、容器编排(如Kubernetes)、微服务架构、服务网格、持续集成/持续部署(CI/CD)、自动化运维(如Prometheus监控和Grafana可视化)等。
|
17天前
|
Prometheus 监控 Cloud Native
性能监控之 node_exporter+Prometheus+Grafana 实现主机监控
【8月更文挑战第3天】性能监控之 node_exporter+Prometheus+Grafana 实现主机监控
19 0
|
3月前
|
Prometheus Cloud Native Java
微服务框架(二十三)Prometheus + Grafana 安装、配置及使用
此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。 本文为Prometheus + Grafana 安装、配置及使用 本系列文章中所使用的框架版本为Spring ...
|
2月前
|
Prometheus 监控 Cloud Native
基于Prometheus和Grafana的监控平台 - 环境搭建
基于Prometheus和Grafana的监控平台 - 环境搭建
|
1月前
|
JSON Prometheus 监控
Prometheus+Grafana 部署
Prometheus 和 Grafana 组成监控解决方案。Prometheus 是开源系统监控工具,Grafana 则用于数据可视化。要连接 Prometheus 数据源,登录 Grafana,点击设置,选择“连接”,添加新数据源,选择 Prometheus 类型,并填入 Prometheus 服务器的 HTTP 地址,如 `http://192.168.1.1:9090`,验证连接。之后,从 Grafana 官方仪表板库导入监控面板,如主机监控模板,以可视化系统状态。完成这些步骤后,便建立了有效的监控系统。
56 1
|
2月前
|
Prometheus 监控 Cloud Native
性能监控神器Prometheus、Grafana、ELK 在springboot中的运用
【6月更文挑战第27天】在 Spring Boot 应用中,监控和日志管理是确保系统稳定性和性能的重要手段。
57 4
|
1月前
|
JSON Prometheus 监控
Prometheus+Grafana主机运行数据
Prometheus 是一款开源监控工具,Node Exporter 是其官方插件,用于收集服务器硬件和系统指标。要安装 Node Exporter,下载最新二进制文件,解压并移动到目标位置。启动二进制文件或设置为服务。在 Prometheus 配置文件 `prometheus.yml` 中添加 Node Exporter 目标配置
26 0