环境准备
准备三台机器
主机名字 | IP地址 | 软件环境 |
---|---|---|
zabbix-server01 | 192.168.111.70 | httpd+php+keepalived+sshpasszabbix server+agent |
zabbix-server02 | 192.168.111.71 | httpd+php+keepalived+sshpasszabbix server+agent |
zabbix-agent | 192.168.111.80 | zabbix agent+mysql |
VIP规划:192.168.111.72
使用sshpass免密钥交互,也可以做ssh相互免密钥登录
zabbix server的高可用,我试过在agent配置文件中,使用VIP,发现不能正常监控,于是才有本文。
我的做法agent中的配置文件,Server和ServerActive中分别都配置了两个ip,就是为了保证,zabbix-server01宕机了,还可以把监控数据发往zabbix-server02,由于两个zabbix-server共用一个数据库,肯定不能同时往数据库写数据,于是我想到了使用keepalived来控制zabbix server的开关,做到VIP在哪,那台机器的zabbix server就启动,另外一台关闭,而zabbix的web页面使用VIP去访问
本文把zabbix数据库独立出来,为了方便做mysql高可用集群
安装zabbix-server服务
将两台zabbix-server01和zabbix-server02机器上传两个文件"zabbix_rpm.tar.gz",“zabbix.conf.php”,然后两台机器执行如下命令安装部署zabbix服务。
tar xf zabbix_rpm.tar.gz
rpm -Uvh zabbix_rpm/*.rpm
wget http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
rpm -ivh https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.11-1.el7.x86_64.rpm
rpm -ivh zabbix-release-4.0-1.el7.noarch.rpm
yum install zabbix-server-mysql zabbix-web-mysql -y
yum install mariadb-server httpd -y
systemctl restart mariadb.service
systemctl restart zabbix-server.service
systemctl restart httpd.service
systemctl restart zabbix-agent.service
sed -i 's@# php_value date.timezone Europe/Riga@php_value date.timezone Asia/Shanghai@g' /etc/httpd/conf.d/zabbix.conf
mysql -e "create database zabbix character set utf8 collate utf8_bin;"
mysql -e "grant all privileges on zabbix.* to zabbix@localhost identified by '123456';"
cd /usr/share/doc/zabbix-server-mysql-*/
zcat create.sql.gz |mysql -uzabbix -p123456 zabbix
sed -i '115a DBPassword=123456' /etc/zabbix/zabbix_server.conf
sed -i '123a DBSocket=/var/lib/mysql/mysql.sock' /etc/zabbix/zabbix_server.conf
cd -
\mv zabbix.conf.php /etc/zabbix/web/
systemctl restart mariadb.service
systemctl restart zabbix-server.service
systemctl restart httpd.service
systemctl restart zabbix-agent.service
netstat -lntup
数据库备份操作
mysqldump -B zabbix >zabbix.sql
前提agent主机安装完成MySQL之后,在进行测试连接agent主机的mysql
mysql -uzabbix -p123456 -h 192.168.111.80
修改zabbix-server连接数据库的配置文件,注意SourceIP一定要指定到vip地址,不然创建监控主机不成功,因为找不到。
vim /etc/zabbix/zabbix_server.conf
DBHost=192.168.111.80
SourceIP=192.168.111.72
修改web连接数据库配置文件
vim /etc/zabbix/web/zabbix.conf.php
$DB['SERVER'] = '192.168.111.80';
安装keepalived和sshpass服务
在zabbix-server01和zabbix-server02上分别安装keepalived的
yum install -y keepalived sshpass
zabbix-server01配置文件如下
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL 29
}vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.111.72
}
notify_master /opt/to_master.sh
}
创建一个脚本,只允许运行一个zabbix-server服务,通过远程sshpass工具连接,停止远端服务,启动本地服务
cat /opt/to_master.sh
#!/bin/bash
sshpass -p 123456 ssh -o StrictHostKeyChecking=no root@192.168.111.71 "systemctl stop zabbix-server.service"
systemctl restart zabbix-server.service
给to_master.sh脚本加一个执行权限
chmod +x /opt/to_master.sh
zabbix-server02配置文件如下
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL 52
}vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.111.72
}
notify_master /opt/to_master.sh
}
创建一个脚本,只允许运行一个zabbix-server服务:通过远程sshpass工具连接,停止远端服务,启动本地服务
cat /opt/to_master.sh
#!/bin/bash
sshpass -p 123456 ssh -o StrictHostKeyChecking=no root@192.168.111.70 "systemctl stop zabbix-server.service"
systemctl restart zabbix-server.service
给to_master.sh脚本加一个执行权限
chmod +x /opt/to_master.sh
将两台zabbix-server01和zabbix-server02均启动keepalived.service服务并设置开机自启动
systemctl start keepalived.service
systemctl enable keepalived.service
开始测试keepalived服务功能,看vip是否切换,当前vip地址在01上
查看服务zabbix-server运行正常
访问成功,vip地址192.168.111.72/zabbix
同时zabbix-server02上的keep服务正常运行,zabbix-server服务未启动
手动停止zabbix-server01上的keepalived服务,vip已切换,zabbix-server已停止
查看zabbix-server02的server服务已经启动,还有存在vip地址,验证成功。
安装数据库zabbix-agent
让zabbix-server01和zabbix-server02两台服务指向zabbix-agent机器的数据
yum install mariadb-server httpd -y
systemctl restart mariadb.service
systemctl enable mariadb.service
scp -rp 192.168.111.70:/root/zabbix.sql .
mysql < zabbix.sql
mysql -e "grant all privileges on zabbix.* to zabbix@'192.168.111.%' identified by '123456';"
安装zabbix-agent服务
下载agent的rpm包
rpm -ivh https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.11-1.el7.x86_64.rpm
#修改配置文件指定VIP地址
vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.111.72
#启动agent服务,设置开机自启动
systemctl start zabbix-agent.service
systemctl enable zabbix-agent.service
zabbix-server界面创建监控主机
创建监控主机
配置链接模板
添加监控主机成功,并且状态正常为绿色
监控数据,我设置更新数据时间间隔为10秒钟,都是10秒一刷新数据,图中vip切换期间有30秒没有检测。