linux 下负载均衡工具lvs


检查内核是否支持lvs的ipvs模块
[root@com1 ~]# modprobe -l|grep ipvs
kernel/net/netfilter/ipvs/ip_vs.ko
kernel/net/netfilter/ipvs/ip_vs_rr.ko
kernel/net/netfilter/ipvs/ip_vs_wrr.ko
kernel/net/netfilter/ipvs/ip_vs_lc.ko
kernel/net/netfilter/ipvs/ip_vs_wlc.ko
kernel/net/netfilter/ipvs/ip_vs_lblc.ko
kernel/net/netfilter/ipvs/ip_vs_lblcr.ko
kernel/net/netfilter/ipvs/ip_vs_dh.ko
kernel/net/netfilter/ipvs/ip_vs_sh.ko
kernel/net/netfilter/ipvs/ip_vs_sed.ko
kernel/net/netfilter/ipvs/ip_vs_nq.ko
kernel/net/netfilter/ipvs/ip_vs_ftp.ko
可以看到内核默认支持ipvs模块,下来安装ipvs管理软件
[root@com1 ~]# yum install ipvsadm

The command has two basic formats for execution:
ipvsadm command [protocol] service-address [scheduling-method] [persistence options]
ipvsadm command [protocol] service-address server-address [packet-forwarding-method] [weight options]

The  first  format  manipulates a virtual service and the algorithm for assigning service requests to real servers. Optionally, a persistent timeout and network mask for the granularity of a persistent service may be specified. The second format manipulates a real server that is associated with an exist-ing  virtual  service. When specifying a real server, the packet-forwarding method and the weight of the real server, relative to other real servers for the virtual service, may be specified, otherwise defaults will be used.

three packet-forwarding methods (NAT, tunneling, and direct routing), and eight load balancing algorithms (round robin, weighted round robin, least-connection, weighted least-connection, locality-based least-con-nection, locality-based least-connection with replication, destination-hashing, and source-hashing).
  --gatewaying       -g                     gatewaying (direct routing) (default)
  --ipip               -i                      ipip encapsulation (tunneling)
  --masquerading     -m                    masquerading (NAT)
  --scheduler          -s scheduler      one of rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq,the default scheduler is wlc.
  --list                     -L|-l                  list the table
  --connection              -c                       output of current IPVS connections
  --numeric                -n                       numeric output of addresses and ports

[root@com1 ~]# ipvsadm -l        
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.2.215:ndmp rr
  -> com1:ndmp                    Local   1      1455       1         
  -> com2:ndmp                    Route   1      1605       0         
[root@com1 ~]# ipvsadm -l --rate
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port                 CPS    InPPS   OutPPS    InBPS   OutBPS
  -> RemoteAddress:Port
TCP  192.168.2.215:ndmp                  0      490        0    41618        0
  -> com1:ndmp                           0      245        0    20970        0
  -> com2:ndmp                           0      246        0    20648        0
[root@com1 ~]# ipvsadm -l --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  192.168.2.215:ndmp              28421  1971291        0  142348K        0
  -> com1:ndmp                       14210  1044997        0 77354965        0
  -> com2:ndmp                       14211   926294        0 64993846        0

[root@com1 ~]# ipvsadm -l -c -n
IPVS connection entries
pro expire state       source             virtual            destination
TCP 14:12  ESTABLISHED 117.35.159.6:13589 192.168.2.215:ndmp com2:ndmp
TCP 02:52  ESTABLISHED 117.35.159.6:9407  192.168.2.215:ndmp com1:ndmp
TCP 14:12  ESTABLISHED 117.35.159.6:13684 192.168.2.215:ndmp com1:ndmp
TCP 03:14  ESTABLISHED 117.35.159.6:7976  192.168.2.215:ndmp com1:ndmp
TCP 09:26  ESTABLISHED 117.35.159.6:23373 192.168.2.215:ndmp com2:ndmp

TCP 01:15  ESTABLISHED 117.35.159.6:7415  192.168.2.215:10000 192.168.2.101:10000
TCP 07:02  ESTABLISHED 117.35.159.6:23713 192.168.2.215:10000 192.168.2.100:10000
TCP 11:59  ESTABLISHED 117.35.159.6:16217 192.168.2.215:10000 192.168.2.101:10000
TCP 11:56  ESTABLISHED 117.35.159.6:16490 192.168.2.215:10000 192.168.2.100:10000
TCP 07:04  ESTABLISHED 117.35.159.6:2617  192.168.2.215:10000 192.168.2.100:10000
TCP 11:55  ESTABLISHED 117.35.159.6:14924 192.168.2.215:10000 192.168.2.100:10000
TCP 07:01  ESTABLISHED 117.35.159.6:25286 192.168.2.215:10000 192.168.2.101:10000

相关内容