写点什么

搭建 Prometheus+Grafana 的云平台监控系统

用户头像
学神来啦
关注
发布于: 2021 年 06 月 16 日


1.1 Prometheus 和 Grafana 概述 1.1.1 Prometheus 概述 Prometheus(普罗米修斯)是一套开源的监控 &报警 &时间序列数据库的组合,起始是由 SoundCloud 公司开发的。现在最常见的 Docker、Mesos、Kubernetes 容器管理系统中,通常会搭配 Prometheus 进行监控。 Prometheus [prəˈmiθju:s] 普罗米修斯 Prometheus 基本原理是通过 HTTP 协议周期性抓取被监控组件的状态,这样做的好处是任意组件只要提供 HTTP 接口就可以接入监控系统,不需要任何 SDK 或者其他的集成过程。这样做非常适合虚拟化环境比如 VM 或者 Docker 。输出被监控组件信息的 HTTP 接口被叫做 exporter。目前互联网公司常用的组件大部分都有 exporter 可以直接使用,比如 Varnish、Haproxy、Nginx、MySQL、Linux 系统信息 (包括磁盘、内存、CPU、网络等等),具体支持的源看:https://github.com/prometheus。 exporter ([ekˈspɔ:tə(r)] 出口商)对比主流的监控工具的操作界面:nagios 监控界面:



zabbix 监控界面



Grafana+Prometheus 监控界面:



通过上面的界面,我们可以看出来 Grafana+Prometheus 展示界面更美观 1.1.2 Prometheus 架构图解普罗米修斯(Prometheus)及其一些生态系统组件的整体架构:



retrieval [rɪˈtriːvl] 数据检索 Prometheus 各组件运行流程如下:1、Prometheus Server:Prometheus Sever 是 Prometheus 组件中的核心部分,负责实现对监控数据的获取,存储及查询。Prometheus Server 可以通过静态配置管理监控目标,也可以配合使用 Service Discovery(服务发现)的方式动态管理监控目标,并从这些监控目标中获取数据。其次 Prometheus Sever 需要对采集到的数据进行存储,Prometheus Server 本身就是一个实时数据库,将采集到的监控数据按照时间序列的方式存储在本地磁盘当中。Prometheus Server 对外提供了自定义的 PromQL,实现对数据的查询以及分析。另外 Prometheus Server 的联邦集群能力可以使其从其他的 Prometheus Server 实例中获取数据。2、Exporters:Exporter 将监控数据采集的端点通过 HTTP 服务的形式暴露给 Prometheus Server,Prometheus Server 通过访问该 Exporter 提供的 Endpoint 端点,即可以获取到需要采集的监控数据。可以将 Exporter 分为 2 类:(1)、直接采集:这一类 Exporter 直接内置了对 Prometheus 监控的支持,比如 cAdvisor,Kubernetes,Etcd,Gokit 等,都直接内置了用于向 Prometheus 暴露监控数据的端点。(2)、间接采集:原有监控目标并不直接支持 Prometheus,因此需要通过 Prometheus 提供的 Client Library 编写该监控目标的监控采集程序。例如:Mysql Exporter,JMX Exporter,Consul Exporter 等。3、AlertManager:在 Prometheus Server 中支持基于 Prom QL 创建告警规则,如果满足 Prom QL 定义的规则,则会产生一条告警。常见的接收方式有:电子邮件,webhook 等。4、PushGateway:Prometheus 数据采集基于 Prometheus Server 从 Exporter pull 数据,因此当网络环境不允许 Prometheus Server 和 Exporter 进行通信时,可以使用 PushGateway 来进行中转。Prometheus 的工作流:1.Prometheus server 定期从配置好的 jobs 和 exporters 中拉取 metrics,或者接收来自 Pushgateway 发送过来的 metrics,或者从其它的 Prometheus server 中拉 metrics。metrics [ˈmetrɪks] 衡量指标 2.Prometheus server 在本地存储收集到的 metrics,并运行定义好的 alerts.rules,记录新的时间序列或者向 Alert manager 推送警报。3.Alertmanager 根据配置文件,对接收到的警报进行处理,发出告警。4.在图形界面中,可视化采集数据。实验拓扑图:




