[root@localhost network-scripts]# nmcli connection //檢視UUID號,繫結網路卡
NAME UUID TYPE DEVICE
ens33 e700afd3-d9d3-4e75-ba0f-1c472d256ce8 ethernet ens33
virbr0 ec8ec847-c505-4e0a-ac6c-a9d1131d768c bridge virbr0
Wired connection 1 032bc5ee-d4a0-3374-a2a1-8b3079c39070 ethernet ens36
[root@localhost network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 20.0.0.11 netmask 255.255.255.0 broadcast 20.0.0.255
……省略部分
ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.30.11 netmask 255.255.255.0 broadcast 192.168.30.255
……省略部分
[root@localhost ~]# yum -y install ipvsadm
[root@localhost ~]# ipvsadm -v //檢視ipvsadm資訊
ipvsadm v1.27 2008/5/15 (compiled with popt and IPVS v1.2.1)
[root@localhost ~]# modprobe ip_vs //載入ip_vs功能,確認核心對ip_vs的支援
[root@localhost ~]# cat /proc/net/ip_vs //檢視資訊
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
###############建立虛擬伺服器規則################################
[root@localhost ~]# ipvsadm -A -t 20.0.0.11:80 -s rr
##############新增Web伺服器節點規則##############################
[root@localhost ~]# ipvsadm -a -t 20.0.0.11:80 -r 192.168.30.22 -m -w 1
[root@localhost ~]# ipvsadm -a -t 20.0.0.11:80 -r 192.168.30.33 -m -w 1
##############儲存規則,並記錄到/opt/ipvsadm檔案中###############
[root@localhost ~]# ipvsadm-save > /opt/ipvsadm
[root@localhost ~]# cat /opt/ipvsadm
-A -t localhost.localdomain:http -s rr
-a -t localhost.localdomain:http -r 192.168.30.22:http -g -w 1
-a -t localhost.localdomain:http -r 192.168.30.33:http -g -w 1
###############做路由轉發####################################
[root@localhost ~]# vi /etc/sysctl.conf
net.ipv4.ip_forward=1
[root@localhost ~]# sysctl -p //檢視轉發資訊
net.ipv4.ip_forward = 1
#################如果有iptables防火牆,需要做SNAT##############
[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.30.0/24 -o ens33 -j SNAT --to-source 20.0.0.11
#########################################.#######################
[root@mysql2 ~]# ifconfig //檢視ip
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.30.22 netmask 255.255.255.0 broadcast 192.168.30.255
[root@mysql2 ~]# route -n //檢視路由表,看是否設定閘道器
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.30.11 0.0.0.0 UG 100 0 0 ens33
192.168.30.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@localhost ~]# yum -y install nfs-utils //使用showmount需要安裝這個工具
[root@localhost ~]# showmount -e 192.168.30.44 //檢視共用狀況
Export list for 192.168.30.44:
/opt/web2 192.168.30.0/24
/opt/web1 192.168.30.0/24
[root@mysql2 ~]# yum -y install httpd //安裝httpd
[root@mysql2 ~]# systemctl start httpd
[root@mysql2 ~]# systemctl enable httpd
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.30.33 netmask 255.255.255.0 broadcast 192.168.30.255
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.30.11 0.0.0.0 UG 100 0 0 ens33
192.168.30.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@localhost ~]# yum -y install nfs-utils
[root@localhost ~]# showmount -e 192.168.30.44 //若檢視不到,可能是nfs伺服器釋出失敗,去nfs伺服器再次釋出一下:exportsfs
Export list for 192.168.30.44:
/opt/web2 192.168.30.0/24
/opt/web1 192.168.30.0/24
[root@mysql2 ~]# yum -y install httpd
[root@mysql2 ~]# systemctl start httpd
[root@mysql2 ~]# systemctl enable httpd
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.30.44 netmask 255.255.255.0 broadcast 192.168.30.255
inet6 fe80::a52a:406e:6512:1c66 prefixlen 64 scopeid 0x20<link>
[root@localhost ~]# route -n //檢視路由表,看閘道器
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.30.11 0.0.0.0 UG 100 0 0 ens33
192.168.30.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@localhost ~]# rpm -q nfs-utils //檢視nfs是否安裝
nfs-utils-1.3.0-0.61.el7.x86_64
[root@localhost ~]# rpm -q rpcbind //檢視rpcbind是否安裝
rpcbind-0.2.0-47.el7.x86_64
[root@localhost ~]# yum -y install nfs-utils //確實安裝了
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Package 1:nfs-utils-1.3.0-0.61.el7.x86_64 already installed and latest version
Nothing to do
[root@localhost ~]# yum -y install rpcbind
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Package rpcbind-0.2.0-47.el7.x86_64 already installed and latest version
Nothing to do
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# vi /etc/exports
/opt/web1 192.168.30.0/24(rw,sync)
/opt/web2 192.168.30.0/24(rw,sync)
[root@localhost ~]# systemctl restart nfs
[root@localhost ~]# systemctl restart rpcbind
[root@localhost ~]# showmount -e
Export list for localhost.localdomain:
/opt/web2 192.168.30.0/24
/opt/web1 192.168.30.0/24
[root@localhost web2]# exportfs -vr
exporting 192.168.30.0/24:/opt/web2
exporting 192.168.30.0/24:/opt/web1
[root@localhost ~]# mkdir /opt/web1/ /opt/web1/
[root@localhost ~]# vi /opt/web1/index.html
<html>
<title>I'm Web1</title>
<body><h1>I'm Web1</h1></body>
<img src="web1.jpg" />
</html>
[root@localhost ~]# vi /opt/web2/index.html
<html>
<title>I'm Web2</title>
<body><h1>I'm Web2</h1></body>
<img src="web2.png" />
</html>
登入http://192.168.30.22,測試正常
登入http://192.168.30.33,測試正常
登入http://20.0.0.11,發現輪流從web1和web2伺服器中讀取資料,第一次存取是web1,間隔1分鐘重新整理(因為存在快取),存取的就是web2。輪詢正常
LVS的負載排程演演算法有四種最常用的:輪詢演演算法(rr)、加權輪詢(wrr)、最少輪詢(Ic)、加權最少輪詢(wlc)
群集的VIP地址為20.0.0.11,針對TCP 80埠提供負載分流服務,使用的輪詢排程演演算法。對於負載均衡排程器來說,VIP必須是本機實際已啟用的IP地址
ipvsadm -A -t 20.0.0.11:80 -s rr //選項"-A"表示新增虛擬伺服器,"-t"用來指定VIP地址及TCP埠,"-S"用來指定負載排程演演算法——rr、wrr、lc、wlc 。
ipvsadm -a -t 20.0.0.11:80 -r 192.168.80.33:80 -m
ipvsadm -a -t 20.0.0.11:80 -r 192.168.80.44:80 -m
//選項"-a"表示新增真實伺服器,」-t」用來指定VIP地址及TCP埠,"-r"用來指定RIP地址及TCP埠,"-m"表示使用NAT群集模式("-g"是DR模式,"-i"是TUN橫式)
{-m引數後面還可以跟-w的引數,這裡沒有做的"-w"用來設定權重(權重為0時表示暫停節點)}
ipvsadm -d -r 192.168.30.22:8o -t 20.0.0.11:80 //需要從伺服器池中刪除某一個節點時,使用選項"-d"」。執行刪除操作必須指定目標物件,包括節點地址、虛擬IP地址。如上所示的操作將會刪除LVS群集20.0.0.11中的節點192.168.30.22
若需要刪除整個虛擬伺服器時,使用選項-D並指定虛擬IP地址即可,無需要指定節點。例如:「ipvsadm -D -t 20.0.0.11:80",則刪除此虛擬服務囂。
ipvsadm -L //檢視節點狀態,加個"-n"將以數位形式顯示地址、埠資訊
ipvsadm-save > /etc/sysconfig/ipvsadm1/儲存策略
使用匯出/匯入工具ipvsadm-save/ipvsadm-restore可以儲存、恢復LVS策略,方法類似於iptables的規則的匯出、匯入
(可以利用ipvsadm-restore < /dev/null 來清空所有規則)