這邊繼續探討 MariaDB Galera Cluster + HAProxy (CentOS) https://benjr.tw/95536 balance 不同模式的運作方式.
balance:
Defines the destination selection policy you want HAProxy to use in choosing which server it routes the incoming connections to.
分散存取資料 (balance) 的方式有很多種
- Round Robin (roundrobin)
Directs new connections to the next destination in a circular order list, modified by the server’s weight.
Round Robin 是標準輪詢的方式.
[root@localhost ~]# cat /etc/haproxy/haproxy.cfg listen galera 172.16.15.181:3306 balance roundrobin mode tcp option tcpka option mysql-check user haproxy server node1 172.16.15.182:3306 server node2 172.16.15.183:3306
可以清楚地看到 查詢測試 是以平均分配的方式到每一台 server 上.
[root@localhost ~]# mysql -h 172.16.15.181 -u root -p111111 -e "show variables like 'server_id'" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 182 | +---------------+-------+ [root@localhost ~]# mysql -h 172.16.15.181 -u root -p111111 -e "show variables like 'server_id'" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 183 | +---------------+-------+ [root@localhost ~]# mysql -h 172.16.15.181 -u root -p111111 -e "show variables like 'server_id'" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 182 | +---------------+-------+ [root@localhost ~]# mysql -h 172.16.15.181 -u root -p111111 -e "show variables like 'server_id'" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 183 | +---------------+-------+
- Static Round Robin (static-rr)
Directs new connections to the next destination in a circular order list, modified by the server’s weight. Unlike the standard implementation of round robin, in static round robin you cannot modify the server weight on the fly. Changing the server weight requires you to restart HAProxy.Round Robin 與 Static Round Robin 都是標準輪詢的方式,但Static Round Robin 可以讓我們自行設定哪一台 server 可以承受較大的負載.
[root@localhost ~]# cat /etc/haproxy/haproxy.cfg listen galera 172.16.15.181:3306 balance static-rr mode tcp option tcpka option mysql-check user haproxy server node1 172.16.15.182:3306 check weight 1 server node2 172.16.15.183:3306 check weight 2
剛剛設定 172.16.15.183 負載 (weight) 為 2,也就是 3 次的查詢測試,兩次的負載會落在這一台上.
[root@localhost ~]# mysql -h 172.16.15.181 -u root -p111111 -e "show variables like 'server_id'" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 182 | +---------------+-------+ [root@localhost ~]# mysql -h 172.16.15.181 -u root -p111111 -e "show variables like 'server_id'" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 183 | +---------------+-------+ [root@localhost ~]# mysql -h 172.16.15.181 -u root -p111111 -e "show variables like 'server_id'" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 183 | +---------------+-------+
- Least Connections (leastconn)
Directs new connections to the server with the smallest number of connections available, which is adjuted for the server’s weight.當有新的連線需求產生時,Least Connections 會把連線需求導向目前哪一台 server 連線數最少的.我們可以自行設定哪一台 server 可以承受較大的負載 (weight).
listen galera 172.16.15.181:3306 # balance source # balance static-rr # balance roundrobin balance leastconn mode tcp option tcpka option mysql-check user haproxy server node1 172.16.15.182:3306 check weight 1 server node2 172.16.15.183:3306 check weight 2
如果連線數沒有同時產生時,會把需求導向到不同台的 server 上.
[root@localhost ~]# mysql -h 172.16.15.181 -u root -p111111 -e "show variables like 'server_id'" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 182 | +---------------+-------+ [root@localhost ~]# mysql -h 172.16.15.181 -u root -p111111 -e "show variables like 'server_id'" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 183 | +---------------+-------+ [root@localhost ~]# mysql -h 172.16.15.181 -u root -p111111 -e "show variables like 'server_id'" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 182 | +---------------+-------+ [root@localhost ~]# mysql -h 172.16.15.181 -u root -p111111 -e "show variables like 'server_id'" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 183 | +---------------+-------+
如果連線數同時產生時,連線需求導向目前哪一台 server 連線數最少的.如果有9個連線同時發出需求時,172.16.15.183 (weight 2 ,server_id 183) 可以接受 6 個連線,雖然看起來跟 Static Round Robin 有點像,但 Least Connections 會先找出目前哪一台 server 連線數最少的,
[root@localhost ~]# ./test.sh Variable_name Value server_id 183 Variable_name Value server_id 182 Variable_name Value server_id 182 Variable_name Value server_id 183 Variable_name Value server_id 183 Variable_name Value server_id 183 Variable_name Value server_id 183 Variable_name Value server_id 182 Variable_name Value server_id 183
- First
Directs new connections to the first server with a connection slot available. They are chosen from the lowest numeric identifier to the highest. Once the server reaches its maximum connections value, HAProxy moves to the next in the list.
這個有點看不懂. - Source Tracking (source)
Divides the source IP address by the total weight of running servers. Ensures that client connections from the same source IP always reach the same server.Source 會依據 Client 來選擇負載分配,不同的 Client 會分配給不同的 Server.也可以設定 負載 (weight) .
[root@localhost ~]# cat /etc/haproxy/haproxy.cfg listen galera 172.16.15.181:3306 balance source mode tcp option tcpka option mysql-check user haproxy server node1 172.16.15.182:3306 check weight 1 server node2 172.16.15.183:3306 check weight 2
因為同一台 Client 的服務需求都會被導向到同一台的 Server 上,所以可以看到 3 次的查詢測試,都落在同一台上.
[root@localhost ~]# mysql -h 172.16.15.181 -u root -p111111 -e "show variables like 'server_id'" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 182 | +---------------+-------+ [root@localhost ~]# mysql -h 172.16.15.181 -u root -p111111 -e "show variables like 'server_id'" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 182 | +---------------+-------+ [root@localhost ~]# mysql -h 172.16.15.181 -u root -p111111 -e "show variables like 'server_id'" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 182 | +---------------+-------+
感謝大師,寫了 HAProxy 詳細的運作方式,而且附上精美的圖示範例,學習起來思緒非常清楚,不會看不懂,謝謝您無私奉獻。