Prometheus监控docker容器&Mysql
1.cadVisor
cadVisor是Google公司开源的一款容器监控工具,支持docker,podman容器管理工具的监控。
GitHub地址: https://github.com/google/cadvisor
1.2 安装docker环境
docker环境就使用一键安装脚本快速安装了【没有脚本就手动安装】
tar xf autoinstall-docker-docker-compose.tar.gz
./install-docker.sh i
1.3 所有docker节点安装cadvisor【若服务器可以FQ,可以直接运行此命令】
docker run \
-v /:/rootfs:ro \
-v /var/run:/var/run:ro \
-v /sys:/sys:ro \
-v /var/lib/docker/:/var/lib/docker:ro \
-v /dev/disk/:/dev/disk:ro \
-p 18080:8080 \
-d \
--name=cadvisor \
--privileged \
--device=/dev/kmsg \
gcr.io/cadvisor/cadvisor-amd64:v0.49.1
cadvisor网址:https://github.com/google/cadvisor/releases/tag/v0.49.1
1.4 访问测试
[root@Promethues13 ~]# curl -s 10.0.0.11:18080/metrics | head -3
# HELP cadvisor_version_info A metric with a constant '1' value labeled by kernel version, OS version, docker version, cadvisor version & cadvisor revision.
# TYPE cadvisor_version_info gauge
cadvisor_version_info{cadvisorRevision="6f3f25ba",cadvisorVersion="v0.49.1",dockerVersion="",kernelVersion="5.15.0-119-generic",osVersion="Alpine Linux v3.18"} 1
[root@Promethues13 ~]#
[root@Promethues13 ~]#
[root@Promethues13 ~]# curl -s 10.0.0.12:18080/metrics | head -3
# HELP cadvisor_version_info A metric with a constant '1' value labeled by kernel version, OS version, docker version, cadvisor version & cadvisor revision.
# TYPE cadvisor_version_info gauge
cadvisor_version_info{cadvisorRevision="6f3f25ba",cadvisorVersion="v0.49.1",dockerVersion="",kernelVersion="5.15.0-119-generic",osVersion="Alpine Linux v3.18"} 1
[root@Promethues13 ~]#
[root@Promethues13 ~]# curl -s 10.0.0.13:18080/metrics | head -3
# HELP cadvisor_version_info A metric with a constant '1' value labeled by kernel version, OS version, docker version, cadvisor version & cadvisor revision.
# TYPE cadvisor_version_info gauge
cadvisor_version_info{cadvisorRevision="6f3f25ba",cadvisorVersion="v0.49.1",dockerVersion="",kernelVersion="5.15.0-119-generic",osVersion="Alpine Linux v3.18"} 1
[root@Promethues13 ~]#
1.5 Prometheus定义要监控的docker主机
[root@Promethues11 ~]# vim /Project/softwares/prometheus-2.53.2.linux-amd64/prometheus.yml
...
- job_name: "nolen-docker"
static_configs:
- targets:
- 10.0.0.11:18080
- 10.0.0.12:18080
- 10.0.0.13:18080
[root@Promethues11 ~]# check
Checking /Project/softwares/prometheus-2.53.2.linux-amd64/prometheus.yml
SUCCESS: /Project/softwares/prometheus-2.53.2.linux-amd64/prometheus.yml is valid prometheus config file syntax
[root@Promethues11 ~]#
[root@Promethues11 ~]# rr
[root@Promethues11 ~]#
1.6 访问Prometheus的WebUI验证是否监控成功 【自己服务器的公网IP+端口号】
http://10.0.0.11:9090/targets
1.7 导入模板展示数据
315
10619

2.Prometheus如何监控MySQL
mysql本身并不支持metrics接口,因此需要独立部署mysql-exporter来提供metrics接口。
2.1 部署mysql
[root@Promethues13 ~]# docker run --name mysql-server -t \
-e MYSQL_DATABASE="nolen" \
-e MYSQL_USER="nolen" \
-e MYSQL_PASSWORD="1" \
-e MYSQL_ALLOW_EMPTY_PASSWORD="yes" \
--restart unless-stopped \
--network host \
-d mysql:8.3.0-oracle \
--character-set-server=utf8mb4 --collation-server=utf8mb4_bin \
--default-authentication-plugin=mysql_native_password
[root@Promethues13 ~]#
[root@Promethues13 ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dae78ac1e1cc mysql:8.3.0-oracle "docker-entrypoint.s…" 6 seconds ago Up 5 seconds mysql-server
[root@Promethues13 ~]#
[root@Promethues13 ~]# ss -ntl | grep 3306
LISTEN 0 70 *:33060 *:*
LISTEN 0 151 *:3306 *:*
[root@Promethues13 ~]#
[root@Promethues13 ~]# docker exec -it mysql-server mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 417
Server version: 8.3.0 MySQL Community Server - GPL
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| nolen |
+--------------------+
5 rows in set (0.00 sec)
mysql>
mysql> SELECT user,host,plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| nolen | % | mysql_native_password |
| root | % | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
6 rows in set (0.00 sec)
mysql>
2.2 下载mysql-exporter
[root@Promethues12 ~]#wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-amd64.tar.gz
[root@Promethues12 ~]# tar xf mysqld_exporter-0.15.1.linux-amd64.tar.gz -C /usr/local/bin/ mysqld_exporter-0.15.1.linux-amd64/mysqld_exporter --strip-components=1
2.3 创建MySQL的配置,指定默认的用户名和密码
[root@Promethues12 ~]# cat > /root/.my.cnf <<EOF
[client]
user=nolen
password=1
EOF
2.4 运行mysqld-exporter
[root@Promethues12 ~]# mysqld_exporter --mysqld.address="10.0.0.13:3306" --web.listen-address=:9104 --config.my-cnf="/root/.my.cnf"
2.5 访问mysqld-exporter的webui

2.6 修改Prometheus的配置文件
[root@Promethues11 ~]# vim /Project/softwares/prometheus-2.53.2.linux-amd64/prometheus.yml
...
- job_name: "nolen-mysqld"
static_configs:
- targets:
- 10.0.0.12:9104
...
[root@Promethues11 ~]# check
Checking /Project/softwares/prometheus-2.53.2.linux-amd64/prometheus.yml
SUCCESS: /Project/softwares/prometheus-2.53.2.linux-amd64/prometheus.yml is valid prometheus config file syntax
[root@Promethues11 ~]#
[root@Promethues11 ~]# rr
[root@Promethues11 ~]#
2.7 导入模板展示数据


浙公网安备 33010602011771号