在 CentOS7 的容器上用 PMM 对 MySQL 数据库做可视化监控

Percona 的开源数据库监控 PMM 简直是酷毙了。 老徐花了2个小时的时间,从无到有在云端的CentOS7 服务器上快速搭建了基于docker 容器技术的 PMM Server。在这里把实施步骤和遇到的问题汇总一下,十分值得初学者借鉴。

1. 容器的安装: # yum install docker ,然后启动 systemctl start docker;
2. 由于防火墙的原因,我们需要使用国内docker 镜像,使用 doacloud 加速,去 https://www.daocloud.io/ 注册以后,就可以得到一条生成镜像的脚本,类似:
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://xxxx.m.daocloud.io
脚本会自动帮助设置 docker 镜像的路径,可惜的是,在 CentOS 7 上生成的文件有误,运行完毕以上命令后,我们需要修改
/etc/docker/daemon.json 为如下内容:
{“registry-mirrors”: [“http://xxxx.m.daocloud.io”],”insecure-registries”: []}

3. 接下来就是把 pmm-server 镜像拉下来: # docker pull percona/pmm-server
4. 创建容器的数据卷,

docker create \
-v /opt/prometheus/data \
-v /opt/consul-data \
-v /var/lib/mysql \
-v /var/lib/grafana \
–name pmm-data \
percona/pmm-server /bin/true

5. 运行 pmm-server 容器,容器内部是 80 端口,外部我们设置为 18888 端口。后面会讲到不开防火墙的情况下怎样从外网访问这个18888端口。

docker run -d \
-p 18888:80 \
–volumes-from pmm-data \
–name pmm-server \
–restart always \
percona/pmm-server

     如果一切正常的话,wget http://localhost:18888/ 应该返回200 状态,并且取到 index.html。然而, 在我的环境里, 发现 docker 里面启动的是 IPV6 的地址,我们需要去 /etc/sysconfig/docker-network 添加 -ip 选项,因为 PMM Server 需要让外部的 PMM Client 连接上来,我们不能让它听在 127.0.0.1 上。

DOCKER_NETWORK_OPTIONS=”-ip=192.168.0.92″

重启 docker 服务。
经过以上步骤以后,一个 PMM Server 就基本搭建完成了。

6. 接下来我们找一台机器把 PMM Client(Agent)安装上去。再简单配置一下。
# wget https://www.percona.com/downloads/pmm/1.8.0/binary/redhat/7/x86_64/pmm-client-1.8.0-1.x86_64.rpm; rpm -ivh pmm-client-1.8.0-1.x86_64.rpm
# yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm
# yum install pmm-client
# pmm-admin config –server 192.168.0.92
# pmm-admin config –server 192.168.0.92:18888

OK, PMM server is alive.

PMM Server | 192.168.0.92:18888
Client Name | iot-db-01
Client Address | 192.168.0.92
# pmm-admin list
pmm-admin 1.8.0

PMM Server | 192.168.0.92:18888
Client Name | iot-db-01
Client Address | 192.168.0.92
Service Manager | linux-systemd

No services under monitoring.

然后我们添加收集数据的用户:
# pmm-admin add mysql –host localhost –user root –password P@ssw0rd
[linux:metrics] OK, now monitoring this system.
[mysql:metrics] OK, now monitoring MySQL metrics using DSN root:***@unix(/var/lib/mysql/mysql.sock)
[mysql:queries] OK, now monitoring MySQL queries from slowlog using DSN root:***@unix(/var/lib/mysql/mysql.sock)

查看prometheus状态:

http://192.168.0.92:18888/prometheus/targets

================================
但是如果在公网上配置的话,我们不希望在安全组上开那么多“洞”,接下来我们讲下通过 Apache mod_proxy 来反向代理,在浏览器通过  80 端口访问 PMM Server。
以下是 Apache Virtualhost 的配置,我们就可以通过: http://demo.yj777.cn/graph/ 访问了。

<VirtualHost *:80>
ServerName demo.yj777.cn
ServerAlias 192.168.0.92

RewriteEngine On
ProxyRequests Off
ProxyPreserveHost On

<Proxy *>
Require all granted
</Proxy>

ProxyPass “/graph/” “http://192.168.0.92:18888/graph/” connectiontimeout=5 timeout=30 keepalive=on
ProxyPassReverse “/graph/” “http://192.168.0.92:18888/graph/”
RewriteRule ^/graph$ http://%{HTTP_HOST}/graph/ [L,R=301]

ProxyPass “/qan/” “http://192.168.0.92:18888/qan/” connectiontimeout=5 timeout=30 keepalive=on
ProxyPassReverse “/qan/” “http://192.168.0.92:18888/qan/”
RewriteRule ^/qan$ http://%{HTTP_HOST}/qan/ [L,R=301]

ProxyPass “/qan-api/” “http://192.168.0.92:18888/qan-api/” connectiontimeout=5 timeout=30 keepalive=on
ProxyPassReverse “/qan-api/” “http://192.168.0.92:18888/qan-api/”
RewriteRule ^/qan-api$ http://%{HTTP_HOST}/qan-api/ [L,R=301]

ProxyPass “/orchestrator/” “http://192.168.0.92:18888/orchestrator/” connectiontimeout=5 timeout=30 keepalive=on
ProxyPassReverse “/orchestrator/” “http://192.168.0.92:18888/orchestrator/”
RewriteRule ^/orchestrator$ http://%{HTTP_HOST}/orchestrator/ [L,R=301]

ProxyPass “/v1/” “http://192.168.0.92:18888/v1/” connectiontimeout=5 timeout=30 keepalive=on
ProxyPassReverse “/v1/” “http://192.168.0.92:18888/v1/”
RewriteRule ^/v1$ http://%{HTTP_HOST}/v1/ [L,R=301]

ProxyPass “/prometheus/” “http://192.168.0.92:18888/prometheus/” connectiontimeout=5 timeout=30 keepalive=on
ProxyPassReverse “/prometheus/” “http://192.168.0.92:18888/prometheus/”
RewriteRule ^/prometheus$ http://%{HTTP_HOST}/prometheus/ [L,R=301]

ProxyPass “/consul/” “http://192.168.0.92:18888/consul/” connectiontimeout=5 timeout=30 keepalive=on
ProxyPassReverse “/consul/” “http://192.168.0.92:18888/consul/”
RewriteRule ^/consul$ http://%{HTTP_HOST}/consul/ [L,R=301]

ProxyPass “/managed/” “http://192.168.0.92:18888/managed/” connectiontimeout=5 timeout=30 keepalive=on
ProxyPassReverse “/managed/” “http://192.168.0.92:18888/managed/”
RewriteRule ^/managed$ http://%{HTTP_HOST}/managed/ [L,R=301]

</VirtualHost>

作者: 甬洁网络

--移动互联网&物联网技术提供商