某IDC客戶要使用linode云主機(jī)部署zabbix監(jiān)控業(yè)務(wù),為了保證監(jiān)控服務(wù)的可靠性提出高可用的需求,本文就來介紹zabbix_server的高可用部署過程。
角色 | ip | 描述 |
zabbix-ha-1 | 192.168.176.190 | 私網(wǎng)互聯(lián)IP |
zabbix-ha-2 | 192.168.176.215 | 私網(wǎng)互聯(lián)IP |
VIP | 公網(wǎng)IP | zabbix_server業(yè)務(wù)地址 |
linode網(wǎng)絡(luò)配置
在同一數(shù)據(jù)中心linode允許云主機(jī)公網(wǎng)ip共享以支持高可用服務(wù)的部署,同時(shí)支持新增私網(wǎng)ip用于互聯(lián),需要注意的是一定要關(guān)閉linode的自動(dòng)網(wǎng)絡(luò)配置。

linode支持網(wǎng)絡(luò)自動(dòng)配置,在某些情況下非常方便,但在需要IP漂移的需求下,則一定要關(guān)閉,否則無法手動(dòng)配置業(yè)務(wù)IP導(dǎo)致配置無法進(jìn)行。

keepalive配置
在此之前,請(qǐng)保證zabbix_server已經(jīng)安裝完成,同時(shí)確保NTP服務(wù)正常啟用,時(shí)間一定要同步準(zhǔn)確!
yum -y install keepalived
systemctl enable keepalived
systemctl start keepalived
systemctl start firewalld.service
systemctl enable firewalld.service
firewall-cmd --add-protocol=vrrp --permanent
firewall-cmd --reload
下面是網(wǎng)卡的配置文件,僅供參考:)
# Generated by Linode Network Helper
# Fri Jul 9 04:45:18 2021 UTC
#
# This file is automatically generated on each boot with your Linode's
# current network configuration. If you need to modify this file, please
# first disable the 'Auto-configure Networking' setting within your Linode's
# configuration profile:
# - https://cloud.linode.com/linodes/595057/advanced
#
# For more information on Network Helper:
# - https://www.linode.com/docs/platform/network-helper
#
# A backup of the previous config is at /etc/sysconfig/network-scripts/.ifcfg-eth0.linode-last
# A backup of the original config is at /etc/sysconfig/network-scripts/.ifcfg-eth0.linode-orig
#
# /etc/sysconfig/network-scripts/ifcfg-eth0
# For full descriptions of what these switches do,
# and what the interface's defaults are, see
# /usr/share/doc/initscripts-*/sysconfig.txt
DEVICE="eth0"
NAME="eth0"
ONBOOT="yes"
# "bootp" and "dhcp" are for dhcp, anything else
# is for a static configuration. "none" is given
# by sysconfig.txt so we're using it.
BOOTPROTO="none"
# Use hardware-based IPv6 addresses, no privacy extensions.
IPV6INIT="yes"
IPV6_ADDR_GEN_MODE="eui64"
IPV6_PRIVACY="no"
# Since we want a static configuration, we're specifying DNS
# addresses in this file for NetworkManager. "No" here tells
# NM to use them when BOOTPROTO!=dhcp.
# If NM is disabled the value will be yes
PEERDNS="no"
DOMAIN=members.linode.com
# resolvconf doesn't recognize more than 3 nameservers.
DNS1=173.255.241.5
DNS2=173.230.145.5
DNS3=74.207.242.5
# Sysconfig.txt says that PREFIX takes precedence over
# NETMASK when both are present. Since both aren't needed,
# we'll go with PREFIX since it seems to be preferred.
# IP assignment for eth0
IPADDR0=192.168.176.190
PREFIX0=17
[root@zabbix-ha-1 ~]#
愉快的開始做keepalived的配置,先創(chuàng)建2個(gè)bash腳本,用于檢測業(yè)務(wù)和啟停zabbix_server服務(wù)。
vim /etc/keepalived/script/zabbix.sh
#!/bin/bash
case $1 in
start)
/etc/init.d/zabbix_server start
;;
stop)
/etc/init.d/zabbix_server stop
;;
esac
vim /etc/keepalived/script/check.sh
#!/bin/bash
zabbix_server=`ps -C zabbix_server --no-header | wc -l`
mysqld=`ps -C mysqld --no-header | wc -l`
case $1 in
zabbix_server)
if [ $zabbix_server -gt 0 ];then
exit 0
else
exit 1
fi
;;
mysqld)
if [ $mysqld -gt 0 ];then
exit 0
else
exit 1
fi
;;
esac
下面就是master服務(wù)器的keepalived的配置文件:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id zabbix-ha-1 #router_id 機(jī)器標(biāo)識(shí)
script_user root #腳本執(zhí)行用戶
enable_script_security #如果腳本路徑的任一部分對(duì)于非root用戶來說,都具有可寫權(quán)限,則不會(huì)以root身份運(yùn)行腳本
}
vrrp_script chk_zabbix {
script "/etc/keepalived/script/check.sh zabbix_server" #腳本路徑
interval 3 #每3秒檢測一次服務(wù)的運(yùn)行狀態(tài)
weight 30 #優(yōu)先級(jí)變化幅度 優(yōu)先級(jí)+30
fall 2 #嘗試兩次都成功才成功
rise 2 #嘗試兩次都失敗才失敗
}
vrrp_script chk_mysql {
script "/etc/keepalived/script/check.sh mysqld"
interval 3
weight 20
fall 2
rise 2
}
vrrp_instance VI_1 { #vrrp實(shí)例定義部分
state MASTER #設(shè)置lvs的狀態(tài),MASTER和BACKUP兩種,必須大寫
interface eth0 #設(shè)置對(duì)外服務(wù)的接口
virtual_router_id 100 #設(shè)置虛擬路由標(biāo)示,這個(gè)標(biāo)示是一個(gè)數(shù)字,同一個(gè)vrrp實(shí)例使用唯一標(biāo)示
priority 100 #定義優(yōu)先級(jí),數(shù)字越大優(yōu)先級(jí)越高,在一個(gè)vrrp——instance下,master的優(yōu)先級(jí)必須大于backup
advert_int 1 #設(shè)定master與backup負(fù)載均衡器之間同步檢查的時(shí)間間隔,單位是秒
preempt delay 10 #搶占時(shí)間10秒,默認(rèn)300秒
unicast_src_ip 192.168.176.190 #VRRP單播源地址,默認(rèn)組播
unicast_peer { #VRRP單播目的地址,默認(rèn)組播
192.168.176.215
}
authentication { #設(shè)置驗(yàn)證類型和密碼
auth_type PASS
auth_pass 1111
}
track_interface {
eth0 #VIP漂移端口
}
virtual_ipaddress {
66.175.218.192/24 dev eth0 #VIP地址
}
virtual_routes {
default via 66.175.218.1 #VIP下放默認(rèn)網(wǎng)關(guān)
}
track_script {
chk_zabbix
}
track_script {
chk_mysql
}
notify_master "/etc/keepalived/script/zabbix.sh start" #當(dāng)當(dāng)前節(jié)點(diǎn)角色為MASTER執(zhí)行的腳本
notify_backup "/etc/keepalived/script/zabbix.sh stop" #當(dāng)當(dāng)前節(jié)點(diǎn)角色為BACKUP執(zhí)行的腳本
}
接下來就是backup節(jié)點(diǎn)的keepalived配置文件:
! Configuration File for keepalived
global_defs {
router_id zabbix-ha-2
script_user root
enable_script_security
}
vrrp_script chk_zabbix {
script "/etc/keepalived/script/check.sh zabbix_server"
interval 3
weight 5
fall 2
rise 2
}
vrrp_script chk_mysql {
script "/etc/keepalived/script/check.sh mysqld"
interval 3
weight 45
fall 2
rise 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 100
priority 90
advert_int 1
preempt delay 10
unicast_src_ip 192.168.176.215
unicast_peer {
192.168.176.190
}
authentication {
auth_type PASS
auth_pass 1111
}
track_interface {
eth0
}
virtual_ipaddress {
66.175.218.192/24 dev eth0
}
virtual_routes {
default via 66.175.218.1
}
track_script {
chk_zabbix
}
track_script {
chk_mysql
}
notify_master "/etc/keepalived/script/zabbix.sh start"
notify_backup "/etc/keepalived/script/zabbix.sh stop"
}
最后設(shè)置keepalived自啟動(dòng),同時(shí)firewall-cmd允許vrrp和其他必須協(xié)議通信:
yum -y install keepalived
systemctl start keepalived
systemctl start firewalld.service
systemctl enable firewalld.service
firewall-cmd --add-protocol=vrrp --permanent
firewall-cmd --add-service=https --permanent
firewall-cmd --add-service=http --permanent
firewall-cmd --reload
#MASTER ZABBIX_SERVER設(shè)置為自啟動(dòng)
#keepalived默認(rèn)日志位置在/var/log/messages
MYSQL的主主同步
需要注意的是,為了避免zabbix_server雙線操作數(shù)據(jù)庫導(dǎo)致沖突,zabbix_server的配置文件中數(shù)據(jù)庫主機(jī)需要填寫成VIP地址。
開之前別忘記放開mysql的3306端口!
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.176.215/32" port protocol="tcp" port="3306" accept'
firewall-cmd --reload
zabbix-ha-1的my.cnf配置
[root@zabbix-ha-1 ~]# cat /etc/my.cnf
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
datadir = /usr/local/mysql/var
skip-external-locking
key_buffer_size = 32M
max_allowed_packet = 1M
table_open_cache = 128
sort_buffer_size = 768K
net_buffer_length = 8K
read_buffer_size = 768K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 16
query_cache_size = 16M
tmp_table_size = 32M
performance_schema_max_table_instances = 1000
explicit_defaults_for_timestamp = true
#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=mysql-bin #開啟bin日志
binlog_format=mixed #設(shè)置binlog格式為mixed
server-id = 1 #保證每個(gè)服務(wù)器不一樣
expire_logs_days = 10
early-plugin-load = ""
auto-increment-increment = 2 #步進(jìn)值auto_imcrement。一般有n臺(tái)主MySQL就填n
auto-increment-offset = 1 #起始值。一般填第n臺(tái)主MySQL。此時(shí)為第一臺(tái)主MySQL
sync_binlog = 1 #事務(wù)特性每1次事務(wù)提交
binlog-do-db = zabbix_server #同步的據(jù)庫名
binlog-ignore-db = mysql #不同步數(shù)據(jù)庫名
binlog-ignore-db = sys
binlog-ignore-db = performance_schema
binlog-ignore-db = information_schema
replicate-do-db = zabbix_server #需要復(fù)制的數(shù)據(jù)庫
replicate-ignore-db=mysql #不需要復(fù)制的數(shù)據(jù)庫
replicate-ignore-db=sys
replicate-ignore-db=performance_schema
replicate-ignore-db=information_schema
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_data_home_dir = /usr/local/mysql/var
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/var
innodb_buffer_pool_size = 128M
innodb_log_file_size = 32M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 768K
read_buffer_size = 768K
write_buffer_size = 2M
[mysqlhotcopy]
interactive-timeout
接下來是zabbix-ha-2的my.cnf配置
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
datadir = /usr/local/mysql/var
skip-external-locking
key_buffer_size = 32M
max_allowed_packet = 1M
table_open_cache = 128
sort_buffer_size = 768K
net_buffer_length = 8K
read_buffer_size = 768K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 16
query_cache_size = 16M
tmp_table_size = 32M
performance_schema_max_table_instances = 1000
explicit_defaults_for_timestamp = true
#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=mysql-bin
binlog_format=mixed
server-id = 2
expire_logs_days = 10
early-plugin-load = ""
auto-increment-increment = 2
auto-increment-offset = 2
sync_binlog = 1
binlog-do-db = zabbix_server
binlog-ignore-db = mysql
binlog-ignore-db = sys
binlog-ignore-db = performance_schema
binlog-ignore-db = information_schema
replicate-do-db = zabbix_server
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=performance_schema
replicate-ignore-db=information_schema
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_data_home_dir = /usr/local/mysql/var
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/var
innodb_buffer_pool_size = 128M
innodb_log_file_size = 32M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 768K
read_buffer_size = 768K
write_buffer_size = 2M
[mysqlhotcopy]
interactive-timeout
新建備份用戶,需要特別注意的是,在兩個(gè)數(shù)據(jù)庫分別導(dǎo)入完zabbix_server的sql語句后,不要啟動(dòng)zabbix_server服務(wù),避免主鍵沖突無法完成同步!
#兩臺(tái)是訪問主機(jī)允許是相互的
mysql> GRANT REPLICATION SLAVE ON *.* TO 'dbsync'@'192.168.197.190' IDENTIFIED BY 'zabbix';
mysql> FLUSH PRIVILEGES;
mysql> GRANT REPLICATION SLAVE ON *.* TO 'dbsync'@'192.168.197.215' IDENTIFIED BY 'zabbix';
mysql> FLUSH PRIVILEGES;
查看二進(jìn)制日志名稱和位置

