環境準備:
兩臺安裝了keepalived的服務器
ip:192.168.134.170;192.168.134.172
1、安裝http服務
yum install httpd -y
2、寫一個測試頁面
[root@localhost ~]# echo "`hostname -I`,web1 test page. " > /var/www/html/inde
[root@localhost ~]# systemctl start httpd
3、修改keepalived配置文件(/etc/keepalived/keepalived.conf)
主節點
! Configuration File for keepalivedglobal_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.200.1smtp_connect_timeout 30router_id LVS_DEVEL1
}vrrp_instance VI_1 {state MASTER# nopreemptinterface ens33virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.134.100}
}
備節點:修改優先級和state即可
4、結果
但是此時如果出現了web服務器故障,沒有辦法進行自動切換。
5、寫檢查腳本
#!/bin/bashwhile true
dohttp_pid_num=`ps -C httpd --no-header | wc -l`if [ $http_pid_num -eq 0 ]thensystemctl start httpdsleep 2http_pid_num=`ps -C httpd --no-header | wc -l`if [ $http_pid_num -eq 0 ]thensystemctl stop keepalivedfifi
done
6、添加運行(放到后臺)==>執行完此步驟后重新啟動,再次測試,便可以進行故障切換。
nohup ./check_httpd.sh > nohup.out 2>&1 &