使用兩臺機器 + 一個虛擬IPnginx
兩臺機器上分別啓動着本身的HTTP服務,端口8999。bash
在兩臺機器上安裝keepalivedtcp
yum install keepalived -y
測試
在master那臺,配置文件位於/etc/keepalived/keepalived.confui
寫入內容日誌
global_defs { router_id ljktest } vrrp_script chk_http_port { script "</dev/tcp/127.0.0.1/8999" interval 1 weight 2 } vrrp_instance VI_1 { state MASTER interface ens32 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.131 } track_script { chk_http_port } }
在Backup那臺寫入code
global_defs { router_id ljktest } vrrp_script chk_http_port { script "</dev/tcp/127.0.0.1/8999" interval 1 weight 2 } vrrp_instance VI_1 { state MASTER interface ens32 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.131 } track_script { chk_http_port } }
兩臺的配置,只有priority的值不同。且要配置成MASTER的那臺必須大於BACKUP那臺。router
systemctl start keepalived
blog
在Master那臺可以看到,網卡上面多了一條這樣的信息。證實生效了。訪問服務192.168.0.131:8999下的服務,會定向到Master那臺上面。進程
inet 192.168.0.131/32 scope global ens32 valid_lft forever preferred_lft forever
關閉Master這臺的keepalived,發現BACKUP這臺變成主。
重啓Master這臺的keepalived,發現Master這臺又會成爲主。
目前這樣的配置,關閉應用程序沒法進行主備切換。
不明白爲何</dev/tcp/127.0.0.1/8999
不會生效。
本身寫個腳原本檢測試試
xypt_logs_check.sh
#!/bin/bash count=`ps -ef|grep xypt_logs_producer|grep -v grep|wc -l` if [ $count -gt 0 ];then exit 0 else exit 1 fi
腳本做用就是用來查看下這個應用進程是否存在,不存在異常退出,存在則正常退出。
配置腳本部分修改爲
vrrp_script chk_http_port { script "/etc/keepalived/xypt_logs_check.sh" interval 2 weight 2 fall 3 rise 2 }
接下來咱們關閉Master這臺上的應用,VIP地址會漂移到Backup那臺。再啓動回來,VIP地址又會再漂移到Master這臺。
查看Master這臺/var/log/messages日誌,能夠看到它的遷移過程。
Aug 7 12:57:17 nn1 Keepalived_vrrp[4088]: /etc/keepalived/xypt_logs_check.sh exited with status 1 Aug 7 12:57:19 nn1 Keepalived_vrrp[4088]: /etc/keepalived/xypt_logs_check.sh exited with status 1 Aug 7 12:57:21 nn1 Keepalived_vrrp[4088]: /etc/keepalived/xypt_logs_check.sh exited with status 1 Aug 7 12:57:21 nn1 Keepalived_vrrp[4088]: VRRP_Script(chk_http_port) failed Aug 7 12:57:22 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) Changing effective priority from 102 to 100 Aug 7 12:57:23 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) Received advert with higher priority 101, ours 100 Aug 7 12:57:23 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) Entering BACKUP STATE Aug 7 12:57:23 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) removing protocol VIPs. Aug 7 12:57:49 nn1 Keepalived_vrrp[4088]: /etc/keepalived/xypt_logs_check.sh exited with status 1 Aug 7 12:57:51 nn1 Keepalived_vrrp[4088]: /etc/keepalived/xypt_logs_check.sh exited with status 1 Aug 7 12:57:55 nn1 Keepalived_vrrp[4088]: VRRP_Script(chk_http_port) succeeded Aug 7 12:57:56 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) Changing effective priority from 100 to 102 Aug 7 12:57:57 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) forcing a new MASTER election Aug 7 12:57:58 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) Transition to MASTER STATE Aug 7 12:57:59 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) Entering MASTER STATE Aug 7 12:57:59 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) setting protocol VIPs. Aug 7 12:57:59 nn1 Keepalived_vrrp[4088]: Sending gratuitous ARP on ens32 for 192.168.0.131 Aug 7 12:57:59 nn1 Keepalived_vrrp[4088]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens32 for 192.168.0.131 Aug 7 12:57:59 nn1 Keepalived_vrrp[4088]: Sending gratuitous ARP on ens32 for 192.168.0.131