Keepalived + Zabbix 主備架構(gòu):

1. 兩臺服務(wù)器:主機(jī)(192.168.1.10) 和 備機(jī) (192.168.1.11)
2. 共享虛擬IP (VIP):192.168.1.100
3. 確保兩臺服務(wù)器時間同步(使用NTP)
4. 相同的操作系統(tǒng)環(huán)境
###各組件部署方案參考:超詳細(xì)| 如何在OpenEuler系統(tǒng)下安裝Zabbix 7.2?
步驟1:數(shù)據(jù)庫主從復(fù)制 (MariaDB/MySQL)
在主機(jī)上(192.168.1.10):
Bash# 編輯MariaDB配置文件sudo vi /etc/my.cnf.d/server.cnf# 添加以下內(nèi)容:[mysqld]server-id=1log-bin=mysql-binbinlog-format=ROWbinlog-do-db=zabbix
在備機(jī)上(192.168.1.11):
Bash# 編輯MariaDB配置文件sudo vi /etc/my.cnf.d/server.cnf# 添加以下內(nèi)容:[mysqld]server-id=2relay-log=mysql-relay-binread-only=1replicate-do-db=zabbix
在主機(jī)上創(chuàng)建復(fù)制用戶:
SQLCREATE USER 'replica'@'192.168.1.11' IDENTIFIED BY 'StrongPassword123!';GRANT REPLICATION SLAVE ON *.* TO 'replica'@'192.168.1.11';FLUSH PRIVILEGES;
在備機(jī)上配置主從復(fù)制:
SQLCHANGE MASTER TOMASTER_HOST='192.168.1.10',MASTER_USER='replica',MASTER_PASSWORD='StrongPassword123!',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=XXX; -- 從主機(jī)執(zhí)行 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# 創(chuàng)建系統(tǒng)用戶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
在主機(jī)上配置Zabbix數(shù)據(jù)庫連接:
Bashsudo vi /etc/zabbix/zabbix_server.conf# 修改以下參數(shù):DBHost=localhostDBName=zabbixDBUser=zabbixDBPassword=YourZabbixDBPassword
在備機(jī)上配置Zabbix數(shù)據(jù)庫連接:
Bashsudo vi /etc/zabbix/zabbix_server.conf# 修改以下參數(shù):DBHost=localhostDBName=zabbixDBUser=zabbixDBPassword=YourZabbixDBPassword# 添加以下參數(shù)禁用數(shù)據(jù)收集(備機(jī)只做熱備)StartPollers=0StartPollersUnreachable=0StartPingers=0
Web前端配置(兩臺服務(wù)器相同):
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';
在兩臺服務(wù)器上安裝Keepalived:
Bashsudo dnf install keepalived -y
在主機(jī)上(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 ?# 使用實(shí)際網(wǎng)卡名virtual_router_id 51priority 200 ?????# 主機(jī)優(yōu)先級更高????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"}
在備機(jī)上(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 ?????# 備機(jī)優(yōu)先級較低????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"}
創(chuàng)建狀態(tài)切換腳本(兩臺服務(wù)器相同):
Bashsudo mkdir /etc/keepalived/scriptssudo vi /etc/keepalived/master.sh#!/bin/bash# 當(dāng)此服務(wù)器成為MASTER時執(zhí)行systemctl start zabbix-serversystemctl start httpdsystemctl start mariadbexit 0sudo vi /etc/keepalived/backup.sh#!/bin/bash# 當(dāng)此服務(wù)器成為BACKUP時執(zhí)行systemctl stop zabbix-serversystemctl stop httpdsystemctl stop mariadbexit 0sudo vi /etc/keepalived/fault.sh#!/bin/bash# 當(dāng)發(fā)生故障時執(zhí)行l(wèi)ogger "Keepalived進(jìn)入FAULT狀態(tài)"exit 0# 設(shè)置腳本權(quán)限sudo chmod +x /etc/keepalived/*.sh
在兩臺服務(wù)器上:
Bash# 啟動Keepalivedsudo systemctl enable keepalivedsudo systemctl start keepalived# 啟動Zabbix服務(wù)(注意:備機(jī)上會自動停止)sudo systemctl enable zabbix-server zabbix-agent httpdsudo systemctl start zabbix-server zabbix-agent httpd
[if !supportLists]1.?[endif]檢查VIP狀態(tài):
Bash ??ip addr show ens33# 在主機(jī)上應(yīng)該看到192.168.1.100
[if !supportLists]2.?[endif]模擬故障轉(zhuǎn)移:
Bash ??# 在主機(jī)上停止Keepalivedsudo systemctl stop keepalived???# 在備機(jī)上檢查VIP是否轉(zhuǎn)移ip addr show ens33???# 檢查備機(jī)上Zabbix服務(wù)是否啟動systemctl status zabbix-server
[if !supportLists]3.?[endif]訪問Web界面:
http://192.168.1.100/zabbix
[if !supportLists]4.?[endif]驗(yàn)證數(shù)據(jù)庫復(fù)制狀態(tài):
SQL ??# 在備機(jī)上執(zhí)行SHOW SLAVE STATUS\G???# 確保Slave_IO_Running和Slave_SQL_Running都是Yes
[if !supportLists]1.?[endif]VIP不漂移:
[if !supportLists]–?[endif]檢查防火墻是否允許VRRP協(xié)議:
[if !supportLists]?[endif]
Bash ????sudo firewall-cmd --add-protocol=vrrp --permanentsudo firewall-cmd --reload
[if !supportLists]??[endif]驗(yàn)證兩臺服務(wù)器是否在同一個二層網(wǎng)絡(luò)
[if !supportLists]??[endif]檢查keepalived.conf中的virtual_router_id是否一致且唯一
[if !supportLists]2.?[endif]數(shù)據(jù)庫復(fù)制中斷:
[if !supportLists]–?[endif]檢查主從狀態(tài):SHOW SLAVE STATUS\G
[if !supportLists]–?[endif]修復(fù)復(fù)制錯誤:
[if !supportLists]?[endif]
SQL ????STOP SLAVE;SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;START SLAVE;
[if !supportLists]3.?[endif]Zabbix服務(wù)不自動啟動/停止:
[if !supportLists]–?[endif]檢查腳本權(quán)限: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]監(jiān)控Keepalived集群:
[if !supportLists]–?[endif]在Zabbix中添加對兩臺服務(wù)器和VIP的監(jiān)控
[if !supportLists]–?[endif]設(shè)置告警規(guī)則(VIP切換、服務(wù)狀態(tài)變化)
[if !supportLists]?[endif]
[if !supportLists]3.?[endif]升級流程:
1. 在備機(jī)上停止Keepalived2. 升級備機(jī)上的Zabbix3. 手動切換VIP到備機(jī)4. 升級原主機(jī)上的Zabbix5. 恢復(fù)主備關(guān)系
此方案確保了Zabbix服務(wù)的高可用性,當(dāng)主機(jī)故障時,VIP會自動漂移到備機(jī),同時備機(jī)上的Zabbix服務(wù)會自動啟動接管工作。數(shù)據(jù)庫主從復(fù)制保證了數(shù)據(jù)的實(shí)時同步。