Prometheus 监控(4):数据库服务监控(mysql、mongodb、redis、elasticsearch)

872 阅读4分钟

“持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第3天,点击查看活动详情

前言

这节主要介绍四种数据库及缓存的监控

一、监控 mysql

1、前往地址中心下载 mysql_exporter 支持 prometheus.io/download/

image-20220111095847384

2、上传文件并解压到指定目录

 [root@localhost package]# tar -zxvf mysqld_exporter-0.13.0.linux-amd64.tar.gz -C /opt/software/

3、修改目录

 [root@localhost software]# mv mysqld_exporter-0.13.0.linux-amd64/ mysql_exporter_0.13.0

4、创建指定账户用来连接数据库(可用直接用root账户,不过建议创建个专有账户)

 mysql> CREATE USER 'iedp'@'%' IDENTIFIED BY 'iedp@2021' WITH MAX_USER_CONNECTIONS 5 ; // 限制用户最大连接数,避免监控引起数据库过载
 mysql> GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'iedp'@'%';
 FLUSH PRIVILEGES;

5、配置数据库连接认证信息并启动 mysql_exporter

  • 使用配置文件方式,在当前目录创建

     $ vim my.cnf
     [client]
        host=localhost
        port=3306
        user=exporter
        password=iedp@2021   
    
  • 启动exporter 客户端

 命令  ./mysqld_exporter --config.my-cnf=.my.cnf
 ​
 常用参数:
 # 选择采集innodb
 --collect.info_schema.innodb_cmp
 # innodb存储引擎状态
 --collect.engine_innodb_status
 # 指定配置文件
 --config.my-cnf=".my.cnf"

6、访问 http://192.168.81.104:9104/metrics 查看 mysql 的收集日志

7、添加系统服务,启动mysql 节点更加方便。

 vi /usr/lib/systemd/system/mysql_exporter.service

地址修改为自己的目录地址。

 [Unit]
 Description=https://prometheus.io
 ​
 [Service]
 ​
 Environment=DATA_SOURCE_NAME=iedp:iedp@2021@(localhost:3306)/
 Restart=on-failure
 ExecStart=/opt/software/mysql_exporter_0.13.0/mysqld_exporter --config.my-cnf=.my.cnf --web.listen-address=:9104
 ​
 [Install]
 WantedBy=multi-user.target

启动添加后的系统服务

 systemctl daemon-reload
 systemctl restart mysql_exporter.service

8、去 Prometheus.yml 中添加 mysqld_exporter 的配置

 # 添加 mysql 的监控配置
   - job_name: 'mysqld_exporter'
     scheme: http
     static_configs:
     - targets: ['192.168.81.104:9104']
     labels:
      app: mysqld_exporter
      role: mysqld_exporter

9、检查并重启服务,先关闭之前服务。

 [root@localhost prometheus2.29]# ./promtool check config prometheus.yml
 Checking prometheus.yml
   SUCCESS: 0 rule files found

image-20220111111355240

10、最后导入Grafana 中的mysql 版本使用

二、监控 redis

下载地址:github.com/oliver006/r…

1、下载最新版本的 redis_exporter

 # 下载
 [root@localhost package]# wget https://github.com/oliver006/redis_exporter/releases/download/v1.35.0/redis_exporter-v1.35.0.linux-amd64.tar.gz
 # 解压
 [root@localhost package]# tar -zxvf redis_exporter-v1.35.0.linux-amd64.tar.gz -C /opt/software/

2、启动 redis_exporter

 # 进入解压后的目录,有密码的输入:
 [root@localhost redis_exporter]# nohup ./redis_exporter -redis.addr 192.168.81.104:6379 -redis.password redis@admin2021 &
 ​
 # 查看是否运行成功
 ss -auntlp |grep 9121

3、promtheus 监控 redis

 # 添加 redis 的配置
   - job_name: 'redis-exporter'
     static_configs:
     - targets: ['192.168.81.104:9121']

重启 prometheus

 curl -X POST http://192.168.81.104:9090/-/reload 

img