開始構(gòu)建zabbix-ha-1到zabbix-ha-2的主從同步,在zabbix-ha-2上執(zhí)行:
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.170.190',
-> MASTER_USER='dbsync',
-> MASTER_PASSWORD='zabbix',
-> MASTER_LOG_FILE='mysql-bin.000014',
-> MASTER_LOG_POS=3928140;
Query OK, 0 rows affected, 2 warnings (0.38 sec)
mysql> START SLAVE;
mysql> SHOW SLAVE STATUS\G

再做一次zabbix-server02到zabbix-server01的主從同步即完成了主主同步,以下操作都在zabbix-ha-1上執(zhí)行:
CHANGE MASTER TO
MASTER_HOST='192.168.176.215',
MASTER_USER='dbsync',
MASTER_PASSWORD='zabbix',
MASTER_LOG_FILE='mysql-bin.000015',
MASTER_LOG_POS=43819972;
START SLAVE;
SHOW SLAVE STATUS\G
然后再在zabbix-ha-1上看mysql的同步情況,至此mysql的主主同步配置完成。
配置文件的同步
由于zabbix-server兩臺(tái)主機(jī)配置文件需實(shí)時(shí)同步,所以通過定時(shí)任務(wù)方式不可行,采用inotify工具對(duì)同步數(shù)據(jù)目錄信息的監(jiān)控,結(jié)合rsync完成對(duì)數(shù)據(jù)信息的實(shí)時(shí)同步。
詳細(xì)可以參考:https://blog.51cto.com/u_3241766/2137303
參考鏈接
https://blog.51cto.com/u_3241766/2137303
https://www.cnblogs.com/l-hh/p/12530882.html
https://blog.csdn.net/m0_37477061/article/details/83687018
https://blog.csdn.net/weixin_42758707/article/details/98762998
https://www.cnblogs.com/struggle-1216/p/12098241.html
https://www.cnblogs.com/bugutian/p/13613673.html
https://www.cnblogs.com/rexcheny/p/10778567.html
https://www.cnblogs.com/wswind/p/13792585.html