title: Prometheus+node_exporter+mysqld_exporter+grafana部署与配置 #标题tags: Prometheus #标签
date: 2020-08-26
categories: 监控 # 分类

记录下Prometheus+node_exporter+mysqld_exporter+grafana协同工作的过程。

安装Prometheus

  1. $ wget https://github.com/prometheus/prometheus/releases/download/v2.20.1/prometheus-2.20.1.linux-amd64.tar.gz
  2. $ tar zxf prometheus-2.20.1.linux-amd64.tar.gz -C /opt/
  3. $ ln -sf /opt/prometheus-2.20.1.linux-amd64 /opt/prometheus
  4. $ ln -sf /opt/prometheus-2.20.1.linux-amd64/prometheus /usr/local/bin/
  5. # 配置文件调整
  6. $ sed -i 's/localhost/192.168.20.2/g' /opt/prometheus/prometheus.yml
  7. # 启动Prometheus
  8. prometheus --config.file="/opt/prometheus/prometheus.yml" \
  9. --web.enable-lifecycle --storage.tsdb.path=/opt/prometheus/data \
  10. --storage.tsdb.retention.time=30d &
  11. # --config.file: 指定配置文件
  12. # --web.enable-lifecycle: 支持通过http请求重载配置
  13. # --storage.tsdb.path: 指定数据存储目录(默认当前目录的的data目录,若不存在则新建)
  14. # --storage.tsdb.retention.time: 指定数据保留时间(默认15d)
  15. # 确定端口在监听
  16. $ ss -lnput | grep 9090 # 确定端口在监听
  17. tcp LISTEN 0 128 :::9090 :::* users:(("prometheus",pid=16429,fd=11))

浏览器访问

访问Prometheus主机的 9090 端口,点击如下,即可看到监控主机的相关信息(Prometheus默认监控本机):
Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图1

Prometheus相关操作

Prometheus的启停操作
  1. # 启动Prometheus
  2. $ prometheus --config.file="/opt/prometheus/prometheus.yml" --web.enable-lifecycle --storage.tsdb.path=/opt/prometheus/data --storage.tsdb.retention.time=30d & # 启动Prometheus
  3. # --web.enable-lifecycle 表示允许通过web接口重载配置文件
  4. # --storage.tsdb.path=/opt/prometheus/data 指定数据存储目录,默认是当前目录的data目录
  5. # --storage.tsdb.retention.time 指定数据保留时间,默认为15d。
  6. $ pkill prometheus # 停止Prometheus
  7. ############ 重载Prometheus的两种方式 ############
  8. # 命令行中给发送hup信号至Prometheus进程
  9. $ kill -HUP $(ps -ef | grep prometheus | grep -v grep | awk '{print $2}')
  10. # 通过web接口重载,启动时需增加选项 --web.enable-lifecycle
  11. $ curl -XPOST http://192.168.20.10:9090/-/reload
  12. # 如果配置文件有误,重启时将会看到类似以下的错误信息
  13. ERRO[0161] Error reloading config: couldn't load configuration (-config.file=prometheus.yml): unknown fields in scrape_config: job_nae source=main.go:146

Prometheus管理接口

健康检查

  1. $ curl http://192.168.20.10:9090/-/healthy

准备检查

  1. $ curl http://192.168.20.10:9090/-/ready
  2. Prometheus is Ready.

重载配置文件

  1. $ curl -XPOST http://192.168.20.10:9090/-/reload
  2. # 需启动时增加 --web.enable-lifecycle 选项。

停止Prometheus

  1. $ curl -XPUT http://192.168.20.10:9090/-/quit
  2. # 或者
  3. $ curl -XPOST http://192.168.20.10:9090/-/quit
  4. # 同样需启动时增加 --web.enable-lifecycle 选项。

安装配置grafana

下载及安装
  1. $ wget https://dl.grafana.com/oss/release/grafana-7.1.4-1.x86_64.rpm
  2. $ yum -y install grafana-7.1.4-1.x86_64.rpm
  3. $ systemctl daemon-reload
  4. $ systemctl enable grafana-server && systemctl start grafana-server

grafana添加Prometheus数据源

访问grafana的3000端口进行登录,默认用户名和密码为: admin/admin。

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图2

根据提示修改默认密码

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图3

添加数据源

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图4

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图5

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图6

