一、环境说明
服务说明 | ip | vip |
---|---|---|
zabbix | 192.168.69.134 | |
keepalived主 | 192.168.69.133 | 192.168.69.100 |
keepalived从 | 192.168.69.130 | 192.168.69.100 |
首先安装lamp和zabbix详情参照安装zabbix
二、安装keepalived主
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@localhost ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@localhost ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@localhost ~]# yum -y install epel-release vim wget gcc gcc-c++
[root@localhost ~]# yum -y install keepalived
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.69.100
}
}
virtual_server 192.168.69.100 80 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.69.130 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
real_server 192.168.69.134 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@localhost scripts]# vim check_n.sh
[root@localhost scripts]# cat check_n.sh
#!/bin/bash
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -lt 1 ];then
systemctl stop keepalived
fi
[root@localhost scripts]# chmod +x check_n.sh
[root@localhost scripts]# chmod +x check_n.sh
[root@localhost scripts]# vim notify.sh
[root@localhost scripts]# chmod +x notify.sh
[root@localhost scripts]# cat notify.sh
#!/bin/bash
VIP=$2
sendmail (){
subject="${VIP}'s server keepalived state is translate"
content="`date +'%F %T'`: `hostname`'s state change to master"
echo $content | mail -s "$subject" [email protected]
}
case "$1" in
master)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -lt 1 ];then
systemctl start nginx
fi
sendmail
;;
backup)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -gt 0 ];then
systemctl stop nginx
fi
;;
*)
echo "Usage:$0 master|backup VIP"
;;
esac
[root@localhost scripts]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
global_defs {
router_id lb01
}
vrrp_script nginx_check {
script "/scripts/check_n.sh"
interval 1
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
virtual_ipaddress {
}
track_script {
nginx_check
}
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.69.100
}
track_script {
nginx_check
}
notify_master "/scripts/notify.sh master 192.168.69.100"
notify_backup "/scripts/notify.sh backup 192.168.69.100"
}
}
virtual_server 192.168.69.100 80 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
[root@localhost ~]# systemctl start keepalived
三、安装keepalived备
[root@localhost ~]# sed -i '/SELINUX=enforcing/s/enforcing/disabled/g' /etc/selinux/config
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# systemctl mask firewalld
Created symlink from /etc/systemd/system/firewalld.service to /dev/null.
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install epel-release vim wget gcc gcc-c++
[root@localhost ~]# yum -y install keepalived
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.69.100
}
}
virtual_server 192.168.69.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.69.130 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
real_server 192.168.69.134 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# vim notify.sh
[root@localhost scripts]# chmod +x notify.sh
[root@localhost scripts]# cat notify.sh
#!/bin/bash
VIP=$2
sendmail (){
subject="${VIP}'s server keepalived state is translate"
content="`date +'%F %T'`: `hostname`'s state change to master"
echo $content | mail -s "$subject" [email protected]
}
case "$1" in
master)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -lt 1 ];then
systemctl start nginx
fi
sendmail
;;
backup)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -gt 0 ];then
systemctl stop nginx
fi
;;
*)
echo "Usage:$0 master|backup VIP"
;;
esac
[root@localhost scripts]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.69.100
}
notify_master "/scripts/notify.sh master 192.168.69.100"
notify_backup "/scripts/notify.sh backup 192.168.69.100"
}
配置zabbix监控,对脑裂进行监控
//在备keepalived上写监控脚本
[root@localhost ~]# cd /scripts/
[root@ scripts]# vim check_keepalived.sh
[root@ scripts]# cat check_keepalived.sh
#!/bin/bash
while true
do
if [ ip a show ens33 |grep 192.168.69.100|wc -l
-ne 0 ]
then
echo 1
else
echo 0
fi
done
四、在zabbix客户端上配置
[root@localhost ~]# cd /scripts/
[root@ scripts]# chown zabbix.zabbix check_keepalived.sh
[root@ scripts]# ll
total 8
-rw-r--r--. 1 zabbix zabbix 125 Mar 9 22:17 check_keepalived.sh
-rwxr-xr-x. 1 root root 662 Mar 9 21:46 notify.sh
[root@2 scripts]# sed -i '/# UnsafeUserParameters=0/s/#//g' /usr/local/etc/zabbix_agentd.conf
[root@2 scripts]# sed -i '/UnsafeUserParameters=0/s/0/1/g' /usr/local/etc/zabbix_agentd.conf
[root@2 scripts]# echo "UserParameter=check_keepalived,/scripts/check_keepalived.sh" >> /usr/local/etc/zabbix_agentd.conf
[root@2 scripts]# pkill zabbix
[root@2 scripts]# zabbix_agentd