关于 Zabbix

Zabbix 是一款免费的开源网络监控软件工具,用于监控和跟踪 IT 基础设施的可用性和性能:服务器、网络设备和其他 IT 资产。

高可用性架构是任何企业部署网络的关键要求之一。
在本教程中,我们将介绍如何设置高可用性 Zabbix 服务器。
我们将创建一个高可用性主动/被动集群,它将由两个 CentOS 7/RHEL 7 服务器组成,具有浮动 IP 并使用共享数据库。
为了实现高可用性,我们将使用 Corosync 集群引擎和 Pacemaker 资源管理器。

环境:

准备工作,请确保在集群中的两个节点上禁用 Selinux

# sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/sysconfig/selinux && reboot

在两个 Zabbix Server 节点 ylplzbxmn01 和 ylplzbxmn02 上,我们需要使用以下命令安装 Zabbix 存储库:

[jack@onitroad ~]# rpm --import http://repo.zabbix.com/RPM-GPG-KEY-ZABBIX
[jack@onitroad ~]# rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm

现在使用以下命令安装 Zabbix 和必要的软件包:

[jack@onitroad ~]# yum update
[jack@onitroad ~]# yum install zabbix-server-mysql zabbix-web-mysql zabbix-agent zabbix-get -y

在数据库服务器 ylplzbxdb01 上使用以下命令安装 MariaDB 服务器包:

[jack@onitroad ~]# yum install mariadb-server -y
[jack@onitroad ~]# systemctl enable mariadb && systemctl start mariadb

不要忘记使用 mysql_secure_installtion 为 root 设置密码,看看这个教程:Securing MySQL server/Mariadb with mysql_secure_installation

首先我们需要创建zabbix数据库(zabbixdb)并创建一个zabbix用户(zabbixuser)。

