18.11 LVS DR模式搭建
1 準備三臺機器
分發器(也叫調度器,簡寫為dir)192.168.189.128
rs1 192.168.189.129
rs2 192.168.189.130
每臺機器上(包括dir,所有rs)都設置vip 192.168.189.200
2 dir上編寫腳本
#vim /usr/local/sbin/lvs_dr.sh #! /bin/bash echo 1 > /proc/sys/net/ipv4/ip_forward ipv=/usr/sbin/ipvsadm vip=192.168.189.200 rs1=192.168.189.129 rs2=192.168.189.130 #註意這裏的網卡名字 ifdown ens33 ifup ens33 ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up route add -host $vip dev ens33:2 $ipv -C $ipv -A -t $vip:80 -s wrr $ipv -a -t $vip:80 -r $rs1:80 -g -w 1 $ipv -a -t $vip:80 -r $rs2:80 -g -w 1
參數解釋:
echo 1 > /proc/sys/net/ipv4/ip_forward 打開端口轉發
ipv=/usr/sbin/ipvsadm
vip=192.168.189.200
rs1=192.168.189.129
rs2=192.168.189.130
設置各參數的變量,變量代替ip
ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up
ifconfig設置一個網卡的虛擬網卡
綁定vip的命令配置,ens33:2
route add -host $vip dev ens33:2
設置網關
3 dir上啟動腳本
[root@centos7-01 ~]# sh !$
sh /usr/local/sbin/lvs_dr.sh
成功斷開設備 'ens33'。
連接已成功激活(D-Bus 活動路徑:/org/freedesktop/NetworkManager/ActiveConnection/17)
4 編寫rs上的腳本
兩臺rs上也編寫腳本 vim /usr/local/sbin/lvs_rs.sh//內容如下
註意rs1上的腳本名是lvs_rs1.sh,rs2上的腳本名是lvs_rs2.sh
rs1上,
[root@centos7-02 ~]# vim /usr/local/sbin/lvs_rs.sh
#/bin/bash
vip=192.168.189.200
#把vip綁定在lo上,是為了實現rs直接把結果返回給客戶端
ifdown lo
ifup lo
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作為更改arp內核參數,目的是為了讓rs順利發送mac地址給客戶端
#參考文檔www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
rs2上,
[root@centos7-03 ~]# vim /usr/local/sbin/lvs_rs2.sh
#/bin/bash
vip=192.168.189.200
#把vip綁定在lo上,是為了實現rs直接把結果返回給客戶端
ifdown lo
ifup lo
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作為更改arp內核參數,目的是為了讓rs順利發送mac地址給客戶端
#參考文檔www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
參數解釋:
以下是更改arp內核參數,目的是為了讓rs順利發送mac地址給客戶端
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
5 檢查vip是否綁定了在rs1,rs2,dir上
dir,
[root@centos7-01 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:15:53:53 brd ff:ff:ff:ff:ff:ff
inet 192.168.189.128/24 brd 192.168.189.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.189.200/32 brd 192.168.189.200 scope global ens33:2
valid_lft forever preferred_lft forever
inet 192.168.189.150/24 brd 192.168.189.255 scope global secondary ens33:0
valid_lft forever preferred_lft forever
inet6 fe80::243c:86d7:d85e:224d/64 scope link
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:15:53:5d brd ff:ff:ff:ff:ff:ff
inet 192.168.149.128/24 brd 192.168.149.255 scope global dynamic ens37
valid_lft 1464sec preferred_lft 1464sec
inet 192.168.149.147/24 brd 192.168.149.255 scope global secondary ens37
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe15:535d/64 scope link
valid_lft forever preferred_lft forever
rs1,
[root@centos7-02 sbin]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet 192.168.189.200/32 brd 192.168.189.200 scope global lo:0
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:73:7c:4c brd ff:ff:ff:ff:ff:ff
inet 192.168.189.129/24 brd 192.168.189.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::165b:9d77:48be:464c/64 scope link
valid_lft forever preferred_lft forever
inet6 fe80::b485:96d0:c537:251e/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:73:7c:56 brd ff:ff:ff:ff:ff:ff
inet6 fe80::db6f:ae30:c2f3:81c5/64 scope link
valid_lft forever preferred_lft forever
rs2,
[root@centos7-03 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet 192.168.189.200/32 brd 192.168.189.200 scope global lo:0
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:46:b2:9c brd ff:ff:ff:ff:ff:ff
inet 192.168.189.130/24 brd 192.168.189.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::b485:96d0:c537:251e/64 scope link
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:46:b2:a6 brd ff:ff:ff:ff:ff:ff
6 準備工作完成,進行訪問vip測試。
在windows瀏覽器上進行訪問測試
由於windows有緩存的問題存在,在實驗上可能會影響實驗結果,所以只能嘗試在不同瀏覽器上進行訪問測試
6.1 在google chrome瀏覽器上訪問vip
截圖
轉到rs1上
6.2 在360瀏覽器上訪問vip
截圖
轉到rs2上
上面也實現了負載均衡的作用。
18.11 LVS DR模式搭建