Nginx+Keepalived實現Nginx高可用

    在架構設計中,可以利用NGINX的反向代理和負載均衡實現後端應用的高可用性,同時我們還需要考慮Nginx的單點故障。真正做到架構高可用性。

主要考慮以下幾點:

 1、Nginx服務因爲意外現象掛掉

 2、服務器宕機導致NGINX不可用

 目前主流的解決方案就是keepalived+nginx 實現nginx的故障轉移,同時做好監控報警。在自動故障轉移的同時能通知到相關的應用負責人檢查相關應用,排查隱患,徹底解決問題。

模擬環境:

序號環境名稱IP地址環境介紹
1訪問客戶端110.57.3.29mac  有Chrome等瀏覽器
2nginx備+keepalived備10.57.31.205反向代理 nginx高可用備
3nginx主+keepalived主10.57.31.206反向代理 nginx高可用主
4web應用服務器10.57.27.20web應用
5VIP10.57.31.230

nginx服務和web應用是已經配置好的環境,這裏就不介紹相關的配置。強烈建議關閉Firewalld和selinux服務 

1、keepalived的安裝

yum install -y  keepalived

  2、keepalived主(10.57.31.206)

[root@linuxceph2 ~]# cat  /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id nginx02  # router_id  唯一標識符
   vrrp_skip_check_adv_addr
   vrrp_stricti
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_nginx {  
	script "/etc/keepalived/nginx_check.sh"   #nginx服務檢查腳本
	interval 1
	weight -2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0  #網卡  
    virtual_router_id 52  #默認爲51  配置完發現主備切換有問題 更改爲52 發現好了  原因未知
    priority 150     #主備的優先級priority 
    advert_int 1     #檢查時間1秒
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script  {
	check_nginx
	}

    virtual_ipaddress {
        10.57.31.230/24    #vip地址 
    }
}
}

啓動服務後觀察網卡信息

image.png

3、keepalived備(10.57.31.205)

# Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id nginx01
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 52

    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.57.31.230/24
    }
}

}

啓動服務後觀察網卡信息

image.png

故障轉移

引起keepalived vip漂移的幾個常見因素

1、服務器宕機 

2、監聽網卡故障  

3、某一服務或者其他事件觸發 可以用腳本監聽服務端口 服務 進程等

測試一:將主服務器宕機

  測試步驟、

    1、ping VIP 

   2、將主keepalived服務器宕機

   3、觀察ping情況和VIP飄移情況 同時堅持web服務

   結果:

 ping出現過丟包

image.png

2 VIP轉移

image.png

VIP漂移成功,服務能正常訪問 恢復主機後 接管備機的VIP

image.png

測試二、停止主節點的網絡服務

image.png

備節點

image.png

出現腦裂情況找不到VIP


測試三  測試nginx服務不能正常工作時VIP漂移

#!/bin/bash
#檢查nginx的pid文件是否存在 不存在時 killall  keepalivedVIP漂移
NGINXPID="/usr/install/nginx/logs/nginx.pid"
if  [ ! -f  $NGINXPID  ];then
	killall keepalived
fi

配置文件已經在上面的模塊中再不描述。

測試過程中遇到的問題:

1、virtual_router_id 配置爲51 的時候漂移有問題 還是防火牆的問題,

2、漂移過程中 防火牆意外開啓 導致網絡無法訪問 

image.png

經過對比線上同事配置的環境和查閱資料,發現是一個默認參數的問題

vrrp_strict 是默認開啓  嚴格執行VRRP協議 導致主機和備機間防火牆問題。

image.png

線上可以參考的配置:

! Configuration File for keepalived

vrrp_instance ka_192_168_128_204 {
    state BACKUP
    interface eth0
    virtual_router_id 204
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 128204
    }
    virtual_ipaddress {
        192.168.128.204/24 brd 192.168.128.255 dev eth0 label eth0:1
    }

}

virtual_server 192.168.128.204 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 192.168.128.119 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.128.120 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }


    real_server 192.168.128.121 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.128.122 80 {
       	 weight 3
       	 TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章