Keepalive高可用部署
阿新 • • 發佈:2021-11-03
準備環境:兩臺機器,機器上提前裝好nginx。
- 第一種方法
-
安裝keepalive,如無特殊要求,直接yum安裝即可。
# ct1 [root@ct1 ~]# yum install -y keepalived # ct2 [root@ct2 ~]# yum install -y keepalived
-
配置檔案
# ct1 master 配置檔案 ! Configuration File for keepalived global_defs { router_id LVS_DEVEL } vrrp_script check_nginx { script "/etc/keepalived/check_nginx.sh" interval 3 } vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.174.100 } track_script { check_nginx } } # ct2 backup配置檔案 ! Configuration File for keepalived global_defs { router_id LVS_DEVEL } vrrp_script check_nginx { script "/etc/keepalived/check_nginx.sh" interval 3 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.174.100 } track_script { check_nginx } }
檢測指令碼:
# check_nginx.sh 賦予執行許可權 #! /bin/bash num=`ps -ef |grep "nginx: master process" | grep -v "grep" | wc -l` if [ $num -lt 1 ];then systemctl stop keepalived fi
-
啟動keepalive
systemctl start keepalived
-
檢視虛IP
# ct1 [root@ct1 keepalived]# ip a ... 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:96:93:a5 brd ff:ff:ff:ff:ff:ff inet 192.168.174.7/24 brd 192.168.174.255 scope global noprefixroute dynamic ens33 valid_lft 80398sec preferred_lft 80398sec inet 192.168.174.100/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::35f1:e90f:4400:48e3/64 scope link noprefixroute valid_lft forever preferred_lft forever # ct2 [root@ct2 keepalived]# ip a ... 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:cc:c1:4a brd ff:ff:ff:ff:ff:ff inet 192.168.174.8/24 brd 192.168.174.255 scope global noprefixroute dynamic ens33 valid_lft 80394sec preferred_lft 80394sec inet6 fe80::35f1:e90f:4400:48e3/64 scope link tentative noprefixroute dadfailed valid_lft forever preferred_lft forever inet6 fe80::8174:b896:90e9:17a3/64 scope link noprefixroute valid_lft forever preferred_lft forever
-
關閉ct1的nginx,測試虛IP切換,可以看到虛IP切換到了ct2上了。
# ct1 [root@ct1 keepalived]# ip a ... 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:96:93:a5 brd ff:ff:ff:ff:ff:ff inet 192.168.174.7/24 brd 192.168.174.255 scope global noprefixroute dynamic ens33 valid_lft 79911sec preferred_lft 79911sec inet6 fe80::35f1:e90f:4400:48e3/64 scope link noprefixroute valid_lft forever preferred_lft forever # ct2 [root@ct2 keepalived]# ip a ... 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:cc:c1:4a brd ff:ff:ff:ff:ff:ff inet 192.168.174.8/24 brd 192.168.174.255 scope global noprefixroute dynamic ens33 valid_lft 79909sec preferred_lft 79909sec inet 192.168.174.100/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::35f1:e90f:4400:48e3/64 scope link tentative noprefixroute dadfailed valid_lft forever preferred_lft forever inet6 fe80::8174:b896:90e9:17a3/64 scope link noprefixroute valid_lft forever preferred_lft forever
-
恢復ct1服務,可以看到ct1恢復後,由於優先順序高,虛IP又切換回來了
# ct1 [root@ct1 keepalived]# ip a ... 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:96:93:a5 brd ff:ff:ff:ff:ff:ff inet 192.168.174.7/24 brd 192.168.174.255 scope global noprefixroute dynamic ens33 valid_lft 79787sec preferred_lft 79787sec inet 192.168.174.100/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::35f1:e90f:4400:48e3/64 scope link noprefixroute valid_lft forever preferred_lft forever # ct2 [root@ct2 keepalived]# ip a ... 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:cc:c1:4a brd ff:ff:ff:ff:ff:ff inet 192.168.174.8/24 brd 192.168.174.255 scope global noprefixroute dynamic ens33 valid_lft 79784sec preferred_lft 79784sec inet6 fe80::35f1:e90f:4400:48e3/64 scope link tentative noprefixroute dadfailed valid_lft forever preferred_lft forever inet6 fe80::8174:b896:90e9:17a3/64 scope link noprefixroute valid_lft forever preferred_lft forever
- 第二種方法
# ct1 配置檔案
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.174.100
}
}
virtual_server 192.168.174.100 80 { # 虛擬IP 埠
delay_loop 2
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.174.8 80 { # 機器真實IP 埠
weight 3
notify_down /etc/keepalived/kill.sh
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
# ct2 配置檔案
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.174.100
}
}
virtual_server 192.168.174.100 80 {
delay_loop 2
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.174.8 80 {
weight 3
notify_down /etc/keepalived/kill.sh
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
/etc/keepalived/kill.sh檔案內容:
# 注意賦予執行許可權
#! /bin/bash
systemctl stop keepalived
驗證,檢視虛IP:
[root@ct1 keepalived]# ip a
...
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:96:93:a5 brd ff:ff:ff:ff:ff:ff
inet 192.168.174.7/24 brd 192.168.174.255 scope global noprefixroute dynamic ens33
valid_lft 69302sec preferred_lft 69302sec
inet 192.168.174.100/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::35f1:e90f:4400:48e3/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@ct2 keepalived]# ip a
...
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:cc:c1:4a brd ff:ff:ff:ff:ff:ff
inet 192.168.174.8/24 brd 192.168.174.255 scope global noprefixroute dynamic ens33
valid_lft 69301sec preferred_lft 69301sec
inet6 fe80::35f1:e90f:4400:48e3/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::8174:b896:90e9:17a3/64 scope link noprefixroute
valid_lft forever preferred_lft forever
關閉ct1 的nginx服務,檢視虛IP是否切換:
[root@ct1 keepalived]# ip a
...
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:96:93:a5 brd ff:ff:ff:ff:ff:ff
inet 192.168.174.7/24 brd 192.168.174.255 scope global noprefixroute dynamic ens33
valid_lft 69081sec preferred_lft 69081sec
inet6 fe80::35f1:e90f:4400:48e3/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@ct2 keepalived]# ip a
...
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:cc:c1:4a brd ff:ff:ff:ff:ff:ff
inet 192.168.174.8/24 brd 192.168.174.255 scope global noprefixroute dynamic ens33
valid_lft 69077sec preferred_lft 69077sec
inet 192.168.174.100/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::35f1:e90f:4400:48e3/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::8174:b896:90e9:17a3/64 scope link noprefixroute
valid_lft forever preferred_lft forever
重新啟動ct1的服務,看是否恢復。
[root@ct1 keepalived]# ip a
...
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:96:93:a5 brd ff:ff:ff:ff:ff:ff
inet 192.168.174.7/24 brd 192.168.174.255 scope global noprefixroute dynamic ens33
valid_lft 68970sec preferred_lft 68970sec
inet 192.168.174.100/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::35f1:e90f:4400:48e3/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@ct2 keepalived]# ip a
...
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:cc:c1:4a brd ff:ff:ff:ff:ff:ff
inet 192.168.174.8/24 brd 192.168.174.255 scope global noprefixroute dynamic ens33
valid_lft 68968sec preferred_lft 68968sec
inet6 fe80::35f1:e90f:4400:48e3/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::8174:b896:90e9:17a3/64 scope link noprefixroute
valid_lft forever preferred_lft forever
上面的結果來看,部署成功。