此博文是案例博文,关于LVS负载均衡群集的原理及相关概述,请参考博文:Centos 7之LVS负载均衡群集html
使用NAT模式的群集技术,LVS负载调度器是全部节点访问Internet的网关服务器,其192.168.200.10做为整个群集的VIP地址。web
使用轮询(rr)的调度算法。算法
web1和web2先搭建web服务,分别准备不一样的网页文件,供客户端访问,以此来肯定client访问LVS服务器的192.168.200.10,能够访问到两台web服务器。vim
- 待client测试成功后,web1和web2即可以挂载NFS服务器提供的共享目录,以此来为client提供相同的网页文件。
[root@centos01 ~]# yum -y install httpd <!--安装httpd服务--> [root@centos01 ~]# echo "www.benet.com" > /var/www/html/index.html <!--建立网站主页测试页--> [root@centos01 ~]# systemctl start httpd <!--启动httpd服务--> [root@centos01 ~]# systemctl enable httpd <!--设置开机自动启动--> [root@centos01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32 <!--编辑网卡配置文件--> GATEWAY=192.168.100.40 <!--添加网关--> [root@centos01 ~]# systemctl restart network<!--重启网卡服务-->
[root@centos02 ~]# yum -y install httpd <!--安装httpd服务--> [root@centos02 ~]# echo "www.accp.com" > /var/www/html/index.html <!--建立网站主页测试页--> [root@centos02 ~]# systemctl start httpd <!--启动httpd服务--> [root@centos02 ~]# systemctl enable httpd <!--设置开机自动启动--> [root@centos02 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32 <!--编辑网卡配置文件--> GATEWAY=192.168.100.40 <!--添加网关--> [root@centos02 ~]# systemctl restart network <!--重启网卡服务-->
[root@centos04 ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens32 /etc/sysconfig/network-scripts/ifcfg-ens34 <!--复制网卡配置文件--> [root@centos04 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens34 <!--编辑ens34网卡配置文件--> TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes NAME=ens34 <!--修更名字--> DEVICE=ens34 <!--修更名字--> ONBOOT=yes IPADDR=192.168.200.10 <!--配置IP地址--> NATEMASK=255.255.255.0 [root@centos04 ~]# systemctl restart network <!--重启网卡服务--> [root@centos04 ~]# vim /etc/sysctl.conf <!--开启路由转发功能--> net.ipv4.ip_forward = 1 [root@centos04 ~]# sysctl -p <!--验证--> net.ipv4.ip_forward = 1 [root@centos04 ~]# modprobe ip_vs <!--加载ip_vs模块--> [root@centos04 ~]# yum -y install ipvsadm<!--安装ipvsadm软件包--> [root@centos04 ~]# ipvsadm -C <!--清空ipvsadm规则--> [root@centos04 ~]# ipvsadm -A -t 192.168.200.10:80 -s rr <!--建立虚拟服务器,群集的VIP地址为192.168.200.10--> [root@centos04 ~]# ipvsadm -a -t 192.168.200.10:80 -r 192.168.100.10:80 -m -w 1 <!--添加服务器节点--> [root@centos04 ~]# ipvsadm -a -t 192.168.200.10:80 -r 192.168.100.20:80 -m -w 1 <!--添加服务器节点--> [root@centos04 ~]# ipvsadm-save <!--保存策略--> -A -t centos04:http -s rr -a -t centos04:http -r 192.168.100.10:http -m -w 1 -a -t centos04:http -r 192.168.100.20:http -m -w 1 [root@centos04 ~]# ipvsadm-save > /etc/sysconfig/ipvsadm.bak<!--导出策略 备份策略--> [root@centos04 ~]# cat /etc/sysconfig/ipvsadm.bak <!--查看备份的策略--> -A -t centos04:http -s rr -a -t centos04:http -r 192.168.100.10:http -m -w 1 -a -t centos04:http -r 192.168.100.20:http -m -w 1
1)客户端配置IP地址centos
2)测试LVS负载均衡服务器是否工做正常(屡次访问192.168.200.10,能够获得两个不一样的页面):服务器
[root@centos03 ~]# yum -y install nfs-utils rpcbind <!--安装NFS相关软件包--> [root@centos03 ~]# systemctl enable nfs <!--设置为开机自动启动--> [root@centos03 ~]# systemctl enable rpcbind <!--设置为开机自动启动--> [root@centos03 ~]# mkdir -p /opt/wwwroot <!--建立共享目录--> [root@centos03 ~]# echo "www.wangyi.com" > /opt/wwwroot/index.html <!--新建网站主页,写入内容--> [root@centos03 ~]# vim /etc/exports <!--设置共享目录--> /opt/wwwroot 192.168.100.0/24(rw,sync,no_root_squash) <!--添加此行--> [root@centos03 ~]# systemctl restart rpcbind <!--重启服务--> [root@centos03 ~]# systemctl restart nfs <!--重启服务--> [root@centos03 ~]# showmount -e <!--查看共享的目录--> Export list for centos03: /opt/wwwroot 192.168.100.0/24
[root@centos01 ~]# yum -y install rpcbind nfs-utils<!--安装相关软件包--> [root@centos01 ~]# systemctl enable rpcbind <!--设置服务开机自动启动--> [root@centos01 ~]# systemctl start rpcbind <!--启动服务--> [root@centos01 ~]# showmount -e 192.168.100.30 <!--查询NFS服务器共享的目录--> Export list for 192.168.100.30: /opt/wwwroot 192.168.100.0/24 [root@centos01 ~]# mount 192.168.100.30:/opt/wwwroot /var/www/html/ <!--挂载共享目录--> [root@centos01 ~]# df -hT /var/www/html/ <!--查看是否挂载成功--> 文件系统 类型 容量 已用 可用 已用% 挂载点 192.168.100.30:/opt/wwwroot nfs4 76G 3.7G 73G 5% /var/www/html [root@centos01 ~]# vim /etc/fstab <!--设置自动挂载--> 192.168.100.30:/opt/wwwroot /var/www/html nfs defaults,_netdev 0 0
[root@centos02 ~]# yum -y install rpcbind nfs-utils <!--安装相关软件包--> [root@centos02 ~]# systemctl enable rpcbind <!--设置服务开机自动启动--> [root@centos02 ~]# systemctl start rpcbind <!--启动服务--> [root@centos02 ~]# showmount -e 192.168.100.30 <!--查询NFS服务器共享的目录--> Export list for 192.168.100.30: /opt/wwwroot 192.168.100.0/24 [root@centos02 ~]# mount 192.168.100.30:/opt/wwwroot /var/www/html/ <!--挂载共享目录--> [root@centos02 ~]# df -hT /var/www/html/ <!--查看是否挂载成功--> 文件系统 类型 容量 已用 可用 已用% 挂载点 192.168.100.30:/opt/wwwroot nfs4 76G 3.7G 73G 5% /var/www/html [root@centos02 ~]# vim /etc/fstab <!--设置自动挂载--> 192.168.100.30:/opt/wwwroot /var/www/html nfs defaults,_netdev 0 0
至此,无论怎样刷新访问,都将是看到同一个网页。最终的LVS负载均衡效果也就实现了。负载均衡
当LVS服务器重启后,LVS规则将会丢失,这就用到了备份,须要注意的是,备份时的主机名和恢复时的主机名必须一致,而且须要注意网卡优先的问题,不然在恢复后,会发现VIP(群集的虚拟IP)变成了LVS服务器的另外一个IP地址ide
[root@centos04 ~]# ipvsadm -ln <!--重启后查看策略--> IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn [root@centos04 ~]# ipvsadm-restore < /etc/sysconfig/ipvsadm.bak <!--恢复策略--> [root@centos04 ~]# 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.100.40:80 rr -> 192.168.100.10:80 Masq 1 0 0 -> 192.168.100.20:80 Masq 1 0 0
—————— 本文至此结束,感谢阅读 ——————测试