本文基于wlnmp一键安装包提供的wlnmp源,演示Zabbix7.0原生高可用性(HA)部署方法,文中所有安装过程全部都使用yum/dnf方式部署。这种部署方式的好处就是便于后期版本平滑升级,杜绝一次性部署后难以升级的痛点,适用于生产环境。
之前吴昊写过Zabbix7.0基于wlnmp源单机部署的文章(见https://blog.whsir.com/post-7953.html),原生高可用与单机版,区别并不是很大,主要在于节点IP地址的配置。
原生高可用默认并没有VIP对前端访问地址进行漂移,zabbix-agent或zabbix-agent2需要同时指向多个zabbix-server地址,当其中一个zabbix-server发生故障后,自动切换到备用的zabbix-server。
参考Zabbix官方文档:https://www.zabbix.com/documentation/7.0/zh/manual/concepts/server/ha
环境部署架构图如下
系统:Anolis OS 8.9(理论适用于RockyLinux 8.x、AlmaLinux 8.x、OpenCloudOS 8.x)
数据库:MySQL 8.4
php:8.3
注:从Zabbix7.0开始,php版本最低要求为php8.0
10.10.10.10服务器部署MySQL
1、添加wlnmp源
1 |
curl -fsSL "https://sh.wlnmp.com/wlnmp.sh" | bash |
2、配置zabbix7.0源
官方源和阿里源任选其一
官方源
1 |
rpm -Uvh https://repo.zabbix.com/zabbix/7.0/rhel/8/x86_64/zabbix-release-latest-7.0.el8.noarch.rpm |
阿里云源
1 |
vi /etc/yum.repos.d/zabbix.repo |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
[zabbix] name=Zabbix Official Repository - $basearch baseurl=https://mirrors.aliyun.com/zabbix/zabbix/7.0/rhel/8/$basearch/ enabled=1 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-B5333005 [zabbix-non-supported] name=Zabbix Official Repository (non-supported) - $basearch baseurl=https://mirrors.aliyun.com/zabbix/zabbix/7.0/rhel/8/$basearch/ enabled=1 gpgkey=https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX gpgcheck=1 [zabbix-unstable] name=Zabbix Official Repository (unstable) - $basearch baseurl=https://mirrors.aliyun.com/zabbix/zabbix/7.0/rhel/8/$basearch/ enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-A14FE591 [zabbix-sources] name=Zabbix Official Repository (source code) - $basearch baseurl=https://mirrors.aliyun.com/zabbix/zabbix/7.0/rhel/8/SRPMS enabled=0 gpgkey=https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-B5333005 gpgcheck=1 [zabbix-tools] name=Zabbix Official Repository (tools) - $basearch baseurl=https://mirrors.aliyun.com/zabbix/zabbix-tools/rhel/8/$basearch/ enabled=1 gpgkey=https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-TOOLS gpgcheck=1 |
3、安装数据库脚本
1 |
dnf install zabbix-sql-scripts -y |
4、安装MySQL8.4
1 |
dnf install wmysql84 -y |
5、启动MySQL并创建库
1 |
/etc/init.d/mysql start |
1 2 3 4 5 6 |
mysql -uroot -p mysql> create database zabbix character set utf8mb4 collate utf8mb4_bin; mysql> create user 'zabbix'@'%' identified by 'whsir'; mysql> grant all privileges on zabbix.* to 'zabbix'@'%'; mysql> set global log_bin_trust_function_creators = 1; mysql> exit |
6、导入zabbix数据库
1 |
zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | mysql --default-character-set=utf8mb4 -uzabbix -p zabbix |
我这里密码为whsir
注意:set global log_bin_trust_function_creators = 1;仅在数据导入前临时设置。数据导入完成后,默认log_bin_trust_function_creators为0即可。
7、优化MySQL并重启数据库
如果没有特殊需求,仅填写CPU、内存生成MySQL 8.4配置文件即可
/etc/init.d/mysql restart
至此10.10.10.10 MySQL数据库服务器部署完成。
10.10.10.11和10.10.10.12服务器分别执行以下内容
1、添加epel源
1 |
dnf install epel-release -y |
替换epel源为国内源
1 2 3 4 5 |
sed -e 's|^metalink=|#metalink=|g' \ -e 's|^#baseurl=https\?://download.fedoraproject.org/pub/epel/|baseurl=https://mirrors.jcut.edu.cn/epel/|g' \ -e 's|^#baseurl=https\?://download.example/pub/epel/|baseurl=https://mirrors.jcut.edu.cn/epel/|g' \ -i.bak \ /etc/yum.repos.d/epel.repo |
2、添加wlnmp源
1 |
curl -fsSL "https://sh.wlnmp.com/wlnmp.sh" | bash |
3、配置zabbix7.0源
官方源和阿里源任选其一
官方源
1 |
rpm -Uvh https://repo.zabbix.com/zabbix/7.0/rhel/8/x86_64/zabbix-release-latest-7.0.el8.noarch.rpm |
阿里云源
1 |
vi /etc/yum.repos.d/zabbix.repo |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
[zabbix] name=Zabbix Official Repository - $basearch baseurl=https://mirrors.aliyun.com/zabbix/zabbix/7.0/rhel/8/$basearch/ enabled=1 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-B5333005 [zabbix-non-supported] name=Zabbix Official Repository (non-supported) - $basearch baseurl=https://mirrors.aliyun.com/zabbix/zabbix/7.0/rhel/8/$basearch/ enabled=1 gpgkey=https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX gpgcheck=1 [zabbix-unstable] name=Zabbix Official Repository (unstable) - $basearch baseurl=https://mirrors.aliyun.com/zabbix/zabbix/7.0/rhel/8/$basearch/ enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-A14FE591 [zabbix-sources] name=Zabbix Official Repository (source code) - $basearch baseurl=https://mirrors.aliyun.com/zabbix/zabbix/7.0/rhel/8/SRPMS enabled=0 gpgkey=https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-B5333005 gpgcheck=1 [zabbix-tools] name=Zabbix Official Repository (tools) - $basearch baseurl=https://mirrors.aliyun.com/zabbix/zabbix-tools/rhel/8/$basearch/ enabled=1 gpgkey=https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-TOOLS gpgcheck=1 |
4、安装nginx、php、zabbix-server、zabbix-web
1 |
dnf install zabbix-server-mysql zabbix-web-mysql wnginx wphp83 -y |
1 |
dnf install wphp83-ldap -y |
5、配置zabbix-server
10.10.10.11配置:
1 2 3 4 |
sed -i s/"# DBPassword="/DBPassword=whsir/ /etc/zabbix/zabbix_server.conf sed -i s/"# DBHost=localhost"/DBHost=10.10.10.10/ /etc/zabbix/zabbix_server.conf sed -i s/"# HANodeName="/HANodeName=10.10.10.11/ /etc/zabbix/zabbix_server.conf sed -i s/"# NodeAddress=localhost:10051"/NodeAddress=10.10.10.11:10051/ /etc/zabbix/zabbix_server.conf |
10.10.10.12配置:
1 2 3 4 |
sed -i s/"# DBPassword="/DBPassword=whsir/ /etc/zabbix/zabbix_server.conf sed -i s/"# DBHost=localhost"/DBHost=10.10.10.10/ /etc/zabbix/zabbix_server.conf sed -i s/"# HANodeName="/HANodeName=10.10.10.12/ /etc/zabbix/zabbix_server.conf sed -i s/"# NodeAddress=localhost:10051"/NodeAddress=10.10.10.12:10051/ /etc/zabbix/zabbix_server.conf |
注意:HANodeName集群中每个zabbix server节点名称必须要有且唯一。NodeAddress参数必须为每个节点指定。
6、启动zabbix-server并设置自启动
我这里10.10.10.11是主,所以先启动10.10.10.11,确保10.10.10.11启动成功并监听10051端口,再启动10.10.10.12,10.10.10.12启动成功后,zabbix-server进程运行,但是并不会监听10051端口。
1 2 |
systemctl enable zabbix-server systemctl start zabbix-server |
注意:备机zabbix_server的10051端口并不会启动,但是进程会在,先配置并启动的那个主机就是主。
7、HA状态查询
在10.10.10.11主节点上执行以下命令
1 |
zabbix_server -R ha_status |
1 2 3 4 5 |
Failover delay: 60 seconds Cluster status: # ID Name Address Status Last Access 1. cm4w1rg4q0001wu54t3byduiv 10.10.10.11 10.10.10.11:10051 active 3s 2. cm4w1s7k300013l542jyh8bpk 10.10.10.12 10.10.10.12:10051 standby 3s |
8、设置目录权限
1 2 |
chown -R www.www /usr/share/zabbix/ chown -R www.www /etc/zabbix/ |
9、配置php并启动
1 2 3 |
sed -i s/"max_input_time = 60"/"max_input_time = 300"/ /usr/local/php/etc/php.ini sed -i s/"max_execution_time = 30"/"max_execution_time = 300"/ /usr/local/php/etc/php.ini sed -i s/"post_max_size = 8M"/"post_max_size = 16M"/ /usr/local/php/etc/php.ini |
1 |
/etc/init.d/php-fpm83 restart |
10、配置nginx并启动
1 2 |
sed -i s#"root html;"#"root /usr/share/zabbix/;"# /usr/local/nginx/conf/vhost/demo.conf sed -i s/"#include enable-php83.conf;"/"include enable-php83.conf;"/ /usr/local/nginx/conf/vhost/demo.conf |
域名或IP,结合实际情况配置server_name
1 |
/etc/init.d/nginx restart |
11、安装zabbix-agent或zabbix-agent2、zabbix-get(可选)
1 |
dnf install zabbix-agent2 zabbix-get -y |
Zabbix agent或者Zabbix agent 2必须将HA集群所有节点列入配置文件。
Agent和Agent 2对比参见官方文档:https://www.zabbix.com/documentation/7.0/zh/manual/appendix/agent_comparison
12、配置zabbix-agent或zabbix-agent2(可选)
我这里使用zabbix-agent2,所以以zabbix-agent2为例
1 |
vi /etc/zabbix/zabbix_agentd2.conf |
如果只需要被动方式的agent2,那么只需要配置Server字段即可,使用逗号分隔
1 |
Server=10.10.10.11,10.10.10.12 |
如果只需要主动方式的agent2,那么只需要配置ServerActive字段即可,使用逗号分隔
1 |
ServerActive=10.10.10.11,10.10.10.12 |
启动zabbix-agent2服务
1 2 |
systemctl enable zabbix-agent2 systemctl start zabbix-agent2 |
注意:被动方式和主动方式可以任选其一,也可以同时配置,建议配置一种即可,避免增加服务器压力。
13、Zabbix仪表盘安装
访问http://10.10.10.11或设置好的域名地址,进行仪表盘安装流程
①修改语言为中文
②确保前置检查必要条件没有问题,然后下一步
③这里配置数据库主机及密码,取消TLS加密选项
④时区改为上海
⑤没问题,直接下一步即可
⑥如果此处提示无法创建配置文件,表示/etc/zabbix/权限没有设置www,按照本文顺序操作,不会出现这类问题,点击完成即可
⑦安装完成,默认登录帐号为Admin,密码zabbix
在仪表盘→报表→系统信息中可看到高可用集群已启用,10.10.10.12待机状态
10.10.10.11安装完成后,再进行10.10.10.12安装,安装方式与10.10.10.11安装方式相同,此处不再重复说明。
14、配置中文字体
Zabbix7.0如何前期版本一样,在安装完成后图形字体都是方块显示。
①配置中文字体
下载中文字体,我这里使用的是阿里巴巴普惠体2.0,当然你也可以从Windows上找个中文字体上传上来,可参考https://blog.whsir.com/post-5965.html
1 |
curl -o /usr/share/zabbix/assets/fonts/AlibabaPuHuiTi-2-65-Medium.ttf https://down.whsir.com/downloads/AlibabaPuHuiTi-2-65-Medium.ttf |
②刷下字体权限
1 |
chown www.www /usr/share/zabbix/assets/fonts/AlibabaPuHuiTi-2-65-Medium.ttf |
③修改zabbix配置文件
1 |
vi /usr/share/zabbix/include/defines.inc.php |
找到以下两处内容
1 2 |
define('ZBX_GRAPH_FONT_NAME', 'graphfont'); define('ZBX_FONT_NAME', 'graphfont'); |
改成
1 2 |
define('ZBX_GRAPH_FONT_NAME', 'AlibabaPuHuiTi-2-65-Medium'); define('ZBX_FONT_NAME', 'AlibabaPuHuiTi-2-65-Medium'); |
保存退出,然后直接刷新页面就好了
15、仪表盘zabbix-server主机配置
默认10.10.10.11和10.10.10.12配置完成后,打开仪表盘会看到agent警告信息,这是因为默认agent监听的是127.0.0.1地址,所以我们这里需要修改默认的zabbix-server监听。
数据采集→主机中,点击Zabbix server,修改Agent地址为10.10.10.11,修改完成后点击更新
克隆zabbix-server监控项,数据采集→主机→Zabbix server中点击克隆
克隆后修改主机名称及Agent地址为10.10.10.12,这样10.10.10.11、10.10.10.12同时配置好了agent监控,当然你也可以结合实际需求,自行更改监控的指标,不使用默认的。
16、故障转移
如果主节点异常,Zabbix将自动故障转移到备用节点,所有节点默认每5秒更新一次状态。因此
①如果主节点关闭并成功报告其状态为“stopped”,另一个节点将在5秒内接管。
②如果主节点关闭/变得不可用,而无法更新其状态,备用节点将等待故障转移延迟 + 5秒来接管。
故障转移延迟支持的范围在10秒到15分钟之间(默认为1分钟;支持时间后缀例如10s、1m)。 要修改故障切换延迟,可以执行以下命令。
1 |
zabbix_server -R ha_set_failover_delay=5m |
17、负载均衡和VIP(可选)
虽然Zabbix7.0支持原生的高可用了,但是前端访问地址还是固定的,当10.10.10.11故障后,前端地址就需要手动改成10.10.10.12,因为IP不存在漂移功能。
为解决前端访问地址固定的问题,可以考虑配合nginx负载均衡、nginx主备或keepalived VIP高可用虚地址方式,使前台访问地址固定。
①nginx负载均衡示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
upstream backend { server 10.10.10.11 max_fails=3 fail_timeout=10s; server 10.10.10.12 max_fails=3 fail_timeout=10s; } server { listen 80; server_name 10.10.10.9; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } |
②keepalived配置VIP
参考:https://blog.whsir.com/post-6554.html
原文链接:Zabbix7.0基于wlnmp源原生HA部署,转载请注明来源!