4、grafana 配置 导入模板 id 为 763

问题:有时候 redis 的面板的 memory usage 的值为 无穷大,这是因为没有为 memory 配置大小 可用将 redis_memory_max_bytes 改为真实的服务器大小。

image.png

三、监控 mongodb

先下载对应的mongodb安装包,进行安装。安装步骤先略过,参考这篇文章:

blog.csdn.net/zhizhenggua…

 # 创建一个管理用户
 db.createUser({user:"admin",pwd:"admin",roles:[{role:"dbAdminAnyDatabase",db:"admin"}]})

1、下载 mongodb_exporter

github地址 : github.com/percona/mon… 下载地址 : github.com/percona/mon…

 wget https://github.com/percona/mongodb_exporter/releases/download/v0.11.2/mongodb_exporter-0.11.2.linux-amd64.tar.gz
 新版本的 exporter 和grafana 模板不一致,不会显示指标数据,所以选用老版本。
 ​

2、启动并指定端口

 [root@node103 mongodb]# nohup ./mongodb_exporter --mongodb.uri=10.2.2.12:27017  --web.listen-address=:30028 &
 # 检查是否启动成功
 [root@node103 mongodb]# ps -ef|grep mongodb

3、prometheus 配置

 # 添加 mongodb 的配置
   - job_name: 'mongodb-exporter'
     static_configs: ['192.168.81.103:27017']

重启 prometheus

 curl -X POST http://192.168.81.104:9090/-/reload 

img

4、Grafana 配置

导入模板id 为 2583 即可

四、监控 elasticsearch

安装 es

 docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
 -e  "discovery.type=single-node" \
 -e ES_JAVA_OPTS="-Xms64m -Xmx512m" \
 -v /opt/software/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
 -v /opt/software/elasticsearch/data:/usr/share/elasticsearch/data \
 -v  /opt/software/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
 -d elasticsearch:7.6.2 

1、下载 es_exporter

地址: github.com/justwatchco…

下载解压:

 wget https://github.com/justwatchcom/elasticsearch_exporter/releases/download/v1.1.0/elasticsearch_exporter-1.1.0.linux-amd64.tar.gz

img

或者直接下载然后上传也可以。 我选的wget 下载 1.1.0 版本。

解压到制定目录,并修改名称:

  tar -zxvf elasticsearch_exporter-1.1.0.linux-amd64.tar.gz -C /opt/software/
  mv elasticsearch_exporter-1.1.0.linux-amd64/ es_exporter

2、启动

进入 exporter的根目录,输入以下命令

 nohup ./elasticsearch_exporter --es.uri http://192.168.81.103:9200 &

参数说明:

 -es.uri             默认http://localhost:9200,连接到的Elasticsearch节点的地址(主机和端口)。 这可以是本地节点(例如localhost:9200),也可以是远程Elasticsearch服务器的地址
 --es.all                默认flase,如果为true,则查询群集中所有节点的统计信息,而不仅仅是查询我们连接到的节点。
 --es.cluster_settings   默认flase,如果为true,请在统计信息中查询集群设置
 --es.indices            默认flase,如果为true,则查询统计信息以获取集群中的所有索引。
 --es.indices_settings   默认flase,如果为true,则查询集群中所有索引的设置统计信息。
 --es.shards             默认flase,如果为true,则查询集群中所有索引的统计信息,包括分片级统计信息(意味着es.indices = true)。
 --es.snapshots          默认flase,如果为true,则查询集群快照的统计信息。

3、访问

启动成功后,访问 http://192.168.81.103:9114/metrics,看抓取的指标信息。能访问说明启动成功。

img

4、配置 prometheus

在 prometheus.yml 中加入 elasticsearch 节点

   # 采集 es 监控数据
   - job_name: 'elasticsearch'
     static_configs:
     - targets: ['192.168.81.103:9114']

然后热重载或重启 prometheus

 curl -X POST http://192.168.81.104:9090/-/reload

5、配置 Grafana

导入 prometheus 的仪表盘,选择 id 为 2322

最后节点展示如下图 image-20220228115512596