轻量级网络流量转发工具 - Balance

Balance是一款具有负载均衡和故障转移功能的TCP代理软件。支持IPV6地址的监听和转发,支持rr轮询和ip hash。

某些功能可以替代iptables,比如讲来自本地的80端口转发到8080上:

iptables是这样写的:


iptables -t nat -A PREROUTING -d 192.168.1.1 -p tcp --dport 80 -j DNAT --to 192.168.1.1:8080


balance只需要这样:


balance 80 localhost:8080


先来看一下官方文档:


        Definitions: A possible destination consisting of a host address and a port  is  called  a

       "channel".   A  channel  is  member of a "channel group". Channels are numbered in a group

       starting with 0.  Groups are numbered starting with 0, which is the initial default group.


       Balance  accepts connections on the given port and forwards them to the supplied channels.

       At least one channel (in the default group) must be specified.  If there are two  or  more

       channels specified in a group balance performs a simple round-robin load balancing between

       the channels.


       Balance allows the definition of further channel groups. The connection  scheme  works  as

       follows:  balance  tries  first  to establish a connection to a channel in the first group

       (0), performing the standard round-robin load balancing scheme.  If  no  channel  in  this

       group is available, balance proceeds with the next higher channel group. Groups are simply

       separated with a "!" at the command line at startup and can  be  controlled  interactively

       with the "group" command.


       A  "%"  instead  of  a  "!" as a group separator declares the previous group to be of type

       "hash".  This means that instead of a round-robin algorithm, a hash distribution based  on

       the client ip address is used to determine the destination channel. This allows connecting

       one client always to the same server (e.g. balancing http sessions to a single server).


       Hosts may be specified either by hostname or by IP address. Ports may be specified  either

       by  name (as listed in /etc/services) or numerically.  If no port is specified in a desti-

       nation, the destination port defaults to the source port that balance controls.


       Balance allows the specification of the maximum number of connections  per  channel.  This

       parameter can be optionally added after the port specification separated by a colon (":").

       If a maximum number of connections is specified a channel will only be used for this maxi-

       mum  number  of simultaneous connections. A maxc value of 0 denotes an unlimited number of

       connections. This is the initial default value of a channel.


       The maximum number of groups and channels balance can handle is specified at compile  time

       and is initially 16 channels in 16 groups.


以上内容比较简单,就不翻译了。


配合balance的帮助文档,可以实现一下功能:


一,将本地的80端口转发到8080:


balance 80 localhost:8080


二,将本地的80端口转发到web1 8080端口,web2 80端口做rr load balance:


balance 80 web1:8080 web2


三,将本地的ipv6的地址的80端口转发到ipv4的80端口


balance -b 2001:DB8::1 80 10.1.1.1


下面介绍一下Balance的高级用法:

四,web1 , web2 做轮训,只有当web1,2 都出现故障后在转发到web3,!的作用就是做channel groups分割,如果之前的channel出现故障,就转向下一个channel groups 。


balance -f 80 web1 web2 ! web3

五,Balance还可以对转发到的server做连接数限制,当web1连接数打到256个的时候转移到web2,当web2的连接数打到128的时候转移到web3


balance -f 80 web1::256 ! web2::128 ! web3


六,如果想做session 保持的话 balance也能做到,要用到%, 这样就做到了同一个用户的请求都达到后端的同一台服务器。


balance -f 80 web1 web2 %



效果如图:


轻量级网络流量转发工具 - Balance_第1张图片


此工具仅作参考,或者适用于内网环境,想要实现更高级功能,还是参考LVS。



参考连接:

https://www.linux.com/news/taking-load-load-balancing-balance

Balance Official Website: https://www.inlab.de/balance.html


欢迎补充!

你可能感兴趣的:(LVS,iptables,balance)