1.检查操作系统是否支持IPVS
[root@idc131 ~]# grep -i vs /boot/config-2.6.18-308.el5
# CONFIG_X86_VSMP is not set
CONFIG_IP_VS=m
# CONFIG_IP_VS_DEBUG is not set
CONFIG_IP_VS_TAB_BITS=12
# IPVS transport protocol load balancing support
CONFIG_IP_VS_PROTO_TCP=y
CONFIG_IP_VS_PROTO_UDP=y
CONFIG_IP_VS_PROTO_ESP=y
CONFIG_IP_VS_PROTO_AH=y
# IPVS scheduler
CONFIG_IP_VS_RR=m
CONFIG_IP_VS_WRR=m
CONFIG_IP_VS_LC=m
CONFIG_IP_VS_WLC=m
CONFIG_IP_VS_LBLC=m
CONFIG_IP_VS_LBLCR=m
CONFIG_IP_VS_DH=m
CONFIG_IP_VS_SH=m
CONFIG_IP_VS_SED=m
CONFIG_IP_VS_NQ=m
# IPVS application helper
CONFIG_IP_VS_FTP=m
CONFIG_SCSI_MVSAS=m
CONFIG_MOUSE_VSXXXAA=m
CONFIG_MAX_RAW_DEVS=8192
2.安装ipvsadm
yum install ipvsadm.x86_64
//注意该软件包的安装源应该在cluster下面,而不是在base下面.
3.ipvsadm的功能主要包括:
3.1管理集群服务.
添加: ipvsadm -A|E -t|u|f service-address [-s scheduler]
-t tcp
-u udp
service-address: IP:PORT
-f fire wall mark 防火墙标记
service-address: Mark number
修改:-E
删除:ipvsadm -D -t|u|f service-address
3.2管理集群节点.
添加:ipvsadm -a -t|u|f service-address -r server-address [-g|i|m] -w weight
-g:DR模型
-i:tunel模型
-m:Nat模型
修改:ipvsadm -e -t|u|f service-address -r server-address
删除:ipvsadm -d -t|u|f service-address -r server-address
3.3查看:-L|-l
-n:显示数字格式的主机地址和端口
--state 显示统计数据
--rate 显示速率
--timeout 显示tcp,tcpfin,udp的超时时长
-c 显示当前ipvs的连接状况.
3.4清空所有集群服务
ipvsadm -C
3.5保存规则 ipvsadm -S >/etc/ipvsadm.conf
service ipvsadm save
3.6载入规则:ipvsadm -R </etc/ipvsadm.conf
4.同步各集群节点时间
date -s '20140825 23:42:00'
5.配置服务:
[root@mytest ~]# ipvsadm -A -t 192.168.112.7:80 -s rr
[root@mytest ~]# ipvsadm -a -t 192.168.112.7:80 -r 192.168.201.8 -m
[root@mytest ~]# ipvsadm -a -t 192.168.112.7:80 -r 192.168.201.9 -m
临时打开网卡间转发方法:
[root@mytest tmp]# cat /proc/sys/net/ipv4/ip_forward
0
[root@mytest tmp]# echo 1 >/proc/sys/net/ipv4/ip_forward
永久网卡间转发方法:
将/etc/sysctl.conf中net.ipv4.ip_forward=0,修改为net.ipv4.ip_forward=1.
重启网络服务.
6.测试.
[root@mytest html]# ab -n 1000 -c 100 http://192.168.112.7/index.html
7.修改为映射到内网的8080端口,且使用wrr调度算法:
7.1添加规则
[root@mytest html]# ipvsadm -C
[root@mytest html]# ipvsadm -A -t 192.168.112.7:80 -s wrr
[root@mytest html]# ipvsadm -a -t 192.168.112.7:80 -r 192.168.201.8:8080 -m -w 3
[root@mytest html]# ipvsadm -a -t 192.168.112.7:80 -r 192.168.201.9:8080 -m -w 6
[root@mytest html]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.112.7:80 wrr
-> 192.168.201.9:8080 Masq 6 0 0
-> 192.168.201.8:8080 Masq 3 0 0
7.2保存规则
[root@mytest html]# service ipvsadm save
ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm: [确定]
7.3测试
[root@mytest html]# ab -n 1000 -c 100 http://192.168.112.7/index.html
7.4查看状态
[root@mytest html]# ipvsadm -L --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes
-> RemoteAddress:Port
TCP mytest:http 36 180 178 20592 16564
-> mytest3:webcache 24 120 119 13728 10448
-> mytest2:webcache 12 60 59 6864 6116
8.至此lvs的nat模型测试完毕.
注:ipvsadm工作在input链上,当满足规则需要转发时,直接从INPUT链转向POST_ROUTING链.