畅聊架构 2019-12-09
关于地址转换(NAT)模式的负载均衡群集详细配置请访问:构建基于地址转换(LVS—NAT)模式的负载均衡群集
准备工作:
Centos 7操作系统四台;
Centos01模拟Web1服务器:IP地址/192.168.100.10
Centos02模拟Web2服务器:IP地址/192.168.100.20
Centos03模拟NFS服务器: IP地址/192.168.100.30
Centos05模拟LVS服务器: IP地址/192.168.100.50
Windows 客户端一台;
[ ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens32 /etc/sysconfig/network-scripts/ifcfg-ens32:0 <!--复制网卡配置文件--> [ ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32:0 <!--配置VIP地址网卡,设置VIP地址为192.168.100.253--> TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes NAME=ens32:0 <!--修改名字--> DEVICE=ens32:0 <!--修改名字--> ONBOOT=yes IPADDR=192.168.100.253 <!--修改IP地址--> NATEMASK=255.255.255.0 [ ~]# systemctl restart network <!--重启网卡服务--> [ ~]# ifconfig <!--查看是否配置成功--> ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.50 netmask 255.255.255.0 broadcast 192.168.100.255 inet6 fe80::20c:29ff:fe16:c54b prefixlen 64 scopeid 0x20<link> ether 00:0c:29:16:c5:4b txqueuelen 1000 (Ethernet) RX packets 2795 bytes 1095013 (1.0 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1371 bytes 182001 (177.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens32:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.253 netmask 255.255.255.0 broadcast 192.168.100.255 ether 00:0c:29:16:c5:4b txqueuelen 1000 (Ethernet)
[ ~]# vim /etc/sysctl.conf <!--修改LVS服务器的内核配置文件支持DR模式--> net.ipv4.ip_forward = 1 net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 net.ipv4.conf.ens32.send_redirects = 0 [ ~]# sysctl -p <!--刷新-->
[ ~]# modprobe ip_vs <!--加载ip_vs模块--> [ ~]# yum -y install ipvsadm <!--安装ipvsadm工具--> [ ~]# ipvsadm -C <!--清除原有策略--> [ ~]# ipvsadm -A -t 192.168.100.253:80 -s rr <!--配置调度服务器IP地址--> [ ~]# ipvsadm -a -t 192.168.100.253:80 -r 192.168.100.10:80 -g -w 1 <!--添加物理服务器--> [ ~]# ipvsadm -a -t 192.168.100.253:80 -r 192.168.100.20:80 -g -w 1 <!--添加物理服务器--> [ ~]# ipvsadm-save > /etc/sysconfig/ipvsadm <!--导出策略备份--> [ ~]# 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.253:80 rr -> 192.168.100.10:80 Route 1 0 0 -> 192.168.100.20:80 Route 1 0 0
[ ~]# yum -y install httpd <!--安装http服务--> [ ~]# echo "www.benet.com" > /var/www/html/index.html <!--准备测试网页,等看到负载均衡的效果后,再挂载共享存储设备--> [ ~]# systemctl start httpd <!--启动http服务--> [ ~]# systemctl enable httpd <!--设置为开机自启动--> [ ~]# cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-lo:0 <!--生成虚拟网卡--> [ ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo:0 <!--编辑虚拟网卡监听地址为lvs服务器的VIP地址--> DEVICE=lo:0 IPADDR=192.168.100.253 NETMASK=255.255.255.255 ONBOOT=yes [ ~]# systemctl restart network <!--重启网卡服务--> [ ~]# ifconfig <!--查看配置是否生效--> lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 192.168.100.253 netmask 255.255.255.255 loop txqueuelen 1 (Local Loopback) [ ~]# vim /etc/sysctl.conf <!--修改web服务器ARP响应--> net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 net.ipv4.conf.default.arp_ignore = 1 net.ipv4.conf.default.arp_announce = 2 net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.lo.arp_announce = 2 [ ~]# sysctl -p <!--刷新-->
[ ~]# yum -y install httpd <!--安装http服务--> [ ~]# echo "www.accp.com" > /var/www/html/index.html <!--准备测试网页,等看到负载均衡的效果后,再挂载共享存储设备--> [ ~]# systemctl start httpd <!--启动http服务--> [ ~]# systemctl enable httpd <!--设置为开机自启动--> [ ~]# scp /etc/sysctl.conf :/etc/ <!--拷贝第一台web服务器/etc/sysctl.conf配置文件到第二台web服务器的/etc/目录下--> The authenticity of host ‘192.168.100.30 (192.168.100.20)‘ can‘t be established. ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I. ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘192.168.100.20‘ (ECDSA) to the list of known hosts. ‘s password: sysctl.conf [ ~]# scp /etc/sysconfig/network-scripts/ifcfg-lo:0 <!--拷贝lo:0网卡配置文件到第二台web服务器--> :/etc/sysconfig/network-scripts/ ‘s password: ifcfg-lo:0 100% 70 177.3KB/s 00:00 [ ~]# sysctl -p <!--在第二台web服务器更新配置--> net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 net.ipv4.conf.default.arp_ignore = 1 net.ipv4.conf.default.arp_announce = 2 net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.lo.arp_announce = 2 [ ~]# systemctl restart network <!--第二台web服务器重启网卡服务-->
客户端配置和服务器同网段IP地址,浏览器访问http://192.168.100.253
关闭浏览器删除缓存,重新访问就会得到一个新的页面,实现了负载均衡
[ ~]# yum -y install rpcbind nfs-utils <!--安装NFS系统--> [ ~]# mkdir /web <!--创建共享网站根目录文件--> [ ~]# echo "www.nfs.com" > /web/index.html <!--网站主页写入测试数据--> [ ~]# vim /etc/exports <!--修改nfs主配置文件共享/web目录, 允许100.10读取共享目录, 100.20允许读取和写入共享目录--> /web 192.168.100.10(ro) 192.168.100.20(rw) [ ~]# systemctl start rpcbind <!--启动rpcbind--> [ ~]# systemctl start nfs <!--启动nfs服务--> [ ~]# systemctl enable rpcbind <!--设置开机自动启动--> [ ~]# systemctl enable nfs <!--设置开机自动启动--> [ ~]# showmount -e 192.168.100.30 <!--查看共享目录--> Export list for 192.168.100.30: /web 192.168.100.20,192.168.100.10
[ ~]# mount 192.168.100.30:/web /var/www/html/ <!--挂载共享目录到网站服务器的根目录--> [ ~]# cat /var/www/html/index.html <!--查看是否挂载成功--> www.nfs.com [ ~]# systemctl restart httpd <!--重启httpd服务-->
[ ~]# mount 192.168.100.30:/web /var/www/html/ <!--挂载共享目录到网站服务器的根目录--> [ ~]# cat /var/www/html/index.html <!--查看是否挂载成功--> www.nfs.com [ ~]# systemctl restart httpd <!--重启httpd服务-->
使用tail -f /var/log/httpd/access.log查看访问成功日志,一定要想监听再去访问才可以看到效果
[ ~]# vim /etc/fstab 192.168.100.30:/web /var/www/html/ nsf defaults 0 0
[ ~]# vim /etc/fstab 192.168.100.30:/web /var/www/html/ nsf defaults 0 0
—————— 本文至此结束,感谢阅读 ——————