HA(High Available):高可用性集羣,是保證業務連續性的有效解決方案,通常有兩個或兩個以上的節點,且分爲活動節點及備用節點。nginx
keepalive是一款能夠實現高可靠的軟件,一般部署在2臺服務器上,分爲一主一備。Keepalived能夠對本機上的進程進行檢測,一旦Master檢測出某個進程出tomcat
現問題,將本身切換成Backup狀態,而後通知另一個節點切換成Master狀態。bash
下載keepalived官網:http://keepalived.org服務器
將keepalived解壓到/usr/local/src目錄下:tar -zxvf keepalived-1.2.19.tar.gz -C /usr/local/src負載均衡
進入到/usr/local/src/keepalived-1.2.19目錄:cd /usr/local/src/keepalived-1.2.19spa
開始configure(檢查安裝環境,並指定未來要安裝的路徑):./configure --prefix=/usr/local/keepalived 3d
#編譯並安裝:make && make install代理
拷貝執行文件:cp /usr/local/keepalived/sbin/keepalived /usr/sbin/日誌
將init.d文件拷貝到etc下,加入開機啓動項:cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived code
將keepalived文件拷貝到etc下:cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
建立keepalived文件夾:mkdir -p /etc/keepalived
將keepalived配置文件拷貝到etc下:cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
添加可執行權限:chmod +x /etc/init.d/keepalived
添加keepalived到開機啓動:chkconfig --add keepalived chkconfig keepalived on
機器IP |
安裝軟件 |
角色 |
虛擬ip |
描述 |
192.168.232.201 |
Nginx、keepalived |
Nginx主機 |
192.168.232.203 |
反向代理到tomcat1和tomcat2 |
192.168.232.205 |
Nginx、keepalived |
Nginx備機 |
主機掛了切換虛擬ip 192.168.232.203 |
反向代理到tomcat1和tomcat2 |
192.168.232.200 |
Tomcat |
Tomcat1 |
無 |
|
192.168.232.204 |
Tomcat |
Tomcat2 |
無 |
兩臺nginx服務器,分別安裝keepalived,配置/etc/keepalived/keepalived.conf
主從配置:
主nginx修改配置文件: /etc/keepalived/keepalived.conf
#MASTER節點
#全局配置
global_defs {
notification_email { #指定keepalived在發生切換時須要發送email到的對象,一行一個
XXX@XXX.com
}
notification_email_from Alexandre.Cassen@firewall.loc #指定發件人
smtp_server 192.168.200.1 #指定smtp服務器地址
smtp_connect_timeout 30 #指定smtp鏈接超時時間
router_id LVS_DEVEL #運行keepalived機器的一個標識
}
vrrp_instance VI_1 {
state MASTER #指定A節點爲主節點 備分機上設置爲BACKUP便可 interface eth0 #設置實例綁定的網卡 virtual_router_id 51 #VRRP組名,兩個節點的設置必須同樣,以指明各個節點屬於同一VRRP組(同一實例下virtual_router_id必須相同) priority 100 #主節點的優先級(1-254之間),備用節點必須比主節點優先級低 advert_int 1 #MASTER與BACKUP負載均衡器之間同步檢查的時間間隔,單位是秒 authentication { #設置驗證信息,兩個節點必須一致 auth_type PASS #主從服務器驗證方式 auth_pass 1111 } virtual_ipaddress { #指定虛擬IP, 兩個節點設置必須同樣 192.168.232.203/24 #若是兩個nginx的ip分別是192.168.232.201,,...205,則此處的虛擬ip跟它倆同一個網段便可,24子網掩碼(255.255.255.0),能夠多個虛擬IP,換行便可 } }
備nginx
修改備nginx下/etc/keepalived/keepalived.conf文件
配置備nginx時須要注意:須要修改state爲BACKUP , priority比MASTER低,virtual_router_id和master的值一致
#BACKUP節點
global_defs {
}
vrrp_instance VI_1 {
state BACKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.232.203/24 } }
#分別啓動兩臺機器上的keepalived:tail /var/log/message 查看日誌。
service keepalived start
用ip addr查看主從:
主機:
備機:
此時若是主機宕機則(service keepalived stop)備機會頂替主機得到虛擬ip
首先將主機的keepalived關閉:
再次查看主機:ip addr
備機:
至此,基本服務器宕機的主從切換配置完畢
訪問虛擬ip是否是還能輪詢訪問tomcat
keepalived是經過檢測keepalived進程是否存在判斷服務器是否宕機,若是keepalived進程在可是nginx進程不在了那麼keepalived是不會作主備切換,因此咱們須要寫
個腳原本監控nginx進程是否存在,若是nginx不存在就將keepalived進程殺掉。
在主nginx上須要編寫nginx進程檢測腳本(check_nginx.sh),判斷nginx進程是否存在,若是nginx不存在就將keepalived進程殺掉,check_nginx.sh內容以下:
#!/bin/bash
# 若是進程中沒有nginx則將keepalived進程kill掉
A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx進程 把值賦給變量A if [ $A -eq 0 ];then ## 若是沒有進程值得爲 零 service keepalived stop ## 則結束 keepalived 進程 fi
將check_nginx.sh拷貝至/etc/keepalived下,
注意修改/etc/keepalived/check_nginx.sh的可執行權限
修改主nginx的keepalived.conf,添加腳本定義檢測:
注意下邊紅色標識地方:
#全局配置
global_defs {
notification_email { #指定keepalived在發生切換時須要發送email到的對象,一行一個
XXX@XXX.com
}
notification_email_from miaoruntu@itcast.cn #指定發件人
#smtp_server XXX.smtp.com #指定smtp服務器地址
#smtp_connect_timeout 30 #指定smtp鏈接超時時間 router_id LVS_DEVEL #運行keepalived機器的一個標識 } vrrp_script check_nginx { script "/etc/keepalived/check_nginx.sh" ##監控腳本 interval 2 ##時間間隔,2秒 weight 2 ##權重 } vrrp_instance VI_1 { state MASTER #標示狀態爲MASTER 備份機爲BACKUP interface eth0 #設置實例綁定的網卡 virtual_router_id 51 #同一實例下virtual_router_id必須相同 priority 100 #MASTER權重要高於BACKUP 好比BACKUP爲99 advert_int 1 #MASTER與BACKUP負載均衡器之間同步檢查的時間間隔,單位是秒 authentication { #設置認證 auth_type PASS #主從服務器驗證方式 auth_pass 8888 } track_script { check_nginx #監控腳本 } virtual_ipaddress { #設置vip 192.168.101.100 #能夠多個虛擬IP,換行便可 } }
修改後重啓keepalived