1. 程式人生 > 實用技巧 >Nginx+Keepalived叢集搭建

Nginx+Keepalived叢集搭建

Keepalived簡介

說到Keepalived,首先介紹一下什麼是VRRP(Virtual Router Redundancy Protocol)協議,即虛擬器路由冗餘協議,是為了解決區域網內預設閘道器單點失效的問題。

VRRP 將區域網內的一組路由器組成一個虛擬路由器組,每個路由器都有自己的區域網地址, 根據設定的優先順序最高決定那個是master路由器。然後閘道器地址賦給該主路由器, 該主路由器定時傳送VRRP報文向虛擬路由器組公佈健康狀況, 備份的路由器根據柏愛文判斷Master路由器是否工作正常,從而決定是否要接替它. VRRP說白了就是實現IP地址漂移的,是一種容錯協議。在下圖中,Router A(10.100.10.1)、Router B(10.100.10.2)和Router C(10.100.10.3) 組成一個虛擬路由器。各虛擬路由器都有自己的IP地址。區域網內的主機將虛擬路由器設定為預設閘道器。 Router A、Router B和Router C中優先順序最高的那臺路由器作為Master路由器,比如A,承擔閘道器的功能。區域網內的服務 只知道這臺主master路由器A的存在,將自己預設路由下一跳地址設定為該路由的ip地址10.100.10.1, 其餘兩臺路由器作為Backup路由器。當master路由器出故障後, backup路由器會根據優先順序重新選舉出新的master路由器承擔閘道器功能。Master路由器週期性地傳送VRRP報文, 在虛擬路由器中公佈其配置資訊(優先順序等)和工作狀況。Backup路由器通過接收到VRRP報文的情況來判斷Master路由器是否工作工常。


Keepalived是基於vrrp協議的一款高可用軟體,它是作用在主機上,而不是路由器上。Keepailived把多臺主機虛擬在一起,提供一個虛擬IP對外提供服務,它擁有一臺master伺服器和多臺backup伺服器,當主伺服器出現故障時,虛擬IP地址會自動漂移到備份伺服器,實現故障轉移的高可用可能,即雙機熱備。注意:伺服器的時間一定要一致。

Haproxy簡介

HAProxy 提供高可用性、負載均衡以及基於TCP和HTTP應用的代理,支援虛擬主機,它是開源、快速並且可靠的一種解決方案。HAProxy 特別適用於那些負載特大的 web 站點, 這些站點通常又需要會話保持或七層處理(和Nginx比較有優勢的地方)。HAProxy 執行在當前的硬體上,完全可以支援數以萬計的併發連線。並且它的執行模式使得它可以很簡單安全的整 合進您當前的架構中, 同時可以保護你的 web 伺服器不被暴露到網路上。

它在kubernetes高可用叢集中的作用如下圖,負責接收各節點發送給API Server的訊息,然後負載均衡到任一個主節點,保證了資料的一致性:

image.png

思路大致就是lb這個叢集中vip地址所在的節點負責監聽kubernetes叢集的各元件傳送給api的訊息,然後haproxy利用負載均衡實現訊息分發到各個kubernetes的master節點,keepalived則保證這個接收請求的“大門“保持敞開。

對於haproxy+keepalived各個節點需要做的準備工作在我的前一篇文章已經介紹了,請先完成準備工作,再開始進行接下來的安裝配置。

安裝Keepalived

  • lb1節點
    yum install -y keepalived
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     sysadmin@firewall.loc #接收人郵箱地址
   }
   notification_email_from Alexandre.Cassen@firewall.loc #傳送人郵箱
   smtp_server 127.0.0.1 #郵箱伺服器
   smtp_connect_timeout 30
   router_id lb1 #主機名,每個節點不同
   vrrp_mcast_group4 224.0.100.100
   #vrrp_strict  註釋,不然嚴格遵守vvrp,訪問不了vip

}

vrrp_instance VI_1 {
    state MASTER     #主伺服器
    interface ens160 #VIP 漂移到的網絡卡
    virtual_router_id 51 #多個節點必須相同
    priority 100     #優先順序,備伺服器比這個低
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass csdc456csdc
    }
    virtual_ipaddress {
        192.168.88.201/24 #vip
    }
}

  • lb2節點
    yum install -y keepalived
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     sysadmin@firewall.loc #接收人郵箱地址
   }
   notification_email_from Alexandre.Cassen@firewall.loc #傳送人郵箱
   smtp_server 127.0.0.1 #郵箱伺服器
   smtp_connect_timeout 30
   router_id lb2 #主機名,每個節點不同
   vrrp_mcast_group4 224.0.100.100
   #vrrp_strict  註釋,不然嚴格遵守vvrp,訪問不了vip
  
}

