Haproxy

當兩台 HAProxy 伺服器中只有一台關閉時系統中斷。故障轉移似乎不起作用

  • July 9, 2016

首先,我對 HAProxy 堆棧的體驗只有一天之久,所以我希望我的問題是有道理的。

我有 2 個 HAProxy 虛擬機和 2 個 Apache 虛擬機(流浪機器),如下所示。

192.168.50.11 HAPROXY VM1
192.168.50.12 HAPROXY VM2
192.168.50.21 APACHE VM1
192.168.50.22 APACHE VM2

192.168.50.10 FLOATING IP - set in keepalived of both HAProxy servers above

如果我關閉其中一台 Apache 伺服器並且呼叫http://192.168.50.10系統仍然可以正常工作,那很好。但是,如果我關閉其中一台 HAProxy 伺服器,整個服務就會關閉。根據我下面的配置,你能告訴我我在這裡缺少什麼嗎?

兩台伺服器上的 HAProxy 設置

/etc/default/haproxy

ENABLED=1

/etc/haproxy/haproxy.cfg

global
   log /dev/log local0
   log 127.0.0.1 local1 notice
   user haproxy
   group haproxy
   maxconn 2000
   daemon

defaults
   log global
   mode http
   option httplog
   option dontlognull
   retries 3
   option redispatch
   timeout connect 5000
   timeout client 50000
   timeout server 50000

listen webservers 192.168.50.10:80
   balance roundrobin
   stats enable
   stats auth admin:admin
   stats uri /haproxy?stats
   option httpchk
   option forwardfor
   option http-server-close
   server webserver1 192.168.50.21:80 check
   server webserver2 192.168.50.22:80 check

兩台伺服器上的保持設置

/etc/sysctl.conf

net.ipv4.ip_nonlocal_bind=1

等/keepalived/keepalived.conf

vrrp_script chk_haproxy {
   script "killall -0 haproxy"
   #Ping every 2 seconds
   interval 2
   weight 2
}

vrrp_instance VI_1 {
   interface eth0
   state MASTER
   virtual_router_id 51
   priority 11
   virtual_ipaddress {
       192.168.50.10
   }
   track_script {
       chk_haproxy
   }
}

**注意:**僅priority取決於 VM,因此它適用priority 11192.168.50.11 HAPROXY VM1機器和機器。priority 12``192.168.50.12 HAPROXY VM2

我在閱讀下面的部落格文章後創建了這個範例。

正如我所想,keepalived 配置文件存在小錯誤。

state MASTER對於 192.168.50.11# This is the master HAProxy

state BACKUP對於 192.168.50.12# This is the failover HAProxy

priority 12對於 192.168.50.11# the higher priority goes with the master HAProxy

priority 11對於 192.168.50.12

引用自:https://serverfault.com/questions/788886