指定Prometheus详细信息

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图7

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图8

点击测试并保存后,显示如下:

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图9

至此,数据源添加成功。

安装node_exporter

node_exporter用于收集主机运行信息,比如CPU、内存、磁盘等资源使用情况。

各种监控exporter,可以去官网下载

  1. $ wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
  2. $ tar zxf node_exporter-1.0.1.linux-amd64.tar.gz -C /opt/
  3. $ ln -sf /opt/node_exporter-1.0.1.linux-amd64 /opt/node_exporter
  4. $ /opt/node_exporter/node_exporter &
  5. $ ss -lnput | grep 9100 # 确定端口在监听
  6. tcp LISTEN 0 128 [::]:9100 [::]:* users:(("node_exporter",pid=7352,fd=3))

安装mysqld_exporter

mysqld_exporter 用于采集mysql相关信息,比如buffer缓冲区大小、运行时长、数据库的QPS、当前连接数等非常详细的信息。

  1. # 安装mysql用于测试
  2. $ yum -y install mariadb*
  3. $ systemctl start mariadb
  4. $ mysql -uroot # 登录到数据库授权监控用户
  5. MariaDB [(none)]> grant select,replication client,process on *.* to 'mysql_monitor'@'192.168.20.3' identified by '123.com';
  6. MariaDB [(none)]> flush privileges;
  7. $ wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
  8. $ tar zxf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /opt/
  9. $ ln -sf /opt/mysqld_exporter-0.12.1.linux-amd64 /opt/mysqld_exporter
  10. # 写入连接mysql账号信息
  11. $ cat > /opt/mysqld_exporter/.my.cnf << EOF
  12. [client]
  13. host=192.168.20.3
  14. user=mysql_monitor
  15. password=123.com
  16. # socket=/tmp/mysql.sock
  17. EOF
  18. $ /opt/mysqld_exporter/mysqld_exporter --config.my-cnf=/opt/mysqld_exporter/.my.cnf &
  19. $ ss -lnput | grep 9104
  20. tcp LISTEN 0 128 [::]:9104 [::]:* users:(("mysqld_exporter",pid=10201,fd=3))

调整Prometheus获取主机及应用监控数据

  1. $ vim /opt/prometheus/prometheus.yml
  2. - job_name: 'prometheus'
  3. # metrics_path defaults to '/metrics'
  4. # scheme defaults to 'http'.
  5. static_configs:
  6. - targets: ['192.168.20.2:9090']
  7. # 增加以下配置,其中job_name为自定义
  8. - job_name: 'agent1'
  9. static_configs:
  10. - targets: ['192.168.20.3:9100']
  11. - job_name: 'mysql1'
  12. static_configs:
  13. - targets: ['192.168.20.3:9104']
  14. # 重载Prometheus配置文件
  15. $ curl -XPOST http://192.168.20.2:9090/-/reload

确定Prometheus已采集到数据

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图10

使用grafana添加图形

添加主机资源使用情况的图形

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图11

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图12

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图13

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图14

导入mysqld图形模板

以下操作在grafana所在主机进行操作。

  1. $ git clone https://github.com/percona/grafana-dashboards.git
  2. $ cp -r grafana-dashboards/dashboards/ /var/lib/grafana/
  3. $ cat >> /etc/grafana/grafana.ini << EOF
  4. [dashboards.json]
  5. enabled = true
  6. path = /var/lib/grafana/dashboards
  7. EOF
  8. $ systemctl restart grafana-server # 重启grafana

下载图形模板

访问 https://github.com/percona/grafana-dashboards,下载zip包,如下:

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图15

在grafana web界面中导入json文件

将下载后的zip包解压后,找到grafana-dashboards-master\grafana-dashboards-master\dashboards目录,如下:

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图16

granfana 导入所需json文件:

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图17

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图18

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图19

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图20

哦吼,报错了,原因是此仪表盘默认寻找的数据源是Prometheus,这也就是我添加数据源时说的那个坑(我添加数据源是指定的数据源名称是Prometheus_data)。
Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图21

修改数据源名称

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图22

修改数据源名称为“Prometheus”,然后点击下方的保存即可。

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图23

查看MySQL数据库运行情况如下

Prometheus node_exporter mysqld_exporter grafana部署与配置 - 图24