搭建 Prometheus+Grafana 的云平台监控系统工作流程:1、安装 exporter(出口)采集数据 2、美化采集到的数据 3、在 web 界面查看懂:1 不懂:21.2 安装配置 Prometheus 监控服务 1、安装 go 语言环境方法 1:使用 tar 包安装 go 语言环境上传到 go1.13.3.linux-amd64.tar.gz 到 linux 服务器上或在线下载:[root@xuegod63 ~]# wget -c https://storage.googleapis.com/golang/go1.13.3.linux-amd64.tar.gz[root@xuegod63 ~]# tar -zxvf go1.13.3.linux-amd64.tar.gz -C /usr/local[root@xuegod63 ~]# echo "export PATH=$PATH:/usr/local/go/bin" > /etc/profile.d/go.sh[root@xuegod63 ~]# source /etc/profile.d/go.sh[root@xuegod63 ~]# go



方法 2:使用 yum 安装 go 语言环境[root@xuegod63 ~]# yum install -y epel-release[root@xuegod63 ~]# yum install go -y[root@xuegod63 ~]# go version 扩展:Go(又称 Golang)是 Google 开发的一种静态强类型、编译型、并发型,并具有垃圾回收功能的编程语言。下载安装 Prometheus(https://prometheus.io/download/)[root@xuegod63 ~]#wget https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.linux-amd64.tar.gz[root@xuegod63 ~]# tar zxvf prometheus-2.27.1.linux-amd64.tar.gz -C /usr/local/[root@xuegod63 ~]# cd /usr/local/prometheus-2.27.1.linux-amd64/修改 prometheus 配置文件,配置监控 linux 主机和 mysql 数据库,配置文件为 yml 语法,注意缩进对齐[root@xuegod63 prometheus-2.7.1.linux-amd64]# vim prometheus.yml #在文档的最后插入以下内容


  • job_name: system-statusstatic_configs:

  • targets: ['192.168.1.64:9100']labels:instance: xuegod64

  • job_name: mysql-statusstatic_configs:

  • targets: ['192.168.1.64:9104']labels:instance: xuegod64-mysql 插入时,要和文件之前的 job_name 保持对齐,如下图:


注释:


  • job_name: system-status #每一个 job_name 都是一个监控项,这里代表监控操作系统状态 static_configs:

  • targets: ['192.168.1.64:9100'] #被监控主机 IP 地址和端口 labels:instance: xuegod64 #实例名称,在 grafana 中表示对应的主机。

  • job_name: mysql-status #监控 mysql 数据库状态 static_configs:

  • targets: ['192.168.1.64:9104'] #目标主机 IP 地址和端口 labels:instance: xuegod64-mysql #实例名称,在 grafana 中表示对应的主机。[root@xuegod63prometheus-2.7.1.linux-amd64]# ./prometheus --config.file=prometheus.yml


出现以下信息吧表示启动成功。或直接后台运行:[root@xuegod63prometheus-2.7.1.linux-amd64]# nohup ./prometheus --config.file=prometheus.yml &[root@xuegod63 ~]# systemctl stop firewalld && systemctl disable firewalld #关闭防火墙浏览器输入http://192.168.1.63:9090



注:因为我们还没有开始配置 xuegod64 了,所以现在还获取不到 xuegod64 的信息 1.3 启动用于采集 linux 系统和 mysql 服务状态的 exporter 服务 prometheus 常见的 exporter 及作用:(1)、node_exporter 用于监控操作系统的性能和运行状态(2)、mysqld_exporter 用于监控 mysql 服务(3)、snmp_exporter 用于监控网络设备更多 exporter 可在官网下载:https://prometheus.io/download/#node_exporter



