為方便進行原理分析,將Client與群集機器放在同一網路中,封包流經的路線為1-2-3-4
一臺排程器: | VM1:192.168.100:23 | 閘道器:不需要或者 192.168.100.1 |
---|---|---|
二臺WEB伺服器叢集池: | 192.168.100.24 | 閘道器:不需要或者 192.168.100.1 |
192.168.100.25 | 閘道器:不需要或者 192.168.100.1 | |
一臺NFS共用伺服器: | 192.168.100.21 | 192.168.100.1 |
虛擬VIP: | 192.168.100.88 | |
其他: | VM1網路卡為僅主機模式 | 筆電VM1 IP為:192.168.100.1 |
如果你是最小化安裝 需要 yum -y install net-tools
我們要使用ifconfig這條命令,所有伺服器全都要裝
【1】設定虛擬IP地址(VIP)
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33:0
[root@localhost network-scripts]# vi ifcfg-ens33:0
NAME=ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.100.88
NETMASK=255.255.255.255
[root@localhost network-scripts]# ifup ifcfg-ens33:0
[root@localhost network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.23 netmask 255.255.255.0 broadcast 192.168.32.255
.....
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.88 netmask 255.255.255.0 broadcast 192.168.32.255
ether 00:0c:29:9b:a7:cc txqueuelen 1000 (Ethernet)
#【2】調整/proc響應引數 對於 DR 群集模式來說,由於 LVS 負載排程器和各節點需要共用 VIP 地址,
#應該關閉 Linux 核心的重定向引數響應
伺服器不是一臺路由器,那麼它不會傳送重定向,所以可以關閉該功能
[root@localhost network-scripts]# vi /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
#生效
[root@localhost network-scripts]# sysctl -p
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
【3】設定負載分配策略
[root@localhost /]# ipvsadm -v #檢查核心是否支援
[root@localhost ~]#yum -y install ipvsadm
[root@localhost ~]# modprobe ip_vs
[root@localhost ~]# cat /proc/net/ip_vs
[root@localhost ~]#yum -y install ipvsadm
[root@localhost ~]# ipvsadm -A -t 192.168.100.88:80 -s rr
[root@localhost ~]# ipvsadm -a -t 192.168.100.88:80 -r 192.168.100.24:80 -g
[root@localhost ~]# ipvsadm -a -t 192.168.100.88:80 -r 192.168.100.25:80 -g
[root@localhost network-scripts]# ipvsadm-save > /opt/ipvsadm
[root@localhost ~]# systemctl start ipvsadm
[root@localhost network-scripts]# systemctl enable ipvsadm
rpm -q nfs-utils ###如果沒裝,yum -y install nfs-utils
rpm -q rpcbind ###如果沒裝,yum -y install rpcbind
###最好先啟動rpcbind,不然有時會報錯
[root@localhost ~]# systemctl restart rpcbind
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# mkdir /opt/51xit /opt/52xit
[root@localhost ~]# echo "51是我" >/opt/51xit/index.html
[root@localhost ~]# echo "我是52" >/opt/52xit/index.html
[root@localhost ~]# vi /etc/exports
/opt/51xit 192.168.100.0/24(rw,sync)
/opt/52xit 192.168.100.0/24(rw,sync)
###最好先重新啟動rpcbind,不然有時會報錯
[root@localhost ~]# systemctl restart rpcbind
[root@localhost ~]# systemctl restart nfs
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# systemctl enable rpcbind
【1】設定虛擬IP地址
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vi ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.88
NETMASK=255.255.255.255
ONBOOT=yes
[root@localhost network-scripts]# ifup lo:0
[root@localhost network-scripts]# ifconfig
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 192.168.100.10 netmask 255.255.255.255
loop txqueuelen 1000 (Local Loopback)
##新增本地存取路由,這個是一個指令碼,相當於開機自動執行 route add -host 192.168.100.88 dev lo:0
[root@localhost network-scripts]# vi /etc/rc.local
/sbin/route add -host 192.168.100.88 dev lo:0
##手動先敲,敲完不能restart network,不然這個本地路由會消失,你需要重新新增
[root@localhost network-scripts]# route add -host 192.168.100.88 dev lo:0
[root@localhost network-scripts]# route -n
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.100.1 0.0.0.0 UG 100 0 0 ens33
192.168.100.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.100.88 0.0.0.0 255.255.255.255 UH 0 0 lo
【2】調整/proc響應引數
[root@localhost network-scripts]# vi /etc/sysctl.conf
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@localhost network-scripts]# sysctl -p
【3】安裝httpd 掛載測試頁
[root@localhost ~]# showmount -e 192.168.100.21
Export list for 192.168.100.21:
/opt/52xit 192.168.100.0/24
/opt/51xit 192.168.100.0/24
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# mount 192.168.100.21:/opt/51xit /var/www/html/
[root@localhost ~]# vi /etc/fstab
192.168.100.21:/opt/51xit /var/www/html/ nfs defaults,_netdev 0 0
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
#登入192.168.100.24測試網站是否正常#
【1】設定虛擬IP地址
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vi ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.88
NETMASK=255.255.255.255
ONBOOT=yes
[root@localhost network-scripts]# ifup lo:0
[root@localhost network-scripts]# ifconfig
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 192.168.100.10 netmask 255.255.255.255
loop txqueuelen 1000 (Local Loopback)
[root@localhost network-scripts]# vi /etc/rc.local
/sbin/route add -host 192.168.100.10 dev lo:0
[root@localhost network-scripts]# route add -host 192.168.100.88 dev lo:0
[root@localhost network-scripts]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.100.1 0.0.0.0 UG 100 0 0 ens33
192.168.100.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.100.88 0.0.0.0 255.255.255.255 UH 0 0 0 lo
【2】調整/proc響應引數
[root@localhost network-scripts]# vi /etc/sysctl.conf
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@localhost network-scripts]# sysctl -p
【2】安裝httpd 掛載測試頁
[root@localhost ~]# showmount -e 192.168.100.21
Export list for 192.168.100.44:
/opt/accp (everyone)
/opt/bdqn (everyone)
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# mount 192.168.100.21:/opt/52xit /var/www/html/
[root@localhost ~]# vi /etc/fstab
192.168.100.21:/opt/52xit /var/www/html/ nfs defaults,_netdev 0 0
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
#登入192.168.100.43測試網站是否正常#
####RPC--註冊問題###
[root@localhost ~]# showmount -e
clnt_create: RPC: Program not registered
解決方案:
[root@localhost ~]# systemctl stop nfs
[root@localhost ~]# systemctl stop rpcbind
[root@localhost ~]# systemctl start rpcbind ###再次啟動
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# showmount -e
Export list for localhost.localdomain:
/opt/52xit 20.0.0.0/24
/opt/51xit 20.0.0.0/24
######問題: /etc/sysconfig/ipvsadm: No such ...ry####
解決方案:ipvsadm --save > /etc/sysconfig/ipvsadm
[root@localhost opt]# systemctl status ipvsadm.service ###檢視ipvsadm是否工作正常
● ipvsadm.service - Initialise the Linux Virtual Server
Loaded: loaded (/usr/lib/systemd/system/ipvsadm.service; enabled; vendor preset: disabled)
Active: active (exited) since Mon 2020-09-21 23:47:17 EDT; 19min ago
Process: 18092 ExecStart=/bin/bash -c exec /sbin/ipvsadm-restore <
/etc/sysconfig/ipvsadm (code=exited, status=0/SUCCESS)
Main PID: 18092 (code=exited, status=0/SUCCESS)
CGroup: /system.slice/ipvsadm.service
#####問題:TCP 127.0.0.1:80 rr ###新增虛擬伺服器資料異常###
[root@localhost ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 127.0.0.1:80 rr ###新增虛擬伺服器資料異常
-> 192.168.100.21:80 Route 1 0 0
-> 192.168.100.22:80 Route 1 0 0
###如何解決,清空重新新增###
ipvsadm -C
ipvsadm -A -t 192.168.100.100:80 -s rr
ipvsadm -a -t 192.168.100.100:80 -r 192.168.100.22:80 -g -w 1
ipvsadm -a -t 192.168.100.100:80 -r 192.168.100.23:80 -g -w 1
ipvsadm --save > /etc/sysconfig/ipvsadm