实验拓扑图:
操作流程:
Director Server pc50 : 192.168.37.50
172.16.131.244
安装并启用ipvsadm
创建虚拟服务器
向虚拟服务器上加入节点
web Server1 pc51: 192.168.37.51
web Server2 pc52: 192.168.37.52
配置WEB 客户端
Clinet : 172.16.131.247
连接虚拟服务器测试
具体步骤:
全局环境准备:
配置yam源或准备以下需要的软件
# systemctl stop firewalld.service //关闭防火墙
# systemctl disable firewalld.service //关闭开机自启
# firewall-cmd --state //查看
# setenforce 0 //设置SELinux 为宽松模式
web服务器 pc51 / pc52 :
# yum -y install httpd
[root@pc51 ~]# echo '192.168.37.51' > /var/www/html/test.html
[root@pc52 ~]# echo "192.168.37.52" > /var/www/html/test.html
# systemctl start httpd
# systemctl enable httpd
# yum -y install elinks
[root@pc51 ~]# elinks --dump http://localhost/test.html
192.168.37.51
[root@pc52 ~]# elinks --dump http://localhost/test.html
192.168.37.52
配置分发器 pc50:
#yum -y install ipvsadm
//开启内核的路由转发功能
# echo "net.ipv4.ip_forward=1" > /etc/sysctl.conf
# sysctl -p
net.ipv4.ip_forward = 1
网站服务器 pc51 / pc52 :
指定网关地址 192.168.37.50
# route -n//查看路由
# route add default gw 192.168.37.50//临时配置网关 网卡重启后生效
//永久配置网关
# vim /etc/sysconfig/network-scripts/ifcfg-ens33
最后加:GATEWAY=192.168.37.50
# systemctl restart network //重新加载网卡
客户端 172.16.131.247 配置
指定网关地址 172.16.131.244 :
# vim /etc/sysconfig/network-scripts/ifcfg-ens33
最后加:GATEWAY=172.16.131.244
# systemctl restart network
# ping -c 2 192.168.37.51 //测试
PING 192.168.37.51 (192.168.37.51) 56(84) bytes of data.
64 bytes from 192.168.37.51: icmp_seq=1 ttl=63 time=0.322 ms
64 bytes from 192.168.37.51: icmp_seq=2 ttl=63 time=0.503 ms
--- 192.168.37.51 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.322/0.412/0.503/0.092 ms
# yum -y install elinks
配置分发器 pc50 :
# yum -y install ipvsadm
# rpm -q ipvsadm
ipvsadm-1.27-7.el7.x86_64
添加虚拟服务
# ipvsadm -L //查看 IPVS
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
# ipvsadm -A -t 172.16.131.244:80 -s rr//添加虚拟服务 调度算法为Round Robin
# ipvsadm -Ln //- n 数字显示
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.131.244:80 rr
//向虚拟服务器中加入节点
# ipvsadm -a -t 172.16.131.244:80 -r 192.168.37.51:80 -m
# ipvsadm -a -t 172.16.131.244:80 -r 192.168.37.52:80 -m
# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.131.244:80 rr
-> 192.168.37.51:80 Masq 1 0 0
-> 192.168.37.52:80 Masq 1 0 0
客户端测试
# elinks --dump http://172.16.131.244/test.html
192.168.37.51
# elinks --dump http://172.16.131.244/test.html
192.168.37.52
//客户端 轮询到不同的后端真实服务器
Director Server 查看:
[root@pc50 ~]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes
-> RemoteAddress:Port
TCP 172.16.131.244:80 2 10 10 846 1098
-> 192.168.37.51:80 1 5 5 423 549
-> 192.168.37.52:80 1 5 5 423 549
模拟pc51 web服务故障:
[root@pc51 ~]# systemctl stop httpd
[root@pc50 ~]# ipvsadm -Z
//客户端测试
# elinks --dump http://172.16.131.244/test.html
192.168.4.52
# elinks --dump http://172.16.131.244/test.html
192.168.4.52
[root@pc50 ~]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes
-> RemoteAddress:Port
TCP 172.16.131.244:80 3 11 11 906 1138
-> 192.168.4.51:80 1 1 1 60 40
-> 192.168.4.52:80 2 10 10 846 1098