chromiumer.com

keepalived(Nginx)

2023.02.22
yum -y install libnl libnl-devel libnfnetlink-devel
wget "https://www.keepalived.org/software/keepalived-2.2.2.tar.gz"
tar -xvf keepalived-2.2.2.tar.gz
cd keepalived-2.2.2
./configure --prefix=/usr/local/keepalived
make && make install

Nginx状态检查

cat <<"EOF">> /usr/local/keepalived/check_web.sh
#!/bin/bash

while true
do
if [ `ps -ef |grep nginx|grep -v grep |wc -l` -lt 2 ];then
   systemctl stop keepalived
   echo "Nginx Status is Not OOK."
   exit
else
   echo "Nginx Status is OOK."
   exit
fi
done
EOF

chmod +x /usr/local/keepalived/check_web.sh

cat <<"EOF">> /usr/lib/systemd/system/keepalived.service
[Unit]
Description=LVS and VRRP High Availability Monitor
After=network-online.target syslog.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/run/keepalived.pid
KillMode=process
EnvironmentFile=-/usr/local/keepalived/etc/sysconfig/keepalived
ExecStart=/usr/local/keepalived/sbin/keepalived  $KEEPALIVED_OPTIONS -f /usr/local/keepalived/etc/keepalived.conf
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable keepalived
systemctl status keepalived
systemctl start keepalived
systemctl stop keepalived
systemctl restart keepalived

/data/nginx/sbin/nginx -c /data/nginx/conf/nginx.conf
/data/nginx/sbin/nginx -s stop
配置(抢占式)
## keepalived 01
! Configuration File for keepalived
global_defs {
    router_id host01
}

vrrp_script check_web {
   script "/usr/local/keepalived/check_web.sh"
   interval 2
   weight 2
}

vrrp_instance NewVIns01{
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass IamPass
    }
    virtual_ipaddress {
        192.168.100.100/24 dev eth0 label eth0:1
    }
    track_script {
       check_web
    }
}


## keepalived 02
! Configuration File for keepalived
global_defs {
    router_id host02
}

vrrp_script check_web {
   script "/usr/local/keepalived/check_web.sh"
   interval 2
   weight 2
}

vrrp_instance NewVIns01{
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass IamPass
    }
    virtual_ipaddress {
        192.168.100.100/24 dev eth0 label eth0:1
    }
    track_script {
       check_web
    }
}
配置(非抢占式)
## keepalived 01
! Configuration File for keepalived
global_defs {
    router_id host01
}

vrrp_script check_web {
   script "/usr/local/keepalived/check_web.sh"
   interval 2
   weight 2
}

vrrp_instance NewVIns01{
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass IamPass
    }
    virtual_ipaddress {
        192.168.100.100/24 dev eth0 label eth0:1
    }
    track_script {
       check_web
    }
}

## keepalived 02
! Configuration File for keepalived
global_defs {
    router_id host02
}

vrrp_script check_web {
   script "/usr/local/keepalived/check_web.sh"
   interval 2
   weight 2
}

vrrp_instance NewVIns01{
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass IamPass
    }
    virtual_ipaddress {
        192.168.100.100/24 dev eth0 label eth0:1
    }
    track_script {
       check_web
    }
}