Keepalived+LVS DR
- 完整架構須要兩臺服務器(角色爲dir)分別安裝keepalived軟件,目的是實現高可用,但keepalived自己也有負載均衡的功能,因此本次實驗能夠只安裝一臺keepalived
- keepalived內置了ipvsadm的功能,因此不須要再安裝ipvsadm包,也不用編寫和執行那個lvs_dir的腳本
- 三臺機器分別爲:
- dir(安裝keepalived)133.130
- rs1 133.132
- rs2 133.133
- vip 133.200
- 編輯keepalived配置文件 vim /etc/keepalived/keepalived.conf//內容地址
- 須要更改裏面的ip信息
- 執行ipvsadm -C 把以前的ipvsadm規則清空掉
- systemctl restart network 能夠把以前的vip清空掉
- 兩臺rs上,依然要執行/usr/local/sbin/lvs_rs.sh腳本
- keepalived有一個比較好的功能,能夠在一臺rs宕機時,再也不把請求轉發過去
- 測試
Keepalived+LVS DR
- 完整架構須要兩臺服務器(角色爲dir)分別安裝keepalived軟件,目的是實現高可用,但keepalived自己也有負載均衡的功能,因此本次實驗能夠只安裝一臺keepalived
- 爲何須要把keepalived 加到lvs 中的目的是什麼?
- 緣由一:lvs,它有個關鍵角色,就是dir分發器,若是分發器宕掉,那全部的訪問就會被終止,由於全部的入口全都在dir分發器上,因此須要把分發器作一個高可用,用keepalived實現高可用,而且keepalived還有負載均衡的做用。
- 緣由二:在使用lvs的時候,若是沒有額外的操做,這時將一個rs機器關機(宕機)時,lvs照樣會分發數據到這臺宕機機器,這是就會出現訪問無效的狀況,說明lvs並不聰明;這時使用keepalived,就能夠保證集羣中其中一臺rs宕機了,web還能正常提供,不會出現用戶訪問時無效連接的結果;通常這種架構,確定是2臺keepalived;
- 由於keepalived內置了ipvsadm的功能,因此再也不須要安裝ipvsadm的包,也不用再編寫和執行.sh腳本
準備工做
- 準備三臺機器,分別爲
- dir(安裝keepalived)74.129
- rs1 74.131
- rs2 74.133
- vip 74.200
- 在dir分發器(A機器)上,清空ipvsadm規則,並查看ipvsadm規則,會發現已經清空
[root@hf-01 ~]# ipvsadm -C
[root@hf-01 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@hf-01 ~]#
- 在分發器(即A機器)上編輯配置文件,在/etc/keepalived/keepalived.conf 配置,配置文件內容
- 由於以前作實驗裏面編輯過配置文件,這時直接刪除,而後粘貼新的配置文件
- 修改配置文件中的網卡、vip ,還有rs機器上的IP
[root@hf-01 ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
#備用服務器上爲 BACKUP
state MASTER
#綁定vip的網卡爲ens33,你的網卡和阿銘的可能不同,這裏須要你改一下
interface ens36
virtual_router_id 51
#備用服務器上爲90
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass aminglinux
}
virtual_ipaddress {
192.168.74.200 //vip 地址
}
}
virtual_server 192.168.74.200 80 { //vip 地址
#(每隔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.74.131 80 { //rs1機器
#(權重)
weight 100
TCP_CHECK {
#(10秒無響應超時)
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.74.133 80 { //rs2機器
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
保存退出
- 啓動nginx服務,查看nginx進程,查看keepalived服務
[root@hf-01 ~]# systemctl start nginx
[root@hf-01 ~]# ps aux |grep nginx
root 2952 0.0 0.2 123372 2104 ? Ss 06:55 0:00 nginx: master process /usr/sbin/nginx
nginx 2953 0.0 0.3 123836 3588 ? S 06:55 0:00 nginx: worker process
root 2994 0.0 0.0 112672 980 pts/0 R+ 07:12 0:00 grep --color=auto nginx
[root@hf-01 ~]# ps aux |grep keep
root 3006 0.0 0.1 121324 1404 ? Ss 07:16 0:00 /usr/sbin/keepalived -D
root 3007 0.0 0.2 121448 2732 ? S 07:16 0:00 /usr/sbin/keepalived -D
root 3008 0.0 0.2 121324 2336 ? S 07:16 0:00 /usr/sbin/keepalived -D
root 3014 0.0 0.0 112672 984 pts/0 R+ 07:16 0:00 grep --color=auto keep
[root@hf-01 ~]#
- 查看IP,會看到虛擬IP依然存在
[root@hf-01 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
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: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ff:fe:93 brd ff:ff:ff:ff:ff:ff
inet 192.168.74.129/24 brd 192.168.74.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet 192.168.74.150/24 brd 192.168.74.255 scope global secondary eno16777736:0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:feff:fe93/64 scope link
valid_lft forever preferred_lft forever
3: ens36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ff:fe:9d brd ff:ff:ff:ff:ff:ff
inet 192.168.74.129/24 brd 192.168.74.255 scope global ens36
valid_lft forever preferred_lft forever
inet 192.168.74.200/32 brd 192.168.74.200 scope global ens36:2
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:feff:fe9d/64 scope link
valid_lft forever preferred_lft forever
[root@hf-01 ~]#
- 查看ipvsadm規則
[root@hf-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.74.200:80 wlc persistent 60
-> 192.168.74.131:80 Route 100 0 0
-> 192.168.74.133:80 Route 100 0 0
[root@hf-01 ~]#
- 這時關閉keepalived服務,再來查看ip,會到虛擬IP停掉了
[root@hf-01 ~]# systemctl stop keepalived
[root@hf-01 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
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: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ff:fe:93 brd ff:ff:ff:ff:ff:ff
inet 192.168.74.129/24 brd 192.168.74.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet 192.168.74.150/24 brd 192.168.74.255 scope global secondary eno16777736:0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:feff:fe93/64 scope link
valid_lft forever preferred_lft forever
3: ens36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ff:fe:9d brd ff:ff:ff:ff:ff:ff
inet 192.168.74.129/24 brd 192.168.74.255 scope global ens36
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:feff:fe9d/64 scope link
valid_lft forever preferred_lft forever
[root@hf-01 ~]#
- 再來查看規則,會發現沒有啓動規則
[root@hf-01 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@hf-01 ~]#
- 這時啓動keepalived,再來查看規則
[root@hf-01 ~]# systemctl start keepalived
[root@hf-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.74.200:80 wlc persistent 60
-> 192.168.74.131:80 Route 100 0 0
-> 192.168.74.133:80 Route 100 0 0
[root@hf-01 ~]#
- 注意事項:兩點
echo 1 > /proc/sys/net/ipv4/ip_forward //打開端口轉發
-
- 在rs機器上建立的/usr/local/sbin/lvs_rs.sh腳本,依然要執行它
#把vip綁定在lo上,是爲了實現rs直接把結果返回給客戶端
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#如下操做爲更改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
總結
- keepalived 有一個比較好的功能,能夠在一臺rs宕機的時候,及時把他踢出 ipvsadm 集羣,將再也不發送數據包給,也就很好的避免的訪問無鏈接的狀況發送