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



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


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 localhost:8080

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

balance 80 web1:8080 web2


balance -b 2001:DB8::1 80


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

balance -f 80 web1 web2 ! web3


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

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

balance -f 80 web1 web2 %





Balance Official Website: