HAProxy 負載模式設定

情境

某天收到客戶端提出帳號登入後,只要登出後,就無法再登入了。這情況在專案驗收後一段時間都未發生,而是在客戶的網管單位進行了重設網路存取設定後才發生。

部署架構及驗證

部署架構,

  1. Web server * 2
  2. HAProxy * 2
  3. DB server * 3 (以 db cluster 方式部署)
  4. 登入採用 JWT 方式

同樣的設定及部署方式,包含程式設計、Web 及 DB 的部署,在其他二個地方都沒有發生(攤手
但基於某些因素,無法直接請客戶的網管單位協助(Orz

只是一個設定值,結果大不同

由原建置 HAProxy 的同事提出,更改 balance 參數值,讓同一來源的 client 可以持續訪問同一 web server,或許可以解決(吧?

修改 config file

1
$ vim /etc/haproxy/haproxy.cnf

找到 web server 相關的設定

1
2
3
4
5
6
listen webserver
bind 172.28.7.100:80 transparent
balance roundrobin
mode http
server web1 192.168.99.244:8080 check fall 5 inter 2000 rise 2
server web2 192.168.99.245:8080 check fall 5 inter 2000 rise 2

balance source
Of these options only the “balance source” requires any real discusion. This method will ensure that a request from the IP address 1.2.3.4 will always go to the same backend, assuming it remains alive. This allows you to sidestep any issues with cookie persistence if sessions are stored locally.

balance roundrobin 修改成 balance source,再重啓 HAProxy

1
$ sudo systemctl restart haproxy

檢查 HAProxy 是否正常運作

1
$ sudo systemctl status haproxy

二個 HAProxy 服務都進行了以上修改,就解決了!!

Reference