keepalived + nginx high availability

background

Under normal circumstances, the use of nginx load balancing, the back-end service availability, dynamic problems can be eliminated certain back-office services appear, it does not affect the normal operation of the system, but nginx is only one, this time is a bottleneck, there will be a single point of failure, nginx problems, the service will be interrupted, affecting the normal operation of the system.

 

keepalived solve the problem nginx single point

Keepalived failover using the transfer function, a plurality of nginx service deployment, such as two, external services a master, the other is in hot standby backup service, the external access through vip. When the master service problems, the machine hot standby backup services will take over the dynamic vip, vip after taking over the hot backup service will backup external services, which nginx single point of failure to solve the problem.

keepalived Configuration

[root@k8snode01 keepalived]# more keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL_01
}

vrrp_script chk_http_port {
   script "/etc/keepalived/check_ng.sh"
   interval 2
   weight -20
}

{VI_1 vrrp_instance
    State of the MASTER # preparation machine the BACKUP
    interface ens33
    virtual_router_id 100
    priority preparation machine 100 to # 50
    advert_int. 1
    authentication {
        AUTH_TYPE the PASS
        AUTH_PASS 1111
    }

notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"

track_script {
   chk_http_port
   }

virtual_ipaddress {
        192.168.23.200
    }
}

[root@k8snode01 keepalived]# 
[root@k8snode01 keepalived]# more check_ng.sh  #nginx检查脚本
#!/bin/bash
currdate=`date '+%Y-%m-%d %H:%M:%S'`
count=`ps -aux | grep -v grep | grep 'nginx.conf' | wc -l`
echo "${currdate}---->ok:${count}" >>/etc/keepalived/checkng.log
result=$(echo "000${count}" | grep "0000")
if [[ "$result" != "" ]]; then
    echo "${currdate}---->/etc/init.d/keepalived stop" >>/etc/keepalived/checkng.log
    /etc/init.d/keepalived stop
    exit 1
else
    exit 0
fi
[root@k8snode01 keepalived]# more notify.sh  #切换主备通知脚本
#!/bin/bash
currdate=`date '+%Y-%m-%d %H:%M:%S'`

echo "${currdate}---->${1}" >>/etc/keepalived/notify.log
[root@k8snode01 keepalived]# 

 

Failure drill

1)查看vip,vip在192.168.23.101机器上
[root@k8snode01 keepalived]# ip addr  #vip在192.168.23.101机器
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    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
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:a3:b8:9f brd ff:ff:ff:ff:ff:ff
    inet 192.168.23.101/24 brd 192.168.23.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.23.200/32 scope global ens33
       valid_lft forever preferred_lft forever
[root@k8snode01 keepalived]#

[root@k8snode02 ~]# ip addr #192.168.23.102机器无vip
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    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
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:32:05:d5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.23.102/24 brd 192.168.23.255 scope global ens33
       valid_lft forever preferred_lft forever
[root@k8snode02 ~]#

2) Use real ip access nginx, in order to facilitate the presentation, 192.168.23.101 return of nginx nginx 192.168.23.101,192.168.23.102 return 192.168.23.102
[root @ k8snode02 ~] # curl "192.168.23.101:8888" where # is returned nginx machine ip
192.168.23.101

[root @ k8snode02 ~] # curl "192.168.23.102:8888" # nginx returned the machine where ip
192.168.23.102

[root @ k8snode02 ~] #

3) using Nginx vip access, should return to normal real ip vip machine where, when return 192.168.23.101
[k8snode02 the root @ ~] # curl "192.168.23.200:8888" # vip return machine where ip
192.168.23.101

[the root k8snode02 ~ @] # curl "192.168.23.200:8888" # vip return the machine where ip
192.168.23.101

[root k8snode02 ~ @] # curl "192.168.23.200:8888" # vip return the machine where ip
192.168.23.101

[root @ k8snode02 ~] # 

4) Analog Master of nginx fault, can shut down or stop the machine 192.168.23.101 nginx service, the vip drift to 192.168.23.102, let nginx prepared to take over the service
[root @ k8snode01 keepalived] # PS -ef | grep process nginx nginx View #
? root 23081 1 0 08:11 00:00:00 nginx: Master Process / the Data / nginxinstall / sbin / nginx -c /data/nginxinstall/conf/nginx.conf
the nobody 23082 23081 0 08:11 00:00:00? nginx: process worker
root 28703 24828 0 08:30 PTS / 1 00:00:00 grep --color = Auto nginx
[root @ k8snode01 keepalived] # pkill # nginx nginx stop the process
[root @ k8snode01 keepalived] # PS -ef | grep nginx #nginx process has stopped
root 28746 24828 0 08:30 PTS / 1 00:00:00 grep --color = Auto nginx
[root @ k8snode01 keepalived] # ip addr # 192.168.23.101 machine vip disappear
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    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
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:a3:b8:9f brd ff:ff:ff:ff:ff:ff
    inet 192.168.23.101/24 brd 192.168.23.255 scope global ens33
       valid_lft forever preferred_lft forever
[root@k8snode01 keepalived]#

[root@k8snode02 ~]# ip addr #vip漂移到192.168.23.102
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    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
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:32:05:d5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.23.102/24 brd 192.168.23.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.23.200/32 scope global ens33
       valid_lft forever preferred_lft forever
[root@k8snode02 ~]#

[root @ k8snode02 ~] # curl "192.168.23.200:8888" # was taken over nginx192.168.23.102 normal service
192.168.23.102

[root @ k8snode02 ~] # curl "192.168.23.200:8888" is nginx192.168.23.102 # to take over the normal service
192.168.23.102

[root @ k8snode02 ~] # 

5) The machine 192.168.23.101 keepalived nginx and pull services, due to the high machine 192.168.23.101 priority, the preempt VIP
[@ k8snode01 the root keepalived] # / Data / nginxinstall / sbin / nginx -C / Data / nginxinstall / the conf /nginx.conf # start nginx
[root @ k8snode01 keepalived] # /etc/init.d/keepalived start # start keepalived
starting keepalived (Via systemctl): [the OK]
[root @ k8snode01 keepalived] # ip addr #vip seize 192.168 .23.101
. 1: LO: <the LOOPBACK, the UP, LOWER_UP> UNKNOWN MTU 65536 Group default qdisc allows users to noqueue State 
    Link / Loopback 00: 00: 00: 00: 00: 00 brd 00: 00: 00: 00: 00: 00
    inet 127.0. 0.1 / Host. 8 scope LO
       valid_lft Forever Forever preferred_lft
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:a3:b8:9f brd ff:ff:ff:ff:ff:ff
    inet 192.168.23.101/24 brd 192.168.23.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.23.200/32 scope global ens33
       valid_lft forever preferred_lft forever
[root@k8snode01 keepalived]#

[root@k8snode02 ~]# ip addr #192.168.23.102上的vip消失
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    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
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:32:05:d5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.23.102/24 brd 192.168.23.255 scope global ens33
       valid_lft forever preferred_lft forever
[root@k8snode02 ~]# 
[root@k8snode02 ~]# curl "192.168.23.200:8888" #主nginx抢占服务
192.168.23.101

[root @ k8snode02 ~] # curl "192.168.23.200:8888" # nginx seize the main service
192.168.23.101

[root @ k8snode02 ~] #


 

 

 

Published 60 original articles · won praise 20 · views 4586

Guess you like

Origin blog.csdn.net/zhaikaiyun/article/details/104827784