Nginx+Keepalive高可用
说明 | ip | 系统 |
---|---|---|
Nginx主 | 192.168.100.11 | CentOS7 |
Nginx备 | 192.168.100.12 | CentOS7 |
VIP | 192.168.100.20 | CentOS7 |
主备分别安装nginx
和keepalived
yum install -y keepalived
yum install -y pcre-devel zlib zlib-devel psmisc
在192.168.100.11主机上修改/etc/keepalived/keepalived.conf
[先备份文件]
vrrp_script check_nginx_alive {
script "/opt/check_nginx_alive.sh"
interval 2
weight -10
}
global_defs {
router_id LV001
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.254
}
track_script {
check_nginx_alive
}
notify_master "/opt/notify.sh master"
notify_backup "/opt/notify.sh backup"
notify_fault "/opt/notify.sh fault"
}
virtual_server 192.168.100.254 80 {
delay_loop 6
lb_algo rr
lb_kind DR
#persistence_timeout 50
protocol TCP
real_server 192.168.100.11 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.100.12 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
在192.168.100.12备机上修改
router_id LV002 # 不能和MASTER相同
state BACKUP
priority 90 #低于MASTER
check_nginx_alive.sh
#!/bin/sh
A=`ps -C nginx --no-header | wc -l`
if [ $A -eq 0 ]
then
echo 'nginx server is died'
killall keepalived
fi
notify.sh
#!/bin/bash
curl -X POST -H 'Content-Type: application/json' --data "{\"text\":\"`hostname` is $1\"}" https://chat.zy-itech.com/hooks/aBxKATjMaMAu9XM2v/Seh48FQayeZXs5GDQeWtwDsrYH4K2c9MzWJxMwm6xCriW26c
启动服务
systemctl restart network # 重启网络
nginx # 启动nginx[设置环境变量]
systemctl start keepalived