1、配置 node_exporter 监控 xuegod64 运行状态[root@xuegod64 ~]# systemctl stop firewalld && systemctl disable firewalld #关闭防火墙上传 node_exporter-1.1.2.linux-amd64.tar.gz 到 linux 上。[root@xuegod64 ~]# tar zxvf /root/node_exporter-1.1.2.linux-amd64.tar.gz -C /usr/local/[root@xuegod64 ~]# nohup /usr/local/node_exporter-1.1.2.linux-amd64/node_exporter &2、配置 mysqld_exporter 用于监控 xuegod64 上的 mysql 服务(1)、安装数据库服务[root@xuegod64 ~]# yum install mariadb-server mariadb -y[root@xuegod64 ~]# systemctl start mariadb[root@xuegod64 ~]# mysql #直接登录 mysqlmysql> GRANT REPLICATION CLIENT,PROCESS ON . TO 'mysql_monitor'@'localhost' identified by '123456';mysql> GRANT SELECT ON . TO 'mysql_monitor'@'localhost';MariaDB [(none)]> exit;注:mysql 用户权限说明:(1)、replication client 权限 #拥有此权限可以查询 master server、slave server 状态。(2)、PROCESS 权限 #通过这个权限,用户可以执行 SHOW PROCESSLIST 和 KILL 命令。默认情况下,每个用户都可以执行 SHOW PROCESSLIST 命令,但是只能查询本用户的进程。如:mysql> show processlist;另外,管理权限 process 不能够指定某个数据库,on 后面必须跟*.*(3)、select 权限 #必须有 select 的权限,才可以使用 select * from table 查看数据(2)、安装 mysqld_exporter



上传 mysqld_exporter 到 linux 系统上[root@xuegod64 ~]# tar xf mysqld_exporter-0.13.0-rc.0.linux-amd64.tar.gz -C /usr/local/创建隐藏配置文件.my.cnf,用于 mysqld_exporter 连接 mysql 采集数据。[root@xuegod64 ~]# vim /usr/local/mysqld_exporter-0.13.0-rc.0.linux-amd64/.my.cnf[client]user=mysql_monitorpassword= 123456[root@xuegod64 ~]# nohup /usr/local/mysqld_exporter-0.13.0-rc.0.linux-amd64/mysqld_exporter --config.my-cnf="/usr/local/mysqld_exporter-0.13.0-rc.0.linux-amd64/.my.cnf" &在 web 界面,再次查看监控信息已经成功添加 linux 和 mysql 节点,已经 up 了浏览器输入http://192.168.1.63:9090



查看网络端口:[root@xuegod64 ~]# netstat -antup | grep 91tcp6 0 0 :::9100 :::* LISTEN 2158/node_exporter


tcp6 0 0 :::9104 :::* LISTEN 41308/mysqld_export 总结 prometheus 使用方法:1、想监控操作系统,只需要一步:在被监控系统上安装 node_exporter 并启动,就 ok 了 2、想监控操 mysql 数据库,只需要两步:(1)、创建一个 mysql 用户(2)、安装 mysqld_exporter 并启动 1.4 实战-使用 Grafana 可视化工具美化监控数据显示效果 1.4.1 配置 grafana1、上传 grafana 到 linux 系统/root/下[root@xuegod63 ~]# yum install -y ./grafana-7.5.7-1.x86_64.rpm 或在线下载:[root@xuegod64 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/grafana-7.5.7-1.x86_64.rpm2、安装图形饼状图插件,方便显示饼状图方法 1:离线安装下载:https://grafana.com/api/plugins/grafana-piechart-panel/versions/1.6.1/download然后上传 grafana-piechart-panel-1.6.1.zip 到 linux 系统上[root@xuegod63 ~]# mkdir /var/lib/grafana/plugins #创建存储插件的路径[root@xuegod63 ~]# unzip grafana-piechart-panel-1.6.1.zip


[root@xuegod63 ~]# mv grafana-piechart-panel /var/lib/grafana/plugins/grafana-piechart-panel[root@xuegod63 ~]# systemctl restart grafana-server 方法 2:在线安装[root@xuegod63 ~]# grafana-cli plugins install grafana-piechart-panel[root@xuegod63 ~]# systemctl restart grafana-server #安装完插件,需要重启服务 3、访问:http://192.168.1.63:3000/login 用户名:admin 密码 admin



