Docker部署Zabbix监控MariaDB主从同步(Percona Monitoring Plugins for Zabbix)
一、安装Docker并部署Zabbix
建议先配置清华大学的docker-ce yum源,速度有保障:清华大学repo源
1.Zabbix Server节点配置
部署环境:
[root@server0 ~]# docker -v Docker version 18.09.1, build 4c52b90 [root@server0 ~]# cat /proc/version Linux version 3.10.0-693.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Tue Aug 22 21:09:27 UTC 2017 [root@server0 ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core)
启动并开机自运行Docker:
systemctl enable docker
systenctl start docker
部署MySQL Docker,提供Zabbix数据库服务:
~]$ docker container run --name zmysql-server \ --network zabbix \ -e MYSQL_DATABASE="zabbix" \ -e MYSQL_USER="zabbix" \ -e MYSQL_PASSWORD="123456" \ -e MYSQL_ROOT_PASSWORD="654321" \ -v /data/zabbix-mysql:/var/lib/mysql \ --rm #根据需要也可以使用--restart=always -d mysql:5.7
部署Zabbix_server和Zabbix_web:
~]$ docker container run --name zabbix-server-mysql \ --network zabbix -t \ -e DB_SERVER_HOST="172.20.0.2" \ -e MYSQL_DATABASE="zabbix" \ -e MYSQL_USER="zabbix" \ -e MYSQL_PASSWORD="123456" \ -e MYSQL_ROOT_PASSWORD="654321" \ -p 10051:10051 \ --rm -d zabbix/zabbix-server-mysql:centos-3.0-latest ~]$ docker container run --name zabbix-nginx \ --network zabbix -t \ -e DB_SERVER_HOST="172.20.0.2" \ -e MYSQL_DATABASE="zabbix" \ -e MYSQL_USER="zabbix" \ -e ZBX_SERVER_HOST="172.20.0.3" \ -e MYSQL_PASSWORD="123456" \ -e MYSQL_ROOT_PASSWORD="654321" \ -e PHP_TZ="Asia/Shanghai" \ -p 80:80 --rm \ -d zabbix/zabbix-web-nginx-mysql:centos-3.0-latest
修改时区:
docker container exec -ti zabbix-server-mysql /bin/bash cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
访问宿主机的IP就能访问zabbix了,使用容器部署服务是非常方便快捷的。部署zabbix的自运行脚本放在了我的github,可以直接运行脚本启动安装Zabbix。
2.在MariaDB主从节点上部署Zabbix-agent
在MariaDB主从节点上设置授权zabbix能访问数据的账号,登录数据库执行:
> GRANT PROCESS,SUPER,REPLICATION CLIENT ON *.* TO zabbix@127.0.0.1 IDENTIFIED BY '123456';
配置zabbix官方yum源并安装zabbix-agent,PHP、php-mysql:
[zabbix] name=Zabbix Official Repository - $basearch baseurl=http://repo.zabbix.com/zabbix/3.0/rhel/7/$basearch/ enabled=1 gpgcheck=0 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX [zabbix-non-supported] name=Zabbix Official Repository non-supported - $basearch baseurl=http://repo.zabbix.com/non-supported/rhel/7/$basearch/ enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX gpgcheck=0
yum install -y zabbix-agent zabbix-senter php php-mysql
二、安装Percona Monitoring Plugins插件监控MariaDB
Percona Monitoring Plugins是一个很强大的数据库测试插件,我们这里只用它的MariaDB For Zabbix模板。
~]# rpm -ivh https://www.percona.com/redir/downloads/percona-release/redhat/latest/percona-release-0.1-6.noarch.rpm ~]# yum install percona-zabbix-templates -y ~]# rpm -ql percona-zabbix-templates #安装监控模板 /var/lib/zabbix/percona /var/lib/zabbix/percona/scripts /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php /var/lib/zabbix/percona/templates /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf /var/lib/zabbix/percona/templates/zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml
修改模板文件配置参数(红色字体为要修改的部分):
~]# cp /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/
~]# vim /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh if [ "$ITEM" = "running-slave" ]; then # Check for running slave RES=`HOME=~zabbix /usr/bin/mysql -uzabbix -p123456 -e 'SHOW SLAVE STATUS\G' | egrep '(Slave_IO_Running|Slave_SQL_Running):' | awk -F: '{print $2}' | tr '\n' ','` if [ "$RES" = " Yes, Yes," ]; then echo 1 else echo 0 fi exit
~]# vim /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php $mysql_user = 'zabbix'; $mysql_pass = '123456';
~]# systemctl restart zabbix-agent.service
测试,若以下命令返回任意数字,则说明模板配置成功:
~]# /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gg ~]# /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh running-slave ~]# /usr/bin/php -q /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.--host localhost --items gg
登录Zabbix页面并导入模板:
将xml模板文件上传至zabbix-server,由于官方的模板文件只支持zabbix2.0,这里提供修改后的3.0模板文件→→Zabbix Percona Template。导入后重zabbix-agent,添加主机和模板,设置触发器等等功能,然后就能监控MariaDB了。Percona的模板很详细,可以满足生产中决大部分MariaDB监控需求。
关于Zabbix的基础配置可参阅我的文章---->Zabbix监控原理及架构、Zabbix3.0基础教程之一:系统监控,zabbix安装与基本配置、Zabbix3.0基础教程之二:item、trigger、action、graph配置