Load-Balancing
如何計算 HAProxy 循環權重
假設我有兩個 Redis 數據庫,希望將 70% 的請求分配給 DB1,將另外 30% 的請求分配給 DB2。如何計算重量參數?
global maxconn 20000 log 127.0.0.1 local0 user haproxy chroot /usr/share/haproxy pidfile /run/haproxy.pid daemon defaults REDIS mode tcp timeout connect 4s timeout server 30s timeout client 30s listen stats bind :9000 # Listen on localhost:9000 mode http stats enable # Enable stats page stats hide-version # Hide HAProxy version stats realm Haproxy\ Statistics # Title text for popup window stats uri /haproxy_stats # Stats URI stats auth admin:123 # Authentication credentials frontend ft_redis bind 127.0.0.1:5000 name redis default_backend bk_redis backend bk_redis balance roundrobin option tcp-check tcp-check connect tcp-check send PING\r\n tcp-check expect string +PONG tcp-check send info\ replication\r\n tcp-check expect string role:master tcp-check send QUIT\r\n tcp-check expect string +OK server redis_6379 localhost:7000 check inter 1s weight 179 server redis_6380 localhost:7001 check inter 1s weight 77
weight
每個伺服器的有效範圍為 1 到 256,但您不必使用 256 作為計算的基礎。每台伺服器的權重是該伺服器聲明的權重與所有聲明的權重之和的比率,因此對於 2 台伺服器,您可以使用值 30 和 70,分佈將是您所期望的:30 ÷ (30 + 70 ) = 0.3 和 70 ÷ (30 + 70) = 0.7。“權重更大”的伺服器按比例接收更多請求。您還可以使用 3 和 7 或 33 和 77 或 1 - 256 範圍內的組合。
您使用的值 77 和 179 應該會給出相似的結果,因為 77 ÷ (77 + 179) ≈ 0.3 和 179 ÷ (77 + 179) ≈ 0.7…只是更難進行心算。保持您的配置使所有權重加起來總和為 100 是一種更人性化的解決方案。