第一次登陆会提示修改密码,咱们改成 123456



配置数据源



url 地址:http://192.168.1.63:9090



在页面最下面,点:Save & Test



Scrape [skreɪp] 刮擦 ; interval [ˈɪntəvl] 间隔;Scrape interval 抓取数据间隔弹出下面信息,说明成功了



1.4.2 将监控 Linux 系统和 mysql 服务运行状态的 web 仪表盘插件导入 grafana1、查找可以美化 Linux 系统监控数据的仪表盘插件




打开官方网站:https://grafana.com/grafana/dashboards在页面中找到这个:https://grafana.com/grafana/dashboards?search=node输入关键字 node,进行查询,查看自已想要的展示看板:



我使用们这个 ID:8919 打开这个链接:https://grafana.com/grafana/dashboards/8919



也可以直接下载 JSON 文件,然后后期,直接导入这个离线文件:



ID 号是: 8919。 注: 如果在线导入的看板,看不到数据,可以换一个 ID 号,再试试。开始导入:



点 load,



刷新浏览器界面:



注:也可以导入之前下载的 node-exporter-for-prometheus-dashboard-cn-v20201010_rev24.json 离线文件上传。



2、查出 mysql 的 Web 仪表盘插件的 ID 号打开此链接可直接搜索到与课程对应版本的仪表盘https://grafana.com/grafana/dashboards?search=mysql%20overview&utm_source=grafana_search然后选择这个由 nasskach 开发的界面



打开链接:https://grafana.com/grafana/dashboards/7362 复制 7362 的 ID



来到 grafana 主界面,导入 dashboard:





下拉选择 Prometheus




1.5 实战-Grafana 配置邮件告警 1.5.1 配置 SMTP 发送邮件配置 163 邮箱 smtp 服务



点击开启 SMTP 服务,点击开启后会提示我们下载手机 APP,我们点继续开启不需要下载 APP。



根据提示发送手机验证码即可开启成功



EMXCERHXVHVDXKJF 开启成功后会新增授权密码管理的选项,如果你之前已经开启过,直接点击新增授权密码即可。



Grafana 配置 SMTP 账户[root@xuegod63 ~]# vim /etc/grafana/grafana.ini 第 522 行修改 smtp 配置



注:所有修改配置前 ; 注释符号需要取消,将对应行,改为以下内容:[smtp]enabled = true #启用 smtphost = smtp.163.com:465 #163 smtp 服务地址 user = jianmingbasic@163.com #163 邮箱用户名 #If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""password = EMXCERHXVHVDXKJF #授权密码; cert_file =; key_file =; skip_verify = falsefrom_address = jianmingbasic@163.com #邮件 From 地址,和登录用户一致即可。;from_name = Grafana; ehlo_identity =修改完邮件配置后修改 domain 信息,邮件内容中包含 Grafana 的访问地址,默认地址为 localhost。改:41 ;domain = localhost 为:41 domain = 192.168.1.631.5.2 配置图片渲染功能配置插件地址改:813 ;server_url =为:813 server_url = http://192.168.1.63:8081/render配置 callback_url,该地址为 Grafana 地址。改:815 ;callback_url =815 callback_url = http://192.168.1.63:3000/修改默认语言,默认配置渲染图片时不支持中文。改:840 ;rendering_language =为:840 rendering_language = zh 安装图片渲染插件(经测试仅 docker 方式搭建的插件支持中文渲染)安装 docker 环境[root@xuegod63 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 配置国内 docker 的 yum 源(阿里云)[root@xuegod63 ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo安装 docker-ce[root@xuegod63 ~]# yum makecache fast[root@xuegod63 ~]# yum install docker-ce docker-ce-cli containerd.io -y[root@xuegod63 ~]# systemctl start docker && systemctl enable docker.service[root@xuegod63 ~]#tee /etc/docker/daemon.json << 'EOF'{"registry-mirrors":["https://rsbud4vc.mirror.aliyuncs.com","https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn","https://dockerhub.azk8s.cn","http://hub-mirror.c.163.com","http://qtid6917.mirror.aliyuncs.com","https://rncxm540.mirror.aliyuncs.com","https://e9yneuy4.mirror.aliyuncs.com"]}EOF[root@xuegod63 ~]# systemctl daemon-reload[root@xuegod63 ~]# systemctl restart docker 运行 docker 版本的图片渲染插件上传课程资料中的 grafana-image-renderer.tar.gz[root@xuegod63 ~]# docker load -i grafana-image-renderer.tar.gz[root@xuegod63 ~]# docker run -d -p 8081:8081 --restart=always index.docker.io/grafana/grafana-image-renderer:latest 重启 Grafana 使配置生效[root@xuegod63 ~]# systemctl restart grafana-server 检查邮件服务是否配置成功,添加告警频道。




