使用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:阅读材料列表。
灵感:
文档:
论文 :