通过docker部署zabbix双机高可用监控系统

2022-03-11 09:34:54 浏览数 (1)

前言

 监控系统对于公司和运维人员来说,是了解线上业务重要的手段。所以双机互备是十分必要。下面就是实际工作中用到的zabbix双机互备搭建方案。

准备工作

首先准备需要的相关资源

版本信息

代码语言:javascript复制
- 系统版本: CentOS Linux release 7.6.1810
- 软件版本:
     - zabbix-release-5.4.6
     - mariadb-10.3.28-1
     - keepalived

服务器资源

代码语言:javascript复制
- 4核8G 500G硬盘 * 2台

IP地址

代码语言:javascript复制
- 10.10.20.1 ( server-1191) (m1)
- 10.10.20.2 ( server-1192) (m2)
- 10.10.10.97 (VIP)

项目代码下载

下载地址:

基础环境配置

系统初始化

执行脚本

代码语言:javascript复制
sh system_init_v3.sh          //系统初始化
sh system_init_python3.sh     //安装python3 
sh system_init_docker.sh      //安装docker和docker-compose

修改dns

代码语言:javascript复制
vim /etc/sysconfig/network-scripts/ifcfg-eno1
DNS1=223.5.5.5
DNS2=114.114.114.114

修改防火墙规则

代码语言:javascript复制
vim /etc/sysconfig/iptables
-A INPUT -p tcp -s  10.10.20.1   -m multiport --dport 3306,873  -j ACCEPT
-A INPUT -p tcp -s  10.10.20.2   -m multiport --dport 3306,873  -j ACCEPT
-A INPUT -p tcp -s  10.10.20.97   -m multiport --dport 3306,873  -j ACCEPT
-A INPUT -p tcp -s 172.16.238.0/24  -j ACCEPT
-A INPUT -p tcp -s 172.16.239.0/24  -j ACCEPT

//重启服务生效
systemctl reload iptables
systemctl restart docker

//验证
nc -z -v -w 10 10.10.20.2 3306
nc -z -v -w 10 10.10.20.1 3306

mysql数据库的主主配置

启动数据库(m1,m2同时执行)

代码语言:javascript复制
cd /opt/zabbix-ha/mariadb-m-m/m1/
docker-compose  up -d

启用GTID 主主复制

  • m1,m2同时配置
代码语言:javascript复制
//m1和m2同时操作
mysql -uroot -pxxxxx -h 127.0.0.1
//确认系统时间
select now();
//确认字符集
show variables like '%character%';
//创建同步账号
grant replication slave,replication client on *.* to 'slave'@'10.10.20.%' identified by "slave@123";
flush privileges;
  • m1 到m2 的同步配置
代码语言:javascript复制
//m1 执行
//查看master状态
show master status;
下图
//查看gtid_binlog
show variables like '%gtid_binlog%';

//m2执行
//设置slave_pos
set global gtid_slave_pos='0-11-7192';

//配置master
change master to  master_host='mariadb_m1',master_user='slave',master_password='slave@123',master_port=3306,master_use_gtid=slave_pos,master_connect_retry=30;

//启动
start slave;
  • m2到m1的同步配置
代码语言:javascript复制
//m1配置
set global gtid_slave_pos='0-12-7192';

//配置master
change master to  master_host='mariadb_m2',master_user='slave',master_password='slave@123',master_port=3306,master_use_gtid=slave_pos,master_connect_retry=30;

//启动
start slave;

zabbix server 双机部署

mysql建库并授权

代码语言:javascript复制
//登录
mysql -uroot -pUtemyan001db -h 127.0.0.1

//建库
create database zabbix character set utf8 collate utf8_bin;

//授权
create user 'zabbix'@'172.16.237.1' identified by 'Utemyan001zbX';
grant all privileges on zabbix.* to 'zabbix'@'172.16.237.1';

flush privileges;

更新防火墙规则

代码语言:javascript复制
vim /etc/sysconfig/iptables
-A INPUT -p tcp -s 172.16.238.0/24  -j ACCEPT
-A INPUT -p tcp -s 172.16.239.0/24  -j ACCEPT

//重启服务生效
systemctl reload iptables
systemctl restart docker

启动zabbix-server并测试

登录m1,m2

代码语言:javascript复制
cd /opt/ly-traffic/zabbix54 
docker-compose up

安装keepalived并配置

  • 安装 yum -y install keepalived mariadb
  • 配置并启动
代码语言:javascript复制
//m1上执行
 ln -sf /opt/zabbix-ha/keepalived/m1-keepalived.conf  /etc/keepalived/keepalived.conf
ln -sf /opt/zabbix-ha/keepalived/scripts/check-backup.sh   /etc/keepalived/check.sh
systemctl enable  --now  keepalived

//m2上执行
 ln -sf /opt/zabbix-ha/keepalived/m2-keepalived.conf  /etc/keepalived/keepalived.conf
ln -sf /opt/zabbix-ha/keepalived/scripts/check-backup.sh  /etc/keepalived/check.sh
systemctl enable  --now  keepalived

访问zabbix

  • zabbix可用性测试 curl -s -X POST -H 'Content-Type:application/json' -d '{ "jsonrpc": "2.0", "method": "user.login", "params": {"user": "Admin", "password": "LuoyunNOC-01" }, "id": 1 }' http://127.0.0.1/api_jsonrpc.php

0 人点赞