取任意名称,选择 Email 发送,填写收件人地址即可发送测试邮件。Include image 需要手动勾选,这样发送邮件时才会附带监控图表信息。新版本中默认不勾选,因为需要安装插件。



测试成功后点击保存即可。



收到 Grafana 邮件



内容是测试信息



1.5.3 自定义监控项与告警规则配置告警,回到仪表盘进行操作。




添加 panel




修改一个新的标题 Panel title: xuegod64-CPU 使用率



修改监控项配置。删除监控项。保留 user 配置即可。因为我们手工测试只能测试到 User 监控项。删除 AFDC 保留 Bmetrics [ˈmetrɪks] 衡量指标 Metrics: (1 - avg(rate(node_cpu_seconds_total{instance=~"xuegod64",mode="idle"}[30s])) by (instance)) * 100Legend: CPU 使用率注:idle 表示总负载,system 表示系统使用率,user 表示用户使用率。只留这个监控项参数



添加告警



配置告警规则。xuegod64-cpu 使用率报警随便输出点告警描述信息:xuegod64-CPU > 50% (test)



注:query 查询的时间默认是 5 分钟的平均数据,如果想快速触发报警可以时间缩短至 1m。0.5=50%保存配置,点击按钮




点击 save 保存即可可以看到这里有这个标识:



应用一下:



1.5.4 压力测试触发告警安装压力测试工具[root@xuegod64 ~]# yum install -y epel-release[root@xuegod64 ~]# yum install -y stress 对 CPU 进行压力测试。注:教学环境中 xuegod64 分配了 8 核心 CPU,我们的监控指标配置的是超过 50%告警,则需要将超过半数的 CPU 资源耗尽,所以-c 参数大于虚拟机中 CPU 核心数量的一半即可,同学们根据自己学习环境进行调整。[root@xuegod64 ~]# stress -c 6



新建终端窗口使用 top 命令查看系统负载[root@xuegod64 ~]# top



已经创建了 6 个占用 CPU100%的进程。查看状态信息,可以看到 pending 和发送邮件间隔了 1 分钟。



查看邮箱



邮件中可以看到监控指标数据



Ctrl+C 关闭压力测试工具




等待平均负载指标下来后就会收到恢复邮件。



总结:

1.1 Prometheus 和 Grafana 概述

1.2 安装配置 Prometheus 监控服务

1.3 启动用于采集 linux 系统和 mysql 服务状态的 exporter 服务

1.4 实战-使用 Grafana 可视化工具美化监控数据显示效果

1.5 实战-Grafana 配置邮件告警


关注'小神'不迷路每天为大家带来优越的技术干货


用户头像

学神来啦

关注

还未添加个人签名 2021.06.02 加入

10年Linux使用及管理经验,7年IT在线教育培训经验。精通Linux、Python、思科、C++、安全渗透等技术。曾任职国内知名互联网公司高级运维架构师、运维总监。对基于Linux下开源程序Openstack、Docker、K8S、DevOps、

评论

发布
暂无评论
搭建Prometheus+Grafana的云平台监控系统