一、原理/需求前端
結合keepalived 對前端nginx實現HA;
nginx進程基於Master+Slave(worker)多進程模型,自身具備很是穩定的子進程管理功能。在Master進程分配模式下,Master進程永遠不進行業務處理,只是進行任務分發,從而達到Master進程的存活高可靠性,Slave(worker)進程全部的業務信號都由主進程發出,Slave(worker)進程全部的超時任務都會被Master停止,屬於非阻塞式任務模型。
Keepalived是Linux下面實現VRRP備份路由的高可靠性運行件。基於Keepalived設計的服務模式可以真正作到主服務器和備份服務器故障時IP瞬間無縫交接。兩者結合,能夠構架出比較穩定的軟件lb方案。nginx
二、環境說明
192.168.2.4 keepalived nginx
192.168.2.5 keepalived nginx
虛擬IP (VIP):192.168.2.6vim
三、安裝keepalived和nginx瀏覽器
yum -y install openssl-devel yum -y install libnl libnl-devel yum install -y libnfnetlink-devel [root@master keepalived-1.3.9]# ./configure --prefix=/usr/local/keepalived [root@master keepalived-1.3.9]# make && make install [root@master ~]# mkdir /etc/keepalived [root@master ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ [root@master ~]# /usr/local/keepalived/sbin/keepalived -D
四、配置MASTER keepalived
vim /etc/keepalived/keepalived.confbash
! Configuration File for keepalived global_defs { #標識本節點的名稱 router_id master } vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" #每2秒檢測一次nginx的運行狀態 interval 2 #失敗一次,將本身的優先級調整爲-20 weight -20 } vrrp_instance VI_1 { #狀態,主節點爲MASTER state MASTER #綁定VIP的網絡接口 interface ens33 #虛擬路由的ID號,兩個節點設置必須同樣 virtual_router_id 51 #節點優先級,值範圍0~254,MASTER>BACKUP priority 100 #組播信息發送時間間隔,兩個節點必須設置同樣,默認爲1秒 advert_int 1 #設置驗證信息,兩個節點必須一致 authentication { auth_type PASS auth_pass 1111 } #虛擬IP,兩個節點設置必須一致,能夠設置多個 virtual_ipaddress { 192.168.1.106 } #nginx存活狀態檢測腳本 track_script { chk_nginx } }
五、配置BACKUP keepalived
vim /etc/keepalived/keepalived.conf服務器
! Configuration File for keepalived global_defs { router_id backup } vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -20 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.106 } track_script { chk_nginx } }
Tips:
state 參數值:主的是MASTER、備用的是BACKUP
priority 參數值: MASTER > BACKUP
virtual_router_id: 參數值要同樣網絡
調用的腳本nginx_check.sh測試
#!/bin/bash A=`ps -C nginx -no-header |wc -l` if [ $A -eq 1 ];then pkill keepalived fi
兩臺測試機4\5均啓動keepalived和nginx
service keepalived restart
service keepalived nginx
驗證nginx啓動正常:
訪問 master:http://192.168.2.4:80/
訪問 backup: http://192.168.2.5:80/
查看keepalived的日誌信息:
4\5均打開日誌信息方便查看keepalived動態:
tail -f /var/log/messages
瀏覽器打開虛擬ip訪問:http://192.168.2.6:80/ ,此時顯示IP爲192.168.2.4ui
kill 192.168.2.4(master) 的keepalived進程
killall keepalivedspa
4的日誌信息以下:
Jun 11 18:03:10 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.2.6 Jun 11 18:03:15 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.2.6 Jun 11 19:30:44 localhost Keepalived: Terminating on signal Jun 11 19:30:44 localhost Keepalived: Stopping Keepalived v1.2.2 (06/10,2014) Jun 11 19:30:44 localhost Keepalived_vrrp: Terminating VRRP child process on signal Jun 11 19:30:44 localhost Keepalived_healthcheckers: Terminating Healthchecker child process on signal
5的日誌信息以下:
Jun 11 19:30:50 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs. Jun 11 19:30:50 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.2.6 Jun 11 19:30:50 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.2.6 added Jun 11 19:30:55 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.2.6
刷新http://192.168.2.6:80/ , 此時顯示IP爲192.168.2.5。 再次啓動192.168.2.4的keepalived進程,192.168.2.4會自動接管成爲master,192.168.2.5自動轉爲backup,從測試結果看,備機能成功接管,已經實現了熱備。