以ipvsadm v1.27版本为例说明ipvsadm命令使用方式,对应内核ipvs版本v1.2.1
# ipvsadm -h
ipvsadm v1.27 2008/5/15 (compiled with popt and IPVS v1.2.1)
Usage:
  ipvsadm -A|E -t|u|f service-address [-s scheduler] [-p [timeout]] [-M netmask] [--pe persistence_engine] [-b sched-flags] 
  ipvsadm -D -t|u|f service-address
  ipvsadm -C
  ipvsadm -R
  ipvsadm -S [-n]
  ipvsadm -a|e -t|u|f service-address -r server-address [options]
  ipvsadm -d -t|u|f service-address -r server-address
  ipvsadm -L|l [options]
  ipvsadm -Z [-t|u|f service-address]
  ipvsadm --set tcp tcpfin udp
  ipvsadm --start-daemon state [--mcast-interface interface] [--syncid sid]
  ipvsadm --stop-daemon state
  ipvsadm -h
Commands:
Either long or short options are allowed.
  --add-service     -A        add virtual service with options
  --edit-service    -E        edit virtual service with options
  --delete-service  -D        delete virtual service
  --clear           -C        clear the whole table
  --restore         -R        restore rules from stdin
  --save            -S        save rules to stdout
  --add-server      -a        add real server with options
  --edit-server     -e        edit real server with options
  --delete-server   -d        delete real server
  --list            -L|-l     list the table
  --zero            -Z        zero counters in a service or all services
  --set tcp tcpfin udp        set connection timeout values
  --start-daemon              start connection sync daemon
  --stop-daemon               stop connection sync daemon
  --help            -h        display this help message
Options:
  --tcp-service  -t service-address   service-address is host[:port]
  --udp-service  -u service-address   service-address is host[:port]
  --fwmark-service  -f fwmark         fwmark is an integer greater than zero
  --ipv6         -6                   fwmark entry uses IPv6
  --scheduler    -s scheduler         one of rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq,
                                      the default scheduler is wlc.
  --pe            engine              alternate persistence engine may be sip,
                                      not set by default.
  --persistent   -p [timeout]         persistent service
  --netmask      -M netmask           persistent granularity mask
  --real-server  -r server-address    server-address is host (and port)
  --gatewaying   -g                   gatewaying (direct routing) (default)
  --ipip         -i                   ipip encapsulation (tunneling)
  --masquerading -m                   masquerading (NAT)
  --weight       -w weight            capacity of real server
  --u-threshold  -x uthreshold        upper threshold of connections
  --l-threshold  -y lthreshold        lower threshold of connections
  --mcast-interface interface         multicast interface for connection sync
  --syncid sid                        syncid for connection sync (default=255)
  --connection   -c                   output of current IPVS connections
  --timeout                           output of timeout (tcp tcpfin udp)
  --daemon                            output of daemon information
  --stats                             output of statistics information
  --rate                              output of rate information
  --exact                             expand numbers (display exact values)
  --thresholds                        output of thresholds information
  --persistent-conn                   output of persistent connection info
  --nosort                            disable sorting output of service/server entries
  --sort                              does nothing, for backwards compatibility
  --ops          -o                   one-packet scheduling
  --numeric      -n                   numeric output of addresses and ports
  --sched-flags  -b flags             scheduler flags (comma-separated)
