環(huán)境:centos7.6
準(zhǔn)備四臺(tái)虛擬機(jī)
一臺(tái)lvs服務(wù)器
一臺(tái)客戶端
兩臺(tái)服務(wù)端
lvs ip:橋接 192.168.2.232
僅主機(jī) 192.168.18.137
客戶端:橋接192.168.2.138
服務(wù)端1:僅主機(jī)192.168.18.139
服務(wù)端2:僅主機(jī)192.168.18.134
所有虛擬機(jī)關(guān)閉防火墻,關(guān)閉selinux
在lvs服務(wù)器中
首先啟用ip_forward
[root@localhost ~]# echo net.ipv4.ip_forward=1 >> /etc/sysctl.conf
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1
[root@localhost ~]# sysctl -a | grep ip_f
sysctl: reading key “net.ipv6.conf.all.stable_secret”
sysctl: reading key “net.ipv6.conf.default.stable_secret”
net.ipv4.ip_forward = 1
net.ipv4.ip_forward_use_pmtu = 0
sysctl: reading key “net.ipv6.conf.ens33.stable_secret”
sysctl: reading key “net.ipv6.conf.ens36.stable_secret”
sysctl: reading key “net.ipv6.conf.lo.stable_secret”
在兩臺(tái)服務(wù)端加網(wǎng)關(guān),網(wǎng)關(guān)指向lvs服務(wù)器(在ifcif-ens33加三行)
服務(wù)器1
IPADDR=192.168.18.139
NETMASK=255.255.255.0
GATEWAY=192.168.18.137
服務(wù)器2
IPADDR=192.168.18.134
NETMASK=255.255.255.0
GATEWAY=192.168.18.137
在兩臺(tái)服務(wù)端安裝apache或者nginx這里我直接用系統(tǒng)自帶的apache
在兩臺(tái)服務(wù)端開啟apache服務(wù)
systemctl start httpd(服務(wù)器 1和2都開啟)
在兩臺(tái)服務(wù)器設(shè)置個(gè)內(nèi)容方便測試
echo server1 > /var/www/html/index.html(服務(wù)器1)
echo server2 > /var/www/html/index.html(服務(wù)器2)
在lvs服務(wù)器測試
[root@localhost ~]# curl 192.168.18.134
server1
[root@localhost ~]# curl 192.168.18.139
server2
在lvs服務(wù)器開啟調(diào)度
定義ipvs規(guī)則
[root@localhost ~]# ipvsadm -A -t 192.168.2.232:80 -s 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 192.168.2.232:80 rr
加后端熱server
[root@localhost ~]# ipvsadm -a -t 192.168.2.232:80 -r 192.168.18.134 -m
[root@localhost ~]# ipvsadm -a -t 192.168.2.232:80 -r 192.168.18.139 -m
[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 192.168.2.232:80 rr
-> 192.168.18.134:80 Masq 1 0 0
-> 192.168.18.139:80 Masq 1 0 0
在客戶端測試
[root@localhost ~]# curl 192.168.2.232
server2
[root@localhost ~]# curl 192.168.2.232
server1
注意:兩臺(tái)僅主機(jī)模式的服務(wù)端在配置ip時(shí)必須用lvs服務(wù)器上的僅主機(jī)模式IP地址作為網(wǎng)關(guān)
lvs服務(wù)器不需要開啟80端口
tail -f /var/log/httpd/access_log查看訪問日志