關於LVS負載均衡工作原理及其概述請訪問:Centos 7之LVS負載均衡羣集
關於地址轉換(NAT)模式的負載均衡羣集詳細配置請訪問:構建基於地址轉換(LVS—NAT)模式的負載均衡羣集
一、開始配置DR模式LVS
準備工作:
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 客戶端一臺;
1、配置負載調度器
1)配置虛擬IP地址(VIP)
[root@centos05 ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens32
/etc/sysconfig/network-scripts/ifcfg-ens32:0 <!--複製網卡配置文件-->
[root@centos05 ~]# 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
[root@centos05 ~]# systemctl restart network <!--重啓網卡服務-->
[root@centos05 ~]# 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)
2)調整/proc相應參數
[root@centos05 ~]# 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
[root@centos05 ~]# sysctl -p <!--刷新-->
3)配置負載分配策略
[root@centos05 ~]# modprobe ip_vs <!--加載ip_vs模塊-->
[root@centos05 ~]# yum -y install ipvsadm <!--安裝ipvsadm工具-->
[root@centos05 ~]# ipvsadm -C <!--清除原有策略-->
[root@centos05 ~]# ipvsadm -A -t 192.168.100.253:80 -s rr <!--配置調度服務器IP地址-->
[root@centos05 ~]# ipvsadm -a -t 192.168.100.253:80 -r 192.168.100.10:80 -g -w 1
<!--添加物理服務器-->
[root@centos05 ~]# ipvsadm -a -t 192.168.100.253:80 -r 192.168.100.20:80 -g -w 1
<!--添加物理服務器-->
[root@centos05 ~]# ipvsadm-save > /etc/sysconfig/ipvsadm <!--導出策略備份-->
[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.253:80 rr
-> 192.168.100.10:80 Route 1 0 0
-> 192.168.100.20:80 Route 1 0 0
2、配置web節點1服務器
[root@centos01 ~]# yum -y install httpd <!--安裝http服務-->
[root@centos01 ~]# echo "www.benet.com" > /var/www/html/index.html
<!--準備測試網頁,等看到負載均衡的效果後,再掛載共享存儲設備-->
[root@centos01 ~]# systemctl start httpd <!--啓動http服務-->
[root@centos01 ~]# systemctl enable httpd <!--設置爲開機自啓動-->
[root@centos01 ~]# cp /etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-lo:0 <!--生成虛擬網卡-->
[root@centos01 ~]# 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
[root@centos01 ~]# systemctl restart network <!--重啓網卡服務-->
[root@centos01 ~]# 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)
[root@centos01 ~]# 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
[root@centos01 ~]# sysctl -p <!--刷新-->
3、配置web節點2服務器
[root@centos02 ~]# yum -y install httpd <!--安裝http服務-->
[root@centos02 ~]# echo "www.accp.com" > /var/www/html/index.html
<!--準備測試網頁,等看到負載均衡的效果後,再掛載共享存儲設備-->
[root@centos02 ~]# systemctl start httpd <!--啓動http服務-->
[root@centos02 ~]# systemctl enable httpd <!--設置爲開機自啓動-->
[root@centos01 ~]# scp /etc/sysctl.conf [email protected]:/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.
[email protected]'s password:
sysctl.conf
[root@centos01 ~]# scp /etc/sysconfig/network-scripts/ifcfg-lo:0
<!--拷貝lo:0網卡配置文件到第二臺web服務器--> [email protected]:/etc/sysconfig/network-scripts/
[email protected]'s password:
ifcfg-lo:0 100% 70 177.3KB/s 00:00
[root@centos02 ~]# 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
[root@centos02 ~]# systemctl restart network <!--第二臺web服務器重啓網卡服務-->
4、客戶端訪問
客戶端配置和服務器同網段IP地址,瀏覽器訪問http://192.168.100.253
關閉瀏覽器刪除緩存,重新訪問就會得到一個新的頁面,實現了負載均衡
二、安裝NFS共享存儲
1、配置NFS
[root@centos03 ~]# yum -y install rpcbind nfs-utils <!--安裝NFS系統-->
[root@centos03 ~]# mkdir /web <!--創建共享網站根目錄文件-->
[root@centos03 ~]# echo "www.nfs.com" > /web/index.html <!--網站主頁寫入測試數據-->
[root@centos03 ~]# vim /etc/exports <!--修改nfs主配置文件共享/web目錄,
允許100.10讀取共享目錄, 100.20允許讀取和寫入共享目錄-->
/web 192.168.100.10(ro) 192.168.100.20(rw)
[root@centos03 ~]# systemctl start rpcbind <!--啓動rpcbind-->
[root@centos03 ~]# systemctl start nfs <!--啓動nfs服務-->
[root@centos03 ~]# systemctl enable rpcbind <!--設置開機自動啓動-->
[root@centos03 ~]# systemctl enable nfs <!--設置開機自動啓動-->
[root@centos03 ~]# showmount -e 192.168.100.30 <!--查看共享目錄-->
Export list for 192.168.100.30:
/web 192.168.100.20,192.168.100.10
2、Web節點1服務器掛載共享目錄
[root@centos01 ~]# mount 192.168.100.30:/web /var/www/html/
<!--掛載共享目錄到網站服務器的根目錄-->
[root@centos01 ~]# cat /var/www/html/index.html <!--查看是否掛載成功-->
www.nfs.com
[root@centos01 ~]# systemctl restart httpd <!--重啓httpd服務-->
3、Web節點2服務器掛載共享目錄
[root@centos02 ~]# mount 192.168.100.30:/web /var/www/html/
<!--掛載共享目錄到網站服務器的根目錄-->
[root@centos02 ~]# cat /var/www/html/index.html <!--查看是否掛載成功-->
www.nfs.com
[root@centos02 ~]# systemctl restart httpd <!--重啓httpd服務-->
4、客戶端訪問
使用tail -f /var/log/httpd/access.log查看訪問成功日誌,一定要想監聽再去訪問纔可以看到效果
5、配置自動掛載NFS
1)web節點1服務器
[root@centos01 ~]# vim /etc/fstab
192.168.100.30:/web /var/www/html/ nsf defaults 0 0
2)web節點2服務器
[root@centos02 ~]# vim /etc/fstab
192.168.100.30:/web /var/www/html/ nsf defaults 0 0
—————— 本文至此結束,感謝閱讀 ——————