Load-Balancing

通過 HAProxy 實現 RTMP 負載均衡

  • July 1, 2015

我有一個小問題要解決:

我需要找到一種方法來平衡我伺服器上傳入的 RTMP 流的頻寬負載。

我有 2 個 RTMP 伺服器(nginx-rtmp),我們稱它們為 S1 和 S2。

RTMP 伺服器正在監聽1936埠,HAProxy與 S1 位於同一台機器上以對流進行負載平衡,目前它執行良好,只是它使用了兩台伺服器上的頻寬。

在目前配置下,客戶端發送一個 RTMP 流,S1 上的 HAProxy 接收它並選擇 S2 作為接收伺服器,最後將流重定向到它。因此,對於 1Mbps 流,S1 使用 1Mbbs 接收流並使用 1Mbps 將其轉發到第二個伺服器,該伺服器也在 S2 上使用 1Mbps。

這是HAProxy的配置:

global
       log /dev/log    local0
       log /dev/log    local1 notice
       chroot /var/lib/haproxy
       #user haproxy
       #group haproxy
       daemon

defaults
       log     global
       mode    http
       option  httplog
       option  dontlognull
       contimeout 5000
       clitimeout 50000
       srvtimeout 50000
       errorfile 400 /etc/haproxy/errors/400.http
       errorfile 403 /etc/haproxy/errors/403.http
       errorfile 408 /etc/haproxy/errors/408.http
       errorfile 500 /etc/haproxy/errors/500.http
       errorfile 502 /etc/haproxy/errors/502.http
       errorfile 503 /etc/haproxy/errors/503.http
       errorfile 504 /etc/haproxy/errors/504.http

frontend ft_rtpm
bind :1935
mode tcp
maxconn 400
default_backend bk_rtmp

backend bk_rtmp
mode tcp
server media01 127.0.0.1:1936 check maxconn 200 weight 1
server media02 ip.address.of.S2:1936 check maxconn 200 weight 1

我需要找到一種方法將請求直接傳遞到負載均衡器選擇的伺服器,而不使用負載均衡器的頻寬。

我知道它被稱為 HA代理,但我希望它是可能的,所以同時我也搜尋了其他解決方案,如 DNS 循環、Anycast、DNS 的 SRV 記錄(這很好,只有在使用它們時……),但它似乎不如 HAProxy 有效(沒有權重、健康檢查、最大連接數……),因為它非常接近最終結果,因為“只有”頻寬問題。

您要查找的內容稱為“直接伺服器返回”,簡稱 DSR。

你不能用 HAProxy 做 DSR,但 LVS 或其他第 4 層負載均衡器可以做到。如果你只是在做第 4 層,那麼 HAPproxy 無論如何可能有點矯枉過正。

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