[jack@onitroad ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.47-MariaDB MariaDB Server
Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE zabbixdb CHARACTER SET utf8 COLLATE utf8_bin;
Query OK, 1 row affected (0.00 sec) 
MariaDB [(none)]> GRANT ALL PRIVILEGES ON zabbixdb.* TO jack@onitroad'%' IDENTIFIED BY "Password";
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
[jack@onitroad ~]#

其中一个节点上,使用 scp 工具将 create.sql.gz 复制到数据库服务器 ylplzbxdb01 :

[jack@onitroad ~]# scp /usr/share/doc/zabbix-server-mysql-3.4.8/create.sql.gz jack@onitroad:/root/

创建 zabbix 数据库和用户后,我们需要使用以下命令导入 zabbix 初始数据库:

[jack@onitroad ~]# zcat create.sql.gz | mysql -u zabbixuser -p zabbixdb
Enter password:
[jack@onitroad ~]#

在集群中的每个节点上,我们需要通过指定数据库主机IP、Zabbix的Zabbix数据库名称、Zabbix数据库用户名和密码来编辑zabbix服务器配置文件zabbix_server.conf中的数据库配置。
我们还需要指定应该是我们集群的虚拟 IP 地址的 SourceIP。

# vi /etc/zabbix/zabbix_server.conf
[...]
SourceIP=Virtual_IP_Address_Cluster
DBHost=IP_Address_DataBase_Server
DBName=zabbixdb
DBUser=zabbixuser
DBPassword=Password

在集群中的每个节点上,我们必须配置 PHP 时区,打开文件 /etc/httpd/conf.d/zabbix.conf 并取消注释“date.timezone”行并将其更改为时区。

# vi /etc/httpd/conf.d/zabbix.conf
[...]
php_value date.timezone Europe/Rome

在集群中的每个节点上,我们需要确保集群节点可以通过名称相互通信。
如果我们有 DNS 服务器,请为两台机器添加其他条目。
否则,我们需要将节点添加到 /etc/hosts 文件中。
以下是我们集群节点的条目:

# cat >> /etc/hosts << END
> 192.168.1.160 ylplzbxmn01 ylplzbxmn01.onitroad.local
> 192.168.1.161 ylplzbxmn02 ylplzbxmn02.onitroad.local
> END

如果我们正在运行 firewalld 守护进程,请在集群中的每个节点上执行以下命令以启用必要的端口

# firewall-cmd --permanent --add-service=high-availability
success
# firewall-cmd --permanent --add-service=http
success
# firewall-cmd --permanent --add-port=10051/tcp
success
# firewall-cmd --permanent --add-port=10050/tcp
success
# firewall-cmd --reload
success

在集群中的每个节点上,安装如下高可用性包

[jack@onitroad ~]# yum install pacemaker pcs -y

为了使用 pcs 配置集群并在节点之间进行通信,我们必须在每个节点上为用户 ID hacluster 设置密码,即 pcs 管理帐户。

# passwd hacluster
Changing password for user hacluster.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

在集群中的每个节点上,执行以下命令以在系统引导时启动和启用 pcsd 服务。

# systemctl start pcsd
# systemctl enable pcsd
Created symlink from /etc/systemd/system/multi-user.target.wants/pcsd.service to /usr/lib/systemd/system/pcsd.service.

在第一个节点 ylplzbxmn01 上,执行以下命令对集群中每个节点的 pcs 用户 hacluster 进行身份验证

[jack@onitroad ~]# pcs cluster auth ylplzbxmn01 ylplzbxmn02
Username: hacluster
Password:
ylplzbxmn01: Authorized
ylplzbxmn02: Authorized

在第一个节点 ylplzbxmn01 上,执行以下命令创建名为 zabbixserver 的集群,该集群由节点 ylplzbxmn01 和 ylplzbxmn02 组成:

[jack@onitroad ~]# pcs cluster setup --name zabbixserver ylplzbxmn01 ylplzbxmn02
Destroying cluster on nodes: ylplzbxmn01, ylplzbxmn02...
ylplzbxmn01: Stopping Cluster (pacemaker)...
ylplzbxmn02: Stopping Cluster (pacemaker)...
ylplzbxmn01: Successfully destroyed cluster
ylplzbxmn02: Successfully destroyed cluster
Sending 'pacemaker_remote authkey' to 'ylplzbxmn01', 'ylplzbxmn02'
ylplzbxmn02: successful distribution of the file 'pacemaker_remote authkey'
ylplzbxmn01: successful distribution of the file 'pacemaker_remote authkey'
Sending cluster config files to the nodes...
ylplzbxmn01: Succeeded
ylplzbxmn02: Succeeded
Synchronizing pcsd certificates on nodes ylplzbxmn01, ylplzbxmn02...
ylplzbxmn01: Success
ylplzbxmn02: Success
Restarting pcsd on the nodes in order to reload the certificates...
ylplzbxmn01: Success
ylplzbxmn02: Success

要在集群的两个节点上启动集群服务,需要在第一个节点 ylplzbxmn01 上执行以下命令:

[jack@onitroad ~]# pcs cluster start --all
ylplzbxmn01: Starting Cluster...
ylplzbxmn02: Starting Cluster...

在集群中的每个节点上,运行以下命令以使 corosync 和 Pacemaker 守护进程在引导时启动:

# systemctl enable corosync
# systemctl enable pacemaker

要显示集群的当前状态,请执行以下命令:

[jack@onitroad ~]# pcs status cluster
Cluster Status:
 Stack: corosync
 Current DC: ylplzbxmn02 (version 1.1.16-12.el7_4.8-94ff4df) - partition with quorum
 Last updated: Tue Apr 17 21:20:40 2015
 Last change: Tue Apr 17 21:17:44 2015 by hacluster via crmd on ylplzbxmn02
 2 nodes configured
 0 resources configured
PCSD Status:
  ylplzbxmn01: Online
  ylplzbxmn02: Online

要仅显示集群节点的当前状态,请运行以下命令:

[jack@onitroad ~]# pcs status nodes
Pacemaker Nodes:
 Online: ylplzbxmn01 ylplzbxmn02
 Standby:
 Maintenance:
 Offline:
Pacemaker Remote Nodes:
 Online:
 Standby:
 Maintenance:
 Offline:

我们不打算使用 Stonith,让我们禁用它,在集群中的第一个节点上,运行以下命令:

[jack@onitroad ~]# pcs property set stonith-enabled=false

由于我们只有两个节点,因此没有必要启用 Quorum,在集群中的第一个节点上,使用以下命令禁用它:

[jack@onitroad ~]# pcs property set no-quorum-policy=ignore

我们的第一个资源将是一个 IPaddr2 资源,它是一个用于访问 zabbix Frontend 的浮动 IP 地址,该资源将被命名为 cluster_vip,我们将为浮动 IP 地址 192.168.1.162 分配一个子网掩码 24

[jack@onitroad ~]# pcs resource create cluster_vip ocf:heartbeat:IPaddr2 ip=192.168.1.162 cidr_netmask=24 op monitor interval=20s

现在让我们为 zabbix Server 守护进程创建一个名为 zabbix_server 的新 systemd 资源,并每 10 秒监控一次操作:

[jack@onitroad ~]# pcs resource create zabbix_server systemd:zabbix-server op monitor interval=10s

我们需要创建一个名为 httpd 的 Apache 资源,它将被监控每秒钟:

[jack@onitroad ~]# pcs resource create httpd systemd:httpd op monitor interval=10s

为了方便集群的管理,我们将创建一个名为 grp_zabbix_httpd 的组资源,其中由 zabbix_server 和 httpd 资源组成

[jack@onitroad ~]# pcs resource group add grp_zabbix_httpd zabbix_server httpd

为了保证资源运行在同一个节点上,为了实现我们将要使用的colocation约束,执行以下命令确保资源组grp_zabbix_httpd和浮动IP地址资源cluster_vip在同一个节点上启动,INFINITY分数也意味着如果cluster_vip 在任何地方都不活动,grp_zabbix_httpd 将不被允许运行。

[jack@onitroad ~]# pcs constraint colocation add grp_zabbix_httpd cluster_vip INFINITY

为了确保集群资源按顺序启动和停止,我们将使用顺序约束,我们需要确保浮动 IP 地址资源 cluster_vip 不仅运行在同一节点上,而且在资源组 grp_zabbix_httpd 之前启动,使用以下命令:

[jack@onitroad ~]# pcs constraint order cluster_vip then grp_zabbix_httpd
Adding cluster_vip grp_zabbix_httpd (kind: Mandatory) (Options: first-action=start then-action=start)

创建资源后,让我们检查集群的状态。

[jack@onitroad ~]# pcs status
Cluster name: zabbixserver
Stack: corosync
Current DC: ylplzbxmn02 (version 1.1.16-12.el7_4.8-94ff4df) - partition with quorum
Last updated: Tue Apr 17 21:30:34 2015
Last change: Tue Apr 17 21:28:15 2015 by root via cibadmin on ylplzbxmn01
2 nodes configured
3 resources configured
Online: [ ylplzbxmn01 ylplzbxmn02 ]
Full list of resources:
 cluster_vip    (ocf::heartbeat:IPaddr2):       Started ylplzbxmn01
 Resource Group: grp_zabbix_httpd
     zabbix_server      (systemd:zabbix-server):        Started ylplzbxmn01
     httpd      (systemd:httpd):        Started ylplzbxmn01
Daemon Status:
  corosync: active/enabled
  pacemaker: active/enabled
  pcsd: active/enabled

注意所有资源都运行在同一个节点ylplzbxmn01上,现在我们需要完成zabbix Frontend的安装,打开浏览器,指向我们集群的Float IP地址http://Float_IP_ADDRESS/zabbix/

– 确保满足所有软件准备工作,然后单击下一步

输入连接zabbix数据库的详细信息,点击下一步

输入 Zabbix 服务器详细信息,然后单击下一步按钮

查看预安装摘要,然后单击下一步按钮。

单击完成按钮完成安装

现在我们将被重定向到 zabbix 前端登录页面。
默认用户名为 Admin ,密码为 zabbix 。

安装完成后,我们要把生成的php文件zabbix.conf.php复制到passive节点,这里的passive节点是ylplzbxsrv02

[jack@onitroad ~]# scp /etc/zabbix/web/zabbix.conf.php jack@onitroad:/etc/zabbix/web/zabbix.conf.php

最后,让我们测试故障转移到被动节点 ylplzbxmn02 ,所有资源在第一个节点 ylplzbxmn01 中都处于活动状态,我们应该像这样将其置于备用状态,所有资源都将移动到第二个节点

[jack@onitroad ~]# pcs cluster standby ylplzbxmn01

将第一个节点置于备用状态后,检查集群状态,所有资源都应在第二个节点中运行

[jack@onitroad ~]# pcs status
Cluster name: zabbixserver
Stack: corosync
Current DC: ylplzbxsrv02 (version 1.1.16-12.el7_4.8-94ff4df) - partition with quorum
Last updated: Sun Apr 22 22:43:07 2015
Last change: Sun Apr 22 22:42:16 2015 by root via cibadmin on ylplzbxsrv01
2 nodes configured
3 resources configured
Node ylplzbxsrv01: standby
Online: [ ylplzbxsrv02 ]
Full list of resources:
 cluster_vip    (ocf::heartbeat:IPaddr2):       Started ylplzbxsrv02
 Resource Group: grp_zabbix_httpd
     zabbix_server      (systemd:zabbix-server):        Started ylplzbxsrv02
     httpd      (systemd:httpd):        Started ylplzbxsrv02
Daemon Status:
  corosync: active/enabled
  pacemaker: active/enabled
  pcsd: active/enabled
在 CentOS 7/RHEL 7 上如何使用 Pacemaker 配置高可用性的 Zabbix 服务器
日期:2020-06-02 22:19:00 来源:oir作者:oir