1.准备环境。
1)克隆 四台虚拟机 pc10, lvs20, web30,web40.
分别设置好IP 192.168.4.10,192.168.4.20(192.168.2.20),192.168.2.30,192.168.2.40.
10位客户机,20为转发和调度机,需设置两张网卡eth0和eth1,30和40为web服务器。
2)每台机器上的firewalld和selinux都关掉。
3)web30和40上都安装httpd 并开启服务:
#yum -y install httpd
#systemctl start httpd
4)lvs20上打开IP转发:
# vim /etc/sysctl.conf
将以下内容添加进去
#net.ipv4.ip_forward = 1
安装ipvsadm:
#yum -y install ipvsadm
然后启动ipvsadm
#systemctl start ipvsadm
如果启动不成功可以执行以下命令:
#ipvsadm --save > /etc/sysconfig/ipvsadm
5)web30和web40上指定网关为192.168.2.20
route add default gw 192.168.2.20
pc10上指定网关为192.168.4.20
2. 配置ipvsadm
在lvs20上操作;
1)添加虚拟服务
[[email protected] ~]# ipvsadm -A -t 192.168.4.20:80 -s rr
[[email protected] ~]# 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.4.20:80 rr
2)添加 realserver web30&40
[[email protected] ~]# ipvsadm -a -t 192.168.4.20:80 -r 192.168.2.30:80 -m
[[email protected] ~]# 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.4.20:80 rr
-> 192.168.2.30:80 Masq 1 0 0
[[email protected] ~]# ipvsadm -a -t 192.168.4.20:80 -r 192.168.2.40:80 -m
[[email protected] ~]# 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.4.20:80 rr
-> 192.168.2.30:80 Masq 1 0 0
-> 192.168.2.40:80 Masq 1 0 0
3)保存策略
[[email protected] ~]# ipvsadm-save
-A -t 192.168.4.20:http -s rr
-a -t 192.168.4.20:http -r 192.168.2.30:http -m -w 1
-a -t 192.168.4.20:http -r 192.168.2.40:http -m -w 1
3.测试 ,pc10上进行测试
安装elinks
[[email protected] ~]# yum -y install elinks
使用elinks测试 lvs20分发效果
[[email protected] ~]# elinks --dump http://192.168.4.20/test.html
web40
[[email protected] ~]# elinks --dump http://192.168.4.20/test.html
web30
可以在分发机器上查看连接信息汇总
[[email protected] ~]# 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.4.20:80 8 48 33 3800 4332
-> 192.168.2.30:80 4 24 17 1900 2192
-> 192.168.2.40:80 4 24 16 1900 2140
改权重值: -e -w
[[email protected] ~]# ipvsadm -e -t 192.168.4.20:80 -r 192.168.2.40:80 -w 3
[[email protected] ~]# 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.4.20:80 rr
-> 192.168.2.30:80 Masq 1 0 0
-> 192.168.2.40:80 Route 3 0 0
改调度算法: -E -m NAT模式 -g DR模式 -i TUN模式
[[email protected] ~]# ipvsadm -E -t 192.168.4.20:80 -s wrr
删除realserver: -d
[[email protected] ~]# ipvsadm -d -t 192.168.4.20:80 -r 192.168.2.30:80
[[email protected] ~]# 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.4.20:80 wrr
-> 192.168.2.40:80 Route 3 0 0
清空虚拟服务: -C
[[email protected] ~]# ipvsadm -C
[[email protected] ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn