1. 高可靠概念
HA(High Available):高可用性集群,是保證業務連續性的有效解決方案,一般有兩個或兩個以上的節點,且分為活動節點及備用節點。
2. 高可靠軟件keepalived
keepalive是一款可以實現高可靠的軟件,通常部署在2台服務器上,分為一主一備。Keepalived可以對本機上的進程進行檢測,一旦Master檢測出某個進程出
現問題,將自己切換成Backup狀態,然后通知另外一個節點切換成Master狀態。
3. keepalived安裝
下載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.19
開始configure(檢查安裝環境,並指定將來要安裝的路徑):./configure --prefix=/usr/local/keepalived
#編譯並安裝:make && make install
4. 將keepalived添加到系統服務中
拷貝執行文件: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
將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
5. 配置keepalived虛擬IP
(1)基本服務器宕機的主從切換配置
機器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
6.基於nginx進程死掉的主從切換配置
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