試驗需求三臺機器:html
分發器,也叫調度器(簡寫爲dir) : 192.168.112.136 ying01linux
rs1 :192.168.112.138 ying02算法
rs2 :192.168.112.139 ying03vim
vip :192.168.112.200瀏覽器
新建lvs_dr腳本,按下面內容配置bash
[root@ying01 ~]# vim /usr/local/sbin/lvs_dr.sh #! /bin/bash echo 1 > /proc/sys/net/ipv4/ip_forward ipv=/usr/sbin/ipvsadm vip=192.168.112.200 rs1=192.168.112.138 rs2=192.168.112.139 #注意這裏的網卡名字 ifdown wns33 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
執行腳本,查看IP,發現vip在ens33上服務器
[root@ying01 ~]# sh /usr/local/sbin/lvs_dr.sh 成功斷開設備 'ens33'。 鏈接已成功激活(D-Bus 活動路徑:/org/freedesktop/NetworkManager/ActiveConnection/3) [root@ying01 ~]# 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:87:3f:91 brd ff:ff:ff:ff:ff:ff inet 192.168.112.136/24 brd 192.168.112.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.112.200/32 brd 192.168.112.200 scope global ens33:2 valid_lft forever preferred_lft forever inet 192.168.112.158/24 brd 192.168.112.255 scope global secondary ens33:0 valid_lft forever preferred_lft forever inet6 fe80::16dc:89c:b761:e115/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:87:3f:9b brd ff:ff:ff:ff:ff:ff inet 192.168.24.128/24 brd 192.168.24.255 scope global dynamic ens37 valid_lft 1434sec preferred_lft 1434sec inet6 fe80::ad38:a02e:964e:1b93/64 scope link valid_lft forever preferred_lft forever
先把網關改成:192.168.112.2,重啓網絡網絡
[root@ying02 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 GATEWAY=192.168.112.2 [root@ying02 ~]# systemctl restart network
新建lvs_rs腳本;架構
[root@ying02 ~]# vim /usr/local/sbin/lvs_rs.sh #/bin/bash vip=192.168.112.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
執行腳本,並route -n ,查看網絡信息oop
[root@ying02 ~]# sh /usr/local/sbin/lvs_rs.sh [root@ying02 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.112.2 0.0.0.0 UG 100 0 0 ens33 192.168.112.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 192.168.112.200 0.0.0.0 255.255.255.255 UH 0 0 0 lo [root@ying02 ~]# 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.112.200/32 brd 192.168.112.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:c6:2c:24 brd ff:ff:ff:ff:ff:ff inet 192.168.112.138/24 brd 192.168.112.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::964f:be22:ddf2:54b7/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:c6:2c:2e brd ff:ff:ff:ff:ff:ff
先把網關改成:192.168.112.2,重啓網絡
[root@ying03 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 [root@ying03 ~]# systemctl restart network
新建lvs_rs腳本;
[root@ying03 ~]# vim /usr/local/sbin/lvs_rs.sh #/bin/bash vip=192.168.112.200 ifdown lo ifup lo #把vip綁定在lo上,是爲了實現rs直接把結果返回給客戶端 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
執行腳本,vip在lo網卡上
[root@ying03 ~]# sh /usr/local/sbin/lvs_rs.sh [root@ying03 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.112.2 0.0.0.0 UG 100 0 0 ens33 192.168.112.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 192.168.112.200 0.0.0.0 255.255.255.255 UH 0 0 0 lo [root@ying03 ~]# 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.112.200/32 brd 192.168.112.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:ed:0f:50 brd ff:ff:ff:ff:ff:ff inet 192.168.112.139/24 brd 192.168.112.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::43bd:36bd:3f01:f8e8/64 scope link valid_lft forever preferred_lft forever inet6 fe80::964f:be22:ddf2:54b7/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:ed:0f:5a brd ff:ff:ff:ff:ff:ff
在瀏覽器測試,輸入VIP:192.168.112.100;不斷刷新瀏覽器;會出現ying02或者ying03頁面
經過ipvsadm -ln查看 規則,能夠看出其信息;
[root@ying01 ~]# 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.112.200:80 wrr -> 192.168.112.138:80 Route 1 2 9 -> 192.168.112.139:80 Route 1 2 9
編輯配置文件,把以前的內容清空,按下面配置
[root@ying01 ~]# vim /etc/keepalived/keepalived.conf vrrp_instance VI_1 { #備用服務器上爲 BACKUP state MASTER #綁定vip的網卡爲ens33 interface ens33 virtual_router_id 51 #備用服務器上爲90 priority 100 advert_int 1 authentication { auth_type PASS auth_pass aminglinux } virtual_ipaddress { 192.168.112.200 } } virtual_server 192.168.112.200 80 { #(每隔10秒查詢realserver狀態) delay_loop 10 #(lvs 算法) lb_algo wlc #(DR模式) lb_kind DR #(同一IP的鏈接60秒內被分配到同一臺realserver) persistence_timeout 60 #(用TCP協議檢查realserver狀態) protocol TCP real_server 192.168.112.138 80 { #(權重) weight 100 TCP_CHECK { #(10秒無響應超時) connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.112.139 80 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
查看網卡IP
[root@ying01 ~]# 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:87:3f:91 brd ff:ff:ff:ff:ff:ff inet 192.168.112.136/24 brd 192.168.112.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.112.158/24 brd 192.168.112.255 scope global secondary ens33:0 valid_lft forever preferred_lft forever inet6 fe80::16dc:89c:b761:e115/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:87:3f:9b brd ff:ff:ff:ff:ff:ff inet 192.168.24.128/24 brd 192.168.24.255 scope global dynamic ens37 valid_lft 1216sec preferred_lft 1216sec inet6 fe80::ad38:a02e:964e:1b93/64 scope link valid_lft forever preferred_lft forever
ipvsadm -ln :查看其規則,開啓與未開啓keepalived服務對比;
[root@ying01 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn [root@ying01 ~]# systemctl start keepalived [root@ying01 ~]# 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.112.200:80 wlc persistent 60 -> 192.168.112.138:80 Route 100 0 0 -> 192.168.112.139:80 Route 100 0 0
開展執行其腳本;作實驗,不斷刷新瀏覽器;
[root@ying01 ~]# sh /usr/local/sbin/lvs_dr.sh 成功斷開設備 'ens33'。 鏈接已成功激活(D-Bus 活動路徑:/org/freedesktop/NetworkManager/ActiveConnection/7) [root@ying01 ~]# 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.112.200:80 wrr -> 192.168.112.138:80 Route 1 0 3 -> 192.168.112.139:80 Route 1 0 3
假如ying03宕機了,咱們把ying03機器關閉;此時可以剔除ying03機器;
[root@ying01 ~]# 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.112.200:80 wrr -> 192.168.112.138:80 Route 1 3 20 您在 /var/spool/mail/root 中有新郵件
恢復ying03機器,規則裏面有添加上ying03
[root@ying01 ~]# 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.112.200:80 wrr -> 192.168.112.138:80 Route 1 3 20 -> 192.168.112.139:80 Route 100 1 1