LVS DR模式搭建、keepalived + LVS
DR模式搭建-準備工作
- 三臺機器
-
分發器,也叫調度器(簡寫為dir)
分發器:192.168.21.130/24
rs1:192.168.21.132/24
rs2:192.168.21.133/24
vip:192.168.21.200/24 - 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.21.200 rs1=192.168.21.132 rs2=192.168.21.133 #ifdown ifup 同等於重啟網卡,對虛擬網卡進行重置 #防止再次啟動腳本的時候,再次設置虛擬網卡的IP地址 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 rr $ipv -a -t $vip:80 -r $rs1:80 -g -w 1 $ipv -a -t $vip:80 -r $rs2:80 -g -w 1
- 啟動腳本
[root@qingyun-01 ~]# sh /usr/local/sbin/lvs_dr.sh
成功斷開設備 ‘ens33‘。
連接已成功激活(D-Bus 活動路徑:/org/freedesktop/NetworkManager/ActiveConnection/4)
- 兩臺rs上也編寫腳本 vim /usr/local/sbin/lvs_rs.sh //內容如下
#! /bin/bash vip=192.168.21.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
- 啟動腳本
[root@qingyun-02 ~]# sh !$
sh /usr/local/sbin/lvs_rs.sh
- 查看路由
- 測試
[root@qingyun-01 ~]# 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.21.200:80 rr -> 192.168.21.132:80 Route 1 0 0 -> 192.168.21.133:80 Route 1 0 0
18.12 keepalived + LVS
Keepalived + LVS DR
- 完整架構需要兩臺服務器(角色為dir)分別安裝keepalived軟件,目的是實現高可用,但keepalived本身也有負載均衡的功能,所以本次實驗可以只安裝一臺keepalived
[root@qingyun-01 ~]# yum install -y keepalived
- keepalived內置了ipvsadm的功能,所以不需要再安裝ipvsadm包,也不用編寫和執行那個lvs_dir的腳本
- 三臺機器分別為:
- dir(安裝keepalived)192.168.21.130/24
- rs1 192.168.21.132/24
- rs2 192.168.21.133/24
- vip 192.168.21.200/32
- 編輯keepalived配置文件 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.21.200
}
}
virtual_server 192.168.21.200 80 {
#(每隔10秒查詢realserver狀態)
delay_loop 10
#(lvs 算法)
lb_algo wlc
#(DR模式)
lb_kind DR
#(同一IP的連接60秒內被分配到同一臺realserver)
persistence_timeout 0
#(用TCP協議檢查realserver狀態)
protocol TCP
real_server 192.168.21.132 80 {
#(權重)
weight 100
TCP_CHECK {
#(10秒無響應超時)
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.21.133 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
- 啟動 keepalived
[root@qingyun-01 etc]# systemctl start keepalived
[root@qingyun-01 etc]# ps aux |grep keepalived
root 1615 0.0 0.1 120740 1408 ? Ss 17:23 0:00 /usr/sbin/keepalived -D
root 1616 0.0 0.3 127480 3336 ? S 17:23 0:00 /usr/sbin/keepalived -D
root 1617 0.0 0.2 131780 2944 ? S 17:23 0:00 /usr/sbin/keepalived -D
root 1623 0.0 0.0 112676 976 pts/0 R+ 17:25 0:00 grep --color=auto keepalived
- 執行ipvsadm -C 把之前的ipvsadm規則清空掉
- 兩臺rs上,依然要執行/usr/local/sbin/lvs_rs.sh腳本
腳本在DR模式搭建小節 - keepalived有一個比較好的功能,可以在一臺rs宕機時,不再把請求轉發過去
- 測試
[root@qingyun-01 etc]# systemctl stop keepalived [root@qingyun-01 etc]# 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:a3:53:bf brd ff:ff:ff:ff:ff:ff inet 192.168.21.130/24 brd 192.168.21.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::7d:8138:e13a:91e5/64 scope link valid_lft forever preferred_lft forever [root@qingyun-01 etc]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@qingyun-01 etc]# systemctl start keepalived
[root@qingyun-01 etc]# 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.21.200:80 wlc
-> 192.168.21.132:80 Route 100 0 0
-> 192.168.21.133:80 Route 100 0 0
#關掉 rs2
[root@qingyun-03 ~]# systemctl stop nginx
root@qingyun-01 etc]# 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.21.200:80 wlc
-> 192.168.21.132:80 Route 100 0 0
#有間隔10s
LVS DR模式搭建、keepalived + LVS