操作系统:centos7
1、准备mysql镜像及mysqld_exporter安装包
2、构建镜像
1)编写Dockerfile
- ~]# mkdir -p /data/file
- ~]# cd /data/file/
- file]# vim Dockerfile
-
- FROM mysql:5.7.21
- ADD mysqld_exporter-0.13.0-rc.0.linux-amd64.tar.gz /prometheus-monitor/exporter
- ADD my.cnf /prometheus-monitor/exporter/mysqld_exporter-0.13.0-rc.0.linux-amd64/
- ADD start.sh /prometheus-monitor/exporter/mysqld_exporter-0.13.0-rc.0.linux-amd64/
- RUN chmod +x /prometheus-monitor/exporter/mysqld_exporter-0.13.0-rc.0.linux-amd64/mysqld_exporter
- RUN chmod +x /prometheus-monitor/exporter/mysqld_exporter-0.13.0-rc.0.linux-amd64/start.sh
- WORKDIR /prometheus-monitor/exporter/mysqld_exporter-0.13.0-rc.0.linux-amd64/
- CMD ["/prometheus-monitor/exporter/mysqld_exporter-0.13.0-rc.0.linux-amd64/start.sh"]
- expose 9080
2)准备start.sh脚本
- service mysql start
- ./mysqld_exporter --config.my-cnf=my.cnf --web.listen-address=:9080
3)准备my.cnf文件
- [client]
- host=localhost
- user=exporter
- password=123123
4)执行命令构建镜像
# docker build -t mysql_exporter .
3、启动容器并创建exporter用户授权权限
- # docker run -itd --name=mysql -p 9080:9080 -e mysql_root_password=123123 mysql_exporter
- # docker exec -it mysql bash
-
- root@85f367fbc12f:/prometheus-monitor/exporter/mysqld_exporter-0.13.0-rc.0.linux-amd64# mysql -uroot -p123123
- mysql> use mysql;
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
-
- Database changed
- mysql> create user 'exporter' identified by '123123';
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> grant process,replication client,select on *.* to 'exporter'@'%';
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> FLUSH PRIVILEGES;
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> exit
重启容器
# docker restart mysql
访问:http://192.168.53.7:9080/metrics
通过访问该地址可以看到mysql监控的相关指标
将此监控纳入外部prometheus服务上,并使用grafana进行展示即完成
重启Prometheus服务