ipvsadm命令分为命令(Commands)和选项(Options)两部分。命令有两种形式,一个是全写,一个是缩写,功能一样,实际使用中一般缩写使用更频繁。
常用命令说明如下:
| 常用命令 | 说明 | 
|---|---|
| -A --add-service | 添加一条虚拟服务记录 | 
| -E --edit-service | 编辑一条虚拟服务记录 | 
| -D --delete-service | 删除一条虚拟服务记录 | 
| -C --clear | 清空虚拟服务ipvs规则 | 
| -R --restore | 恢复虚拟服务规则 | 
| -S --save | 保存虚拟服务规则 | 
| -a --add-server | 指定一条内核虚拟服务器记录添加一条real server记录 | 
| -e --edit-server | 指定一条内核虚拟服务器记录编辑一条real server记录 | 
| -d --delete-server | 指定一条内核虚拟服务器记录删除一条real server记录 | 
| -L -l --list | 显示内核虚拟服务器列表 | 
| -Z --zero | 虚拟服务表计数器清零(--stats) | 
| --set tcp tcpfin udp | 设置连接超时时间,分别为TCP、TCPFIN(TCP关闭连接状态)、UDP连接超时时间。 | 
| --start-daemon | 启动同步守护进程。在选项后面指定自己角色,是master还是backup,主负载调度器会同步所有策略及连接状态到备负载调度器,当主故障,备可以立即接替工作 | 
| --stop-daemon | 停止同步守护进程 | 
选项同样也有两种形式,一个是全写,一个是缩写,功能一致。
常用选项说明如下:
| 常用选项 | 说明 | 
|---|---|
| --tcp-service -t service-address | 服务IP使用TCP协议,IP[:port] | 
| --udp-service -u service-address | 服务IP使用UDP协议,IP[:port] | 
| --fwmark-service -f fwmark | 使用整型的防火墙标记标识集群服务,可以结合iptables将多个以调度器为目标的端口定义成一个防火墙标识,ipvsadm通过此关联标识,可以实现对一个IP多个端口调度,即实现后端服务器可以开放多个服务 | 
| --scheduler -s scheduler | 参数为rr/wrr/lc/wlc/lblc/lblcr/dh/sh/sed/nq其中之一,默认调度算法是wlc | 
| --pe engine | 设置持久化引擎如源IP,默认不设置 | 
| --persistent -p [timeout] | 开启持久化,在指定时间timeout内,来自同一个IP的请求都会转发到同一台real server | 
| --netmask -M netmask | 使用网络掩码来屏蔽持久化源IP范围,默认32位掩码,即所有源IP的请求都会享受持久化服务 | 
| --real-server -r server-address | 指定real server的IP和port | 
| --gatewaying -g | 设置转发模式使用DR模式(direct routing),默认配置 | 
| --ipip -i | 设置转发模式使用TUN模式 | 
| --masquerading -m | 设置转发模式使用NAT模式 | 
| --weight -w weight | 设置real server的权重 | 
| --u-threshold -x uthreshold | 设置转发请求的最大上连接阈值,范围0~65535,当连接数超过上限,lvs则不会转发请求 | 
| --l-threshold -y lthreshold | 设置转发请求的下连接阈值,范围0~65535,当连接数降低至指定值,lvs则继续提供服务,默认0 | 
| --mcast-interface interface | 设置用于连接同步守护进程的组播接口 | 
| --syncid sid | 设置连接同步守护进程的SID号,用于标识连接同步组,范围0~255 | 
| --connection -c | 输出lvs的当前连接,一般和“-l”共用 | 
| --timeout | 输出tcp、tcpfin、udp的超时信息,一般和“-l”共用 | 
| --daemon | 输出连接同步守护信息,一般和“-l”共用 | 
| --stats | 输出lvs统计信息,一般和“-l”共用 | 
| --rate | 输出转发速率信息,一般和“-l”共用 | 
| --exact | 显示数据包和字节数的准确值,扩大字符长度 | 
| --thresholds | 显示阈值信息,一般和“-l”共用 | 
| --persistent-conn | 输出持久化连接信息,一般和“-l”共用 | 
| --numeric -n | 将输出的IP和port以数字化显示 | 
| --sched-flags -b flags | 设置调度算法的范围表示,用于SH算法,两个标识:sh-fallback,如果real server不可用,将其转发到其他real server;sh-port,将源地址的端口号也添加到散列键=值中 | 
添加一条虚拟服务器记录,指定加权轮询调度方式,然后指定该虚拟服务器记录添加real server,指定DR转发模式和权重
ipvsadm -A -t 192.168.14.100:80 -s wrr
ipvsadm -a -t 192.168.14.100:80 -r 192.168.14.129 -g -w 1
ipvsadm -a -t 192.168.14.100:80 -r 192.168.14.130 -g -w 1
查看当前内核虚拟服务器表及其统计数据,“-n”参数将所有输出以数字显示
[root@proxy ~]# ipvsadm -ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
-> RemoteAddress:Port
TCP  192.168.14.100:80                   0        0        0        0        0
-> 192.168.14.129:80                   0        0        0        0        0
-> 192.168.14.130:80                   0        0        0        0        0
[root@proxy ~]# ipvsadm -l --stats 
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
-> RemoteAddress:Port
TCP  proxy:http                          0        0        0        0        0
-> 192.168.14.129:http                 0        0        0        0        0
-> 192.168.14.130:http                 0        0        0        0        0
[root@proxy ~]# ipvsadm -S
-A -t proxy:http -s wrr
-a -t proxy:http -r 192.168.14.129:http -g -w 1
-a -t proxy:http -r 192.168.14.130:http -g -w 1
[root@proxy ~]# ipvsadm -Sn
-A -t 192.168.14.100:80 -s wrr
-a -t 192.168.14.100:80 -r 192.168.14.129:80 -g -w 1
-a -t 192.168.14.100:80 -r 192.168.14.130:80 -g -w 1
[root@proxy ~]# ipvsadm -Sn > ipvs_test
[root@proxy ~]# ipvsadm --restore < ipvs_test
[root@proxy ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.14.100:80 wrr
-> 192.168.14.129:80            Route   1      0          0         
-> 192.168.14.130:80            Route   1      0          0  原文:http://blog.51cto.com/13599730/2312722