Ipvsadm命令的用法管理集群服务:ipvsadm -A|E -t|u|f service-address [-s scheduler]
-A:添加集群服务
-E:修改集群服务
-t:承载的应用层协议为基于TCP协议提供服务的协议;其server-address的格式为“VIP:PORT”,如“172.16.4.100:80”;
-u:承载的应用层协议为基于UDP协议提供服务的协议;其server-address的格式为“VIP:PORT”,如“172.16.4.100:53”;
-f:承载的应用层协议为基于TCP或UDP协议提供服务的协议,但此类报文会经由iptables/netfilter打标记,即防火墙标记,其server-address的格式为“FWM”,例如“10”;
-sscheduler:指明调度方法,默认为wlc;
删除:ipvsadm –D –t|u|f server-address
管理集群服务的RS:ipvsadm -a|e -t|u|f service-address-r server-address [-g|i|m] [-w weight]
添加或修改:
-r server-address:指明RS,server-address格式一般为“IP[:PORT:]”:注意,只支持端口映射的lvs类型才应该显示定义此处端口。
[-g|i|m]:指明lvs类型
-g:dr类型
-i:tun类型
-m:nat类型
[-w weight]:当前RS的权重;
注意:仅对于支持加权调度的scheduler,权重才有意义;
删除:
ipvsadm -d -t|u|f service-address -r server-address
清空所有集群服务的定义: ipvsadm –c 保存及恢复集群服务及RS的定义: ipvsadm -S > /etc/sysconfig/ipvsadm ipvsadm-save > /etc/sysconfig/ipvsadm service ipvsadm save
ipvsadm -R < /etc/sysconfig/ipvsadm ipvsadm-restore < /etc/sysconfig/ipvsadm service ipvsadm restart 查看规则:ipvsadm-L|l [options]
-c:列出当前所有connection;
1
2
3
4
| [iyunv@LVS ~]# ipvsadm -L -c
IPVS connection entries
pro expire state source virtual destination
TCP 00:54 SYN_RECV 172.16.0.101:57660172.16.4.1:http 172.16.4.101:http
|
输出说明:
pro:使用的协议
expire:连接过期时间
state:连接状态
source:客户端地址
virtual:vip地址
destination:响应客户端的RS服务器地址
--stats:列出统计数据:
1
2
3
4
5
6
7
| [iyunv@LVS ~]# ipvsadm -L --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes
->RemoteAddress:Port
TCP 172.16.4.1:http 22 104 0 11448 0
->172.16.4.101:http 11 51 0 5764 0
->172.16.4.102:http 11 53 0 5684 0
|
输出说明:
Conns:连接数
InPkts:入站报文
OutPkts: 出战响应
InBytes:入站字节数
OutBytes:出战字节数
说明:如果地址是vip地址,则是总数;如果是RS地址,则是direction分配给vip的数据。
--rate: 列出速率
1
2
3
4
5
6
7
| [iyunv@LVS ~]# ipvsadm -L --rate
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port CPS InPPS OutPPS InBPS OutBPS
->RemoteAddress:Port
TCP 172.16.4.1:http 0 0 0 0 0
->172.16.4.101:http 0 0 0 0 0
->172.16.4.102:http 0 0 0 0 0
|
输出说明:
CPS:每秒钟连接数
InPPS:每秒钟入站报文数
OutPPS:每秒钟出战报文数
InBPS:每秒钟入站字节大小
OutBPS:每秒钟出战字节大小
-n, --numeric: 数字格式显示IP及端口;
1
2
3
4
5
6
7
| [iyunv@LVS ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
->RemoteAddress:Port ForwardWeight ActiveConn InActConn
TCP 172.16.4.1:80 rr
->172.16.4.101:80 Route 1 0 0
->172.16.4.102:80 Route 2 0 0
|
输出说明:
Weight:权重
ActiveConn:活动连接
InActConn:非活动连接
--exact: 精确值;
1
2
3
4
5
6
7
| [iyunv@LVS~]# ipvsadm -L --exact
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
->RemoteAddress:Port ForwardWeight ActiveConn InActConn
TCP 172.16.4.1:http rr
->172.16.4.101:http Route 1 0 1
->172.16.4.102:http Route 2 0 0
|
清空计数器:及速率统计和连接信息等都会为0
ipvsadm-Z [-t|u|f service-address]
配置LVS-NAT配置拓扑:
拓扑说明:
| 主机名
| 系统环境
| IP地址
| 调度器
| LVS
| Centos6.6 64位
| 外网:eth0 172.16.4.100
内网:eth1 192.168.1.1
| WEB-01
| WEB-01
| 内网:eth0 192.168.1.10
| WEB-02
| WEB-02
| 内网:eth0 192.168.1.11
| 配置以前注意事项说明:
1、RS的网关必须指向Director
2、director需要开启路由转发功能
后端web服务器配置WEB-01和WEB-02最好准备不同的页面,一遍测试负载均衡效果。
1
2
3
4
| [iyunv@WEB-01 ~]# echo "WEB-01" > /var/www/html/index.html
[iyunv@WEB-01 ~]# service httpd start
[iyunv@WEB-02 ~]# echo "WEB-02" >/var/www/html/index.html
[iyunv@WEB-02 ~]# service httpd start
|
确保调度器可以访问web测试
1
2
3
4
| [iyunv@LVS ~]# curl 192.168.1.10
WEB-01
[iyunv@LVS ~]# curl 192.168.1.11
WEB-02
|
调度器配置启动路由转发功能
1
2
3
4
| [iyunv@LVS~]# vim /etc/sysctl.conf
net.ipv4.ip_forward= 1
[iyunv@LVS~]# sysctl –p
net.ipv4.ip_forward= 1 #出现此选项说明转发功能开启成功
|
设置ipvsadm策略
1
2
3
| [iyunv@LVS ~]# ipvsadm -A -t 172.16.4.100:80 -s rr
[iyunv@LVS ~]# ipvsadm -a -t 172.16.4.100:80 -r 192.168.1.10-m -w 1
[iyunv@LVS ~]# ipvsadm -a -t 172.16.4.100:80 -r 192.168.1.11 -m -w 1
|
验证集群:访问为结果web-01,刷新出现web-02,说明实现了负载均衡功能
查看ipvsadm也出现了连接
1
2
3
4
5
6
7
| [iyunv@LVS ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
->RemoteAddress:Port ForwardWeight ActiveConn InActConn
TCP 172.16.4.100:80 rr
->192.168.1.10:80 Masq 1 0 1
->192.168.1.11:80 Masq 1 0 1
|
配置LVS-DR环境拓扑:
环境描述:
| 主机名
| 系统环境
| IP地址
| 调度器
| LVS
| Centos6.6 64位
| 外网:eth0 172.16.4.100
VIP: eth0:0 172.16.4.1
| WEB-01
| WEB-01
| 外网:eth0 172.16.4.101
VIP: eth0:0 172.16.4.1
| WEB-02
| WEB-02
| 外网:eth0 172.16.4.102
VIP: eth0:0 172.16.4.1
| 后端web服务器配置说明:WEB-01和WEB-02除了网页内容不一样(生产环境网页内容必须一样)之外,其他的都一样。
WEB-01配置设置web页面
1
2
| [iyunv@WEB-01 ~]# echo "WEB-01" >/var/www/html/index.html
[iyunv@WEB-01 ~]# service httpd start
|
设置内核参数,抑制ARP
1
2
3
4
| [iyunv@WEB-01 ~]# echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
[iyunv@WEB-01 ~]# echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce
[iyunv@WEB-01 ~]# echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore
[iyunv@WEB-01 ~]# echo "2">/proc/sys/net/ipv4/conf/all/arp_announce
|
设置vip地址
1
2
3
4
| [iyunv@WEB-01 ~]# ifconfig lo:0 172.16.4.1 broadcast172.16.4.1 netmask 255.255.255.255 up
[iyunv@WEB-01 ~]# ifconfig | grep 172.16.4.1
inet addr:172.16.4.101 Bcast:172.16.255.255 Mask:255.255.0.0
inet addr:172.16.4.1 Mask:255.255.255.255
|
设置路由
1
| [iyunv@WEB-01 ~]# route add -host 172.16.4.1 dev lo:0
|
WEB-02配置设置web页面
1
2
| [iyunv@WEB-02 ~]# echo "WEB-02" >/var/www/html/index.html
[iyunv@WEB-02 ~]# service httpd start
|
设置内核参数
1
2
3
4
| [iyunv@WEB-02 ~]# echo "1">/proc/sys/net/ipv4/conf/lo/arp_ignore
[iyunv@WEB-02 ~]# echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce
[iyunv@WEB-02 ~]# echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore
[iyunv@WEB-02 ~]# echo "2">/proc/sys/net/ipv4/conf/all/arp_announce
|
设置VIP
1
2
3
4
5
| [iyunv@WEB-02 ~]# ifconfig lo:0 172.16.4.1 broadcast172.16.4.1 netmask 255.255.255.255 up
ifconfig |grep 172.16.4.1
[iyunv@WEB-02 ~]# ifconfig | grep 172.16.4.1
inet addr:172.16.4.102 Bcast:172.16.255.255 Mask:255.255.0.0
inet addr:172.16.4.1 Mask:255.255.255.255
|
设置路由
1
| [iyunv@WEB-02 ~]# route add -host 172.16.4.1 dev lo:0
|
调度器设置确保lvs可以访问后端web服务器
1
2
3
4
| [iyunv@LVS ~]# curl 172.16.4.101
WEB-01
[iyunv@LVS ~]# curl 172.16.4.102
WEB-02
|
设置vip地址
1
2
3
4
5
| [iyunv@LVS ~]# ifconfig eth0:0 172.16.4.1 netmask255.255.255.255 up
ifconfig | grep 172.16.4.1
[iyunv@LVS ~]# ifconfig | grep 172.16.4.1
inet addr:172.16.4.100 Bcast:172.16.255.255 Mask:255.255.0.0
inet addr:172.16.4.1 Bcast:172.16.4.1 Mask:255.255.255.255
|
配置负载均衡策略
1
2
3
| [iyunv@LVS ~]# ipvsadm -A -t 172.16.4.1:80 -s rr
[iyunv@LVS ~]# ipvsadm -a -t 172.16.4.1:80 -r172.16.4.10 -g -w 1
[iyunv@LVS ~]# ipvsadm -a -t 172.16.4.1:80 -r172.16.4.11 -g -w 1
|
客户端访问验证,持续刷新如果出现轮训效果说明配置成功
特殊情况:httpd启用长连接时的lvs轮询httpd启动长连接的方式。
1
2
3
| KeepAlive ON
MaxKeepAliveRequests 100
KeepAliveTimeout 15
|
启动之后,客户端请求通过lvs调度到后端的RS服务器,会始终调度到一个页面,直到长连接结束,rr的调度算法在这种情况下是不会实现轮训的。
使用脚本配置LVS-DRDirection使用的脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
| #!/bin/bash
#
vip=172.16.4.1
rip=('172.16.4.101' '172.16.4.102')
weight=('1' '1')
port=80
scheduler=rr
ipvstype='-g'
rpm -q ipvsadm >> /dev/null
if [ $? != 0 ];then
echo"please install ipvsadm"
exit 1
fi
case $1 in
start)
iptables -F-t filter
ipvsadm -C
ifconfigeth0:0 $vip broadcast $vip netmask 255.255.255.255 up
route add-host $vip dev eth0:0
ipvsadm -A-t $vip:$port -s $scheduler
[ $? -eq 0] && echo "ipvs service $vip:$port added." || exit 2
for i in`seq 0 $[${#rip[@]}-1]`; do
ipvsadm-a -t $vip:$port -r ${rip[$i]} $ipvstype -w ${weight[$i]}
[ $? -eq0 ] && echo "RS ${rip[$i]} added."
done
touch/var/lock/subsys/ipvs
;;
stop)
ipvsadm -C
ifconfigeth0:0 down
rm -f /var/lock/subsys/ipvs
echo"ipvs stopped."
;;
status)
if [ -f/var/lock/subsys/ipvs ]; then
echo"ipvs is running."
ipvsadm -L -n
else
echo"ipvs is stopped."
fi
;;
*)
echo"Usage: `basename $0` {start|stop|status}"
exit 3
;;
esac
|
RS使用的脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
| #!/bin/bash
#
vip=172.16.4.1
interface="lo:0"
case $1 in
start)
echo 1> /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1> /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2> /proc/sys/net/ipv4/conf/all/arp_announce
echo 2> /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig$interface $vip broadcast $vip netmask 255.255.255.255 up
route add-host $vip dev $interface
;;
stop)
echo 0 >/proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 >/proc/sys/net/ipv4/conf/all/arp_announce
echo 0 >/proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig$interface down
;;
status)
if ifconfiglo:0 |grep $vip &> /dev/null; then
echo"ipvs is running."
else
echo"ipvs is stopped."
fi
;;
*)
echo"Usage: `basename $0` {start|stop|status}"
exit 1
esac
|
|