nginx keepalived高可用
安装nginx
yum -y install nginx
nginx 起停
systemctl start/stop nginx
安装keepalived
yum -y install keepalived
keepalived配置
双机主备配置
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL1
}
vrrp_script chk_nginx {
script "/etc/keepalived/check_nginx_alive.sh"
interval 2
weigth 2
}
vrrp_instance VI_1 {
state MASTER
interface enp132s0f1
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
12.23.166.149/24
}
track_script {
chk_nginx
}
}
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL2
}
vrrp_script chk_nginx {
script "/etc/keepalived/check_nginx_alive.sh"
interval 2
weigth 2
}
vrrp_instance VI_1 {
state BACKUP
interface enp132s0f1
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
12.23.166.149/24
}
track_script {
chk_nginx
}
}
双主热备配置
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL1
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx_alive.sh"
interval 2
weight 10
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.12.121/24 dev eth0
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.12.122/24 dev eth0
}
}
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL2
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx_alive.sh"
interval 2
weight 10
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.12.121/24 dev eth0
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.12.122/24 dev eth0
}
}
keepalived起停
systemctl start/stop/enable keepalived
check_nginx_alive.sh
#!/bin/bash
P=`ps -C nginx --no-header |wc -l`
if [ $P -eq 0 ];then
systemctl restart nginx;
sleep 3;
if [ `ps -C nginx --no-header |wc -l` -eq 0 ]; then
killall keepalived
fi
fi