Keepalived + Zabbix 主备架构:
1. 两台服务器:主机(192.168.1.10) 和 备机 (192.168.1.11)
2. 共享虚拟IP (VIP):192.168.1.100
3. 确保两台服务器时间同步(使用NTP)
4. 相同的操作系统环境
###各组件部署方案参考:超详细| 如何在OpenEuler系统下安装Zabbix 7.2?
在主机上(192.168.1.10):
Bash# 编辑MariaDB配置文件sudo vi /etc/my.cnf.d/server.cnf# 添加以下内容:[mysqld]server-id=1log-bin=mysql-binbinlog-format=ROWbinlog-do-db=zabbix
在备机上(192.168.1.11):
Bash# 编辑MariaDB配置文件sudo vi /etc/my.cnf.d/server.cnf# 添加以下内容:[mysqld]server-id=2relay-log=mysql-relay-binread-only=1replicate-do-db=zabbix
在主机上创建复制用户:
SQLCREATE USER 'replica'@'192.168.1.11' IDENTIFIED BY 'StrongPassword123!';GRANT REPLICATION SLAVE ON *.* TO 'replica'@'192.168.1.11';FLUSH PRIVILEGES;
在备机上配置主从复制:
SQLCHANGE MASTER TOMASTER_HOST='192.168.1.10',MASTER_USER='replica',MASTER_PASSWORD='StrongPassword123!',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=XXX; -- 从主机执行 SHOW MASTER STATUS; 获取具体值START SLAVE;
Bash# 安装依赖dnf install gcc make libevent-devel openssl-devel pcre-devel mysql-devel -y# 下载源码wget https://cdn.zabbix.com/zabbix/sources/stable/7.2/zabbix-7.2.0.tar.gztar -zxvf zabbix-7.2.0.tar.gzcd zabbix-7.2.0# 编译安装./configure \--prefix=/app/zabbix \--enable-server \--enable-agent \--with-mysqlmake -j$(nproc)make install# 创建系统用户groupadd --system zabbixuseradd --system -g zabbix -d /app/zabbix -s /sbin/nologin zabbix
方案B:RPM仓库安装
Bash# 添加Zabbix官方仓库rpm -Uvh https://repo.zabbix.com/zabbix/7.2/release/centos/9/noarch/zabbix-release-latest-7.2.el9.noarch.rpm# 安装核心组件dnf install zabbix-server-mysql zabbix-web-mysql zabbix-agent -y
步骤3:配置Zabbix
在主机上配置Zabbix数据库连接:
Bashsudo vi /etc/zabbix/zabbix_server.conf# 修改以下参数:DBHost=localhostDBName=zabbixDBUser=zabbixDBPassword=YourZabbixDBPassword
在备机上配置Zabbix数据库连接:
Bashsudo vi /etc/zabbix/zabbix_server.conf# 修改以下参数:DBHost=localhostDBName=zabbixDBUser=zabbixDBPassword=YourZabbixDBPassword# 添加以下参数禁用数据收集(备机只做热备)StartPollers=0StartPollersUnreachable=0StartPingers=0
Web前端配置(两台服务器相同):
Bashsudo vi /etc/zabbix/web/zabbix.conf.php<?php$DB['TYPE'] = 'MYSQL';$DB['SERVER'] = 'localhost';$DB['PORT'] = '0';$DB['DATABASE'] = 'zabbix';$DB['USER'] = 'zabbix';$DB['PASSWORD'] = 'YourZabbixDBPassword';$ZBX_SERVER = '192.168.1.100'; // 使用虚拟IP$ZBX_SERVER_PORT = '10051';$ZBX_SERVER_NAME = 'Zabbix HA Cluster';
在两台服务器上安装Keepalived:
Bashsudo dnf install keepalived -y
在主机上(192.168.1.10) 配置Keepalived:
Bashsudo vi /etc/keepalived/keepalived.confvrrp_script chk_zabbix {script "/usr/bin/pgrep zabbix_server"interval 2weight 50}vrrp_script chk_httpd {script "/usr/bin/pgrep httpd"interval 2weight 50}vrrp_instance VI_1 {state MASTERinterface ens33 # 使用实际网卡名virtual_router_id 51priority 200 # 主机优先级更高 authentication {auth_type PASSauth_pass kylin123} virtual_ipaddress {192.168.1.100/24 dev ens33 # VIP配置} track_script {chk_zabbixchk_httpd} notify_master "/etc/keepalived/master.sh"notify_backup "/etc/keepalived/backup.sh"notify_fault "/etc/keepalived/fault.sh"}
在备机上(192.168.1.11) 配置Keepalived:
Bashsudo vi /etc/keepalived/keepalived.confvrrp_script chk_zabbix {script "/usr/bin/pgrep zabbix_server"interval 2weight 50}vrrp_script chk_httpd {script "/usr/bin/pgrep httpd"interval 2weight 50}vrrp_instance VI_1 {state BACKUPinterface ens33virtual_router_id 51priority 100 # 备机优先级较低 authentication {auth_type PASSauth_pass kylin123} virtual_ipaddress {192.168.1.100/24 dev ens33} track_script {chk_zabbixchk_httpd} notify_master "/etc/keepalived/master.sh"notify_backup "/etc/keepalived/backup.sh"notify_fault "/etc/keepalived/fault.sh"}
创建状态切换脚本(两台服务器相同):
Bashsudo mkdir /etc/keepalived/scriptssudo vi /etc/keepalived/master.sh#!/bin/bash# 当此服务器成为MASTER时执行systemctl start zabbix-serversystemctl start httpdsystemctl start mariadbexit 0sudo vi /etc/keepalived/backup.sh#!/bin/bash# 当此服务器成为BACKUP时执行systemctl stop zabbix-serversystemctl stop httpdsystemctl stop mariadbexit 0sudo vi /etc/keepalived/fault.sh#!/bin/bash# 当发生故障时执行logger "Keepalived进入FAULT状态"exit 0# 设置脚本权限sudo chmod +x /etc/keepalived/*.sh
在两台服务器上:
Bash# 启动Keepalivedsudo systemctl enable keepalivedsudo systemctl start keepalived# 启动Zabbix服务(注意:备机上会自动停止)sudo systemctl enable zabbix-server zabbix-agent httpdsudo systemctl start zabbix-server zabbix-agent httpd
[if !supportLists]1. [endif]检查VIP状态:
Bash ip addr show ens33# 在主机上应该看到192.168.1.100
[if !supportLists]2. [endif]模拟故障转移:
Bash # 在主机上停止Keepalivedsudo systemctl stop keepalived # 在备机上检查VIP是否转移ip addr show ens33 # 检查备机上Zabbix服务是否启动systemctl status zabbix-server
[if !supportLists]3. [endif]访问Web界面:
http://192.168.1.100/zabbix
[if !supportLists]4. [endif]验证数据库复制状态:
SQL # 在备机上执行SHOW SLAVE STATUS\G # 确保Slave_IO_Running和Slave_SQL_Running都是Yes
[if !supportLists]1. [endif]VIP不漂移:
[if !supportLists]– [endif]检查防火墙是否允许VRRP协议:
[if !supportLists] [endif]
Bash sudo firewall-cmd --add-protocol=vrrp --permanentsudo firewall-cmd --reload
[if !supportLists]• [endif]验证两台服务器是否在同一个二层网络
[if !supportLists]• [endif]检查keepalived.conf中的virtual_router_id是否一致且唯一
[if !supportLists]2. [endif]数据库复制中断:
[if !supportLists]– [endif]检查主从状态:SHOW SLAVE STATUS\G
[if !supportLists]– [endif]修复复制错误:
[if !supportLists] [endif]
SQL STOP SLAVE;SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;START SLAVE;
[if !supportLists]3. [endif]Zabbix服务不自动启动/停止:
[if !supportLists]– [endif]检查脚本权限:chmod +x /etc/keepalived/*.sh
[if !supportLists]– [endif]查看Keepalived日志:journalctl -u keepalived
[if !supportLists] [endif]
[if !supportLists]1. [endif]定期备份:
Bash # 备份Zabbix配置mysqldump -u zabbix -p zabbix > zabbix_backup_$(date +%F).sql
[if !supportLists]2. [endif]监控Keepalived集群:
[if !supportLists]– [endif]在Zabbix中添加对两台服务器和VIP的监控
[if !supportLists]– [endif]设置告警规则(VIP切换、服务状态变化)
[if !supportLists] [endif]
[if !supportLists]3. [endif]升级流程:
1. 在备机上停止Keepalived2. 升级备机上的Zabbix3. 手动切换VIP到备机4. 升级原主机上的Zabbix5. 恢复主备关系
此方案确保了Zabbix服务的高可用性,当主机故障时,VIP会自动漂移到备机,同时备机上的Zabbix服务会自动启动接管工作。数据库主从复制保证了数据的实时同步。