vrrp_instance VI_1 {
    state BACKUP     #備伺服器
    interface ens160 #VIP 漂移到的網絡卡
    virtual_router_id 51 #多個節點必須相同
    priority 90     #優先順序,備伺服器比這個低
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass csdc456csdc
    }
    virtual_ipaddress {
        192.168.88.201/24 #vip
    }
}

  • lb3節點
    yum install -y keepalived
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     sysadmin@firewall.loc #接收人郵箱地址
   }
   notification_email_from Alexandre.Cassen@firewall.loc #傳送人郵箱
   smtp_server 127.0.0.1 #郵箱伺服器
   smtp_connect_timeout 30
   router_id lb3 #主機名,每個節點不同
   vrrp_mcast_group4 224.0.100.100
   #vrrp_strict  註釋,不然嚴格遵守vvrp,訪問不了vip
  

}

vrrp_instance VI_1 {
    state BACKUP   #備伺服器
    interface ens160 #VIP 漂移到的網絡卡
    virtual_router_id 51 #多個節點必須相同
    priority 80     #優先順序,備伺服器比這個低
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass csdc456csdc
    }
    virtual_ipaddress {
        192.168.88.201/24 #vip
    }
}

然後在lb0、lb1、lb2節點執行下列命令啟動Keepalived
systemctl start keepalived
systemctl enable keepalived
現在你可以嘗試關閉主節點,看看vip是否漂移到其他節點去了,然後再開啟主節點,vip又重新繫結到主節點的網絡卡上。

安裝HAProxy

  • lb1、lb2、lb3節點
    yum install haproxy -y
vi /etc/haproxy/haproxy.cfg
#--------------------------------------------------------------------- 
# Example configuration for a possible web application.  See the       
# full configuration options online.                                   
#                                                                      
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt           
#                                                                      
#--------------------------------------------------------------------- 
                                                                       
#--------------------------------------------------------------------- 
# Global settings                                                      
#--------------------------------------------------------------------- 
global                                                                 
    # to have these messages end up in /var/log/haproxy.log you will   
    # need to:                                                         
    #                                                                  
    # 1) configure syslog to accept network log events.  This is done  
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in           
    #    /etc/sysconfig/syslog                                         
    #                                                                  
    # 2) configure local2 events to go to the /var/log/haproxy.log     
    #   file. A line like the following can be added to                
    #   /etc/sysconfig/syslog                                          
    #                                                                  
    #    local2.*                       /var/log/haproxy.log           
    #                                                                  
    log         127.0.0.1 local2                                       
                                                                       
    chroot      /var/lib/haproxy                                       
    pidfile     /var/run/haproxy.pid                                   
    maxconn     4000                                                   
    user        haproxy                                                
    group       haproxy                                                
    daemon                                                             
                                                                       
    # turn on stats unix socket                                        
    stats socket /var/lib/haproxy/stats                                
                                                                       
#--------------------------------------------------------------------- 
# common defaults that all the 'listen' and 'backend' sections will    
# use if not designated in their block                                 
#--------------------------------------------------------------------- 
defaults                                                               
    mode                    tcp                                        
    log                     global                                     
    option                  httplog                                    
    option                  dontlognull                                
    option http-server-close                                           
    option forwardfor       except 127.0.0.0/8                         
    option                  redispatch                                 
    retries                 3                                          
    timeout http-request    10s                                        
    timeout queue           1m                                         
    timeout connect         10s                                        
    timeout client          1m                                         
    timeout server          1m                                         
    timeout http-keep-alive 10s                                        
    timeout check           10s                                        
    maxconn                 3000                                       
                                                                       
#--------------------------------------------------------------------- 
# main frontend which proxys to the backends                           
#--------------------------------------------------------------------- 
frontend  kubernetes                                                   
    bind *:6443                                                        
    mode tcp                                                           
    default_backend             kubernetes-master                      
                                                                       
#--------------------------------------------------------------------- 
# round robin balancing between the various backends                   
#--------------------------------------------------------------------- 
backend kubernetes-master                                              
    balance     roundrobin                                             
    server  master1 192.168.88.97:6443 check maxconn 2000              
    server  master2 192.168.88.98:6443 check maxconn 2000              
    server  master3 192.168.88.99:6443 check maxconn 2000              

執行如下命令啟動:
systemctl start haproxy
systemctl enable haproxy

至此Haproxy+Keepalived搭建完畢。



參考:https://www.jianshu.com/p/7a41f0294f32