本文通过 Zabbix 原生 HA 功能实现高可用(暂不讨论数据库和前端的高可用,下期会讲到),本文以 LAMP 环境演示,其他环境可以参考之前的文章。
192.168.0.228
192.168.0.39
192.168.0.200
192.168.0.125
192.168.0.103
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
systemctl stop firewalld && systemctl disable firewalld
cat <<EOF > /etc/yum.repos.d/zabbix.repo
[zabbix]
name=Zabbix Official Repository - \$basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.5/rhel/8/\$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[zabbix-non-supported]
name=Zabbix Official Repository non-supported - \$basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/non-supported/rhel/8/\$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=1
EOF
curl https://mirrors.tuna.tsinghua.edu.cn/zabbix/RPM-GPG-KEY-ZABBIX-A14FE591 \
-o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
curl https://mirrors.tuna.tsinghua.edu.cn/zabbix/RPM-GPG-KEY-ZABBIX \
-o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
curl https://mirrors.tuna.tsinghua.edu.cn/zabbix/RPM-GPG-KEY-ZABBIX-A14FE591 \
-o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
curl https://mirrors.tuna.tsinghua.edu.cn/zabbix/RPM-GPG-KEY-ZABBIX \
-o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
dnf install zabbix-web-mysql zabbix-apache-conf zabbix-agent2 -y
systemctl start httpd php-fpm
systemctl enable httpd php-fpm
理论上应该是可以访问的,否则安装出现了错误。
cat <<EOF > /etc/yum.repos.d/mariadb.repo
# MariaDB 10.6 CentOS repository list - created 2022-01-16 15:10 UTC
# https://mariadb.org/download/
[mariadb]
name = MariaDB
baseurl = https://mirrors.cloud.tencent.com/mariadb/yum/10.6/centos8-amd64
module_hotfixes=1
gpgkey=https://mirrors.cloud.tencent.com/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF
dnf -y install mariadb-server
systemctl start mariadb && systemctl enable mariadb
mariadb-secure-installation
create database zabbix character set utf8 collate utf8_bin;
create user zabbix@localhost identified by 'xiaoyu123';
grant all privileges on zabbix.* to zabbix@'%' identified by 'xiaoyu123' with grant option;
quit;
cat <<EOF > /etc/yum.repos.d/zabbix.repo
[zabbix]
name=Zabbix Official Repository - \$basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.5/rhel/8/\$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[zabbix-non-supported]
name=Zabbix Official Repository non-supported - \$basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/non-supported/rhel/8/\$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=1
EOF
curl https://mirrors.tuna.tsinghua.edu.cn/zabbix/RPM-GPG-KEY-ZABBIX-A14FE591 \
-o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
curl https://mirrors.tuna.tsinghua.edu.cn/zabbix/RPM-GPG-KEY-ZABBIX \
-o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
curl https://mirrors.tuna.tsinghua.edu.cn/zabbix/RPM-GPG-KEY-ZABBIX-A14FE591 \
-o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
curl https://mirrors.tuna.tsinghua.edu.cn/zabbix/RPM-GPG-KEY-ZABBIX \
-o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
dnf -y install zabbix-sql-scripts zabbix-agent2
zcat /usr/share/doc/zabbix-sql-scripts/mysql/server.sql.gz | mysql -uzabbix -p zabbix
cat <<EOF > /etc/yum.repos.d/zabbix.repo
[zabbix]
name=Zabbix Official Repository - \$basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.5/rhel/8/\$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[zabbix-non-supported]
name=Zabbix Official Repository non-supported - \$basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/non-supported/rhel/8/\$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=1
EOF
curl https://mirrors.tuna.tsinghua.edu.cn/zabbix/RPM-GPG-KEY-ZABBIX-A14FE591 \
-o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
curl https://mirrors.tuna.tsinghua.edu.cn/zabbix/RPM-GPG-KEY-ZABBIX \
-o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
curl https://mirrors.tuna.tsinghua.edu.cn/zabbix/RPM-GPG-KEY-ZABBIX-A14FE591 \
-o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
curl https://mirrors.tuna.tsinghua.edu.cn/zabbix/RPM-GPG-KEY-ZABBIX \
-o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
dnf -y install zabbix-server-mysql zabbix-agent2
vim /etc/zabbix/zabbix_server.conf
由于采用的组件是分布式部署,所以数据库部分需要特别设置。
翻到配置最后部分,找到 High availability cluster parameters
部分,该部分即为原生 HA 配置。
HANodeName 部分为集群节点名称,而 NodeAddress 部分一定要设置为相对应主机的 IP 地址,否则切换时会出现无法找到主机
systemctl start zabbix-server zabbix-agent2
systemctl enable zabbix-server zabbix-agent2
在主重启服务
service zabbix-server restart
此时表现不会立即切换,会等待一分钟的时间,重新拉起备机,默认的 failover_delay 为1分钟,最低为10S,
调整故障转移时长目前只能通过 zabbix_server 命令调整
zabbix_server -R ha_set_failover_delay=10s
server=127.0.0.1,10.0.0.1
server=127.0.0.1;10.0.0.1
注释高可用部分即可
zabbix_server -R
最后 整个 Zabbix 6.0 的 HA 功能配置起来还是比较简单的,但是还是有很多不足之处,例如无法提供 EIP 功能,在 agent 指定时需要指定两个IP,与之前的多写有点相似,其次无法做到配置同步,在实际测试中,如果备机的配置较低或者没有优化配置,会出现 server 挂死,但相比之前的第三方方案还是要强了很多,毕竟不依赖第三方组件,而且切换灵敏,目前官方发布了 6.0 LTS RC1 版本,基本可以预测不会有太大的调整,在后续的版本中希望持续优化 HA 功能,真正实现完整的高可用。