使用BCC(XDP/BPF)
usage: sbulb [-h] -vs VIRTUAL_SERVER
(-rs REAL_SERVER [REAL_SERVER ...] | -cfg CONFIG_FILE) -p PORT
[PORT ...] [-d {0,1,2,3,4}]
[-l {CRITICAL,ERROR,WARNING,INFO,DEBUG,TRACE}] [-mp MAX_PORTS]
[-mrs MAX_REALSERVERS] [-ma MAX_ASSOCIATIONS]
ifnet
positional arguments:
ifnet network interface to load balance (e.g. eth0)
optional arguments:
-h, --help show this help message and exit
-vs VIRTUAL_SERVER, --virtual_server VIRTUAL_SERVER
<Required> Virtual server address (e.g. 10.40.0.1)
-rs REAL_SERVER [REAL_SERVER ...], --real_server REAL_SERVER [REAL_SERVER ...]
<Required> Real server address(es) (e.g. 10.40.0.2 10.40.0.3)
-cfg CONFIG_FILE, --config_file CONFIG_FILE
<Required> a path to a file containing real server address(es).
File will be polled each second for modification and configuration
updated dynamically. A file content example :
[Real Servers]
10.0.0.4
10.0.0.2
10.0.0.6
-p PORT [PORT ...], --port PORT [PORT ...]
<Required> UDP port(s) to load balance
-d {0,1,2,3,4}, --debug {0,1,2,3,4}
Use to set bpf verbosity, 0 is minimal. (default: 0)
-l {CRITICAL,ERROR,WARNING,INFO,DEBUG,TRACE}, --loglevel {CRITICAL,ERROR,WARNING,INFO,DEBUG,TRACE}
Use to set logging verbosity. (default: ERROR)
-mp MAX_PORTS, --max_ports MAX_PORTS
Set the maximum number of port to load balance. (default: 16)
-mrs MAX_REALSERVERS, --max_realservers MAX_REALSERVERS
Set the maximum number of real servers. (default: 32)
-ma MAX_ASSOCIATIONS, --max_associations MAX_ASSOCIATIONS
Set the maximum number of associations. (default: 1048576)
This defined the maximum number of foreign peers supported at the same time.
例如: sudo python3 -m sbulb eth0 -vs 10.188.7.99 -rs 10.188.100.163 10.188.100.230 -p 5683 5684
該負載平衡器可以被視為4層NAT負載 - 平衡器,因為它僅修改IP地址。
用於入學流量:
clientip:port/realserverip關聯。用於出口流量:
clientip:port/realserverip關聯。我們將這種關聯保持在盡可能長的時間內是一張大的LRU地圖,這意味著只有在LRU映射滿足並必須創建新的關聯時才能刪除最古老的關聯。
所使用的算法是一種簡單的旋轉蛋白。
在集群中,一般來說,好的做法是在每個服務器實例之間共享狀態,但是有時某些狀態無法共享...
例如,一組無法共享DTLS連接的服務器,在這種情況下,您希望始終將數據包從給定的客戶端發送到同一服務器以限制握手的數量。
為此,您需要在客戶端和服務器之間建立長期的關聯,但是大多數UDP LoadBalancer都是Thougth具有Esphemere關聯。在大多數情況下,可以配置此關聯壽命,您可以設置一個較大的價值,但是在這裡借助LRU地圖,我們可以盡可能地保持關聯。
另一點是服務器啟動通信。我們希望能夠像客戶端啟動通信一樣從服務器啟動通信。表示使用相同的關聯表。
這是一個簡單的負載量,因此有一些局限性:
您需要:
SBulb支持SD_NOTIFY(3)機制,但不需要SystemD或任何SystemD庫才能運行。這允許SBULB在準備接受連接時通知SystemD。要使用此功能,您可以編寫這樣的服務:
[Unit]
Description=UDP Load Balancer
Wants=network-online.target
[Service]
Type=notify
NotifyAccess=all
Environment=PYTHONUNBUFFERED=1
ExecStart=/usr/bin/python3 -m slulb args...
[Install]
WantedBy=multi-user.target
請參閱我們的Wiki頁面。
啟動單元測試:
sudo python3 -m unittest # all tests
sudo python3 -m unittest sbulb.tests.IPv4TestCase # only 1 test case
sudo python3 -m unittest sbulb.tests.IPv4TestCase.test_lru # only 1 test
測試需要BCC V0.14(Python3-BPFCC)和SCAPY(PYTHON3-SCAPER)。
為什麼要XDP/BPF?為什麼內核社區用BPF代替iPtables?
閱讀有關XDP/BPF的信息:深入BPF:閱讀材料列表。
靈感:
文件:
論文 :