keepalived makes nginx highly available

  • 2020-06-19 12:27:25
  • OfStack

keepalived literal translation is to keep alive, is to keep the online in network, also is the so-called high availability or hot standby, to prevent a single point of failure (single point of failure is 1 denier a 1 point failure will lead to the whole system architecture is not available), keepalived implementation is based on vrrp, as to what is vrrp please directly see vrrp here, let's see the application directly.

keepalived use

For ease of use, I wrote a 1 key configuration script based on ubuntu 16.04 server. See the script for configuration usage


#!/bin/bash
# nginx+keepalived  High availability 1 Key script for ubuntu 16.04

if [ $# -ne 4 ]; then
 echo "USAGE: $0 [MASTER|BACKUP] priority interface virtual_ipaddress"
 exit 0
fi

echo -e 'Installing nginx'
apt-get install nginx -y > /dev/null 2<&1

echo -e 'Installing keepalived'
apt-get install keepalived -y > /dev/null 2<&1

echo -e 'Configuring keepalived'
if [ ! -e /etc/keepalived ];then
 mkdir /etc/keepalived
fi
cat > /etc/keepalived/keepalived.conf <<EOF
! Configuration File for keepalived
global_defs {
 notification_email {
  chencheng199211@gmail.com
 }
 notification_email_from email # Represents the source address of the message when the notification message is sent 
 smtp_server 127.0.0.1 # Said to send email The use of smtp Server address, which you can use locally sendmail To implement the 
 smtp_connect_timeout 30
 router_id master # The machine id 
}
vrrp_script chk_nginx {
 script "/etc/keepalived/check_nginx.sh"
 interval 2 # Script execution interval, per second 
 weight -5 # Priority changes resulting from script results: 10 Denotes priority +10 ; -10 Is a priority -10
 fall 3
 rise 2
}

vrrp_instance VI_1 {
 state $1 #state The specified instance(Initial) The initial state, that is, after the configuration, the initial state of this server is specified here, but the specified here does not count, or must be determined through the campaign through the priority, if set here as master But if he has a lower priority than the other 1 Station, then this station will send its own priority when sending the notification, plus 1 If a station finds that its priority is not as high as its own, it will respond by preempting it master
 interface $3 # The instance is bound to the network card because the virtual is being configured IP Must be added to an existing network card 
 virtual_router_id 51 # Set up here VRID It's very important here. Same thing VRID for 1 He will decide on multicast MAC address 
 priority $2 # Set the priority of this node. The higher priority is master
 advert_int 2 # Check interval, default is 1 seconds 
 authentication { # I'm gonna set up authentication here 
  auth_type PASS # The authentication method can be PASS or AH Two kinds of authentication 
  auth_pass 1111 # The authentication code 
 }
 virtual_ipaddress { # That's what we set up here VIP Which is virtual IP Address, he followed state Change while adding delete, when state for master Add when, when state for backup When deleting, here is mainly determined by the priority, and state It doesn't matter how many values you set, you can set more than one here IP address 
  $4
 }
 track_script {
  chk_nginx
 }

 #dont_track_primary : ignore VRRP the interface error 
 #track_interface : Trace interface, set additional monitoring, any inside 1 Block network card problems, will enter the fault (FAULT) States, for example, with nginx When you do an equalizer, the internal network has to work properly, and if something goes wrong with the internal network, the equalizer won't work, so you have to do a health check on the internal and external network at the same time 
 #mcast_src_ip : The source from which multicast packets are sent IP Address, notice here, this is actually sending at that address VRRP Notice, this is very important, 1 Select a stable network card port to send heartbeat If it is not set, use the default bound network card IP , that is, interface The specified IP address 
 #garp_master_delay : Switching to master After status, delay to proceed free of charge ARP(gratuitous ARP) request 
 #virtual_routes : the principle and virtual ipaddress1 Like, but here is to add and remove the route 
 #lvs_sync_daemon_interface : lvs syncd The network card that binds 
 #nopreempt : Settings do not preempt, here can only be set in state for backup And the priority of this node must be higher. When the main mysql Do not preempt resources after recovery 
 #preempt_delay : Preemption delay 
 #debug : debug level 
 #notify_master : indicates when switching to master State, the script to be executed 
 #notify_backup : indicates when switching to backup State, the script to be executed 
 #notify_fault : 
}
EOF

cat > /etc/keepalived/check_nginx.sh <<EOF
#!/bin/bash
# description:
#  Regularly check nginx Does it exist? If not, it starts nginx
#  If the startup fails, stop keepalived
status=$(ps -C nginx --no-heading | wc -l)
if [ "${status}" = "0" ]; then
  service nginx start
  sleep 2
  status2=$(ps -C nginx --no-heading | wc -l)
  if [ "${status2}" = "0" ]; then
    /etc/init.d/keepalived stop
  fi
fi
EOF

echo -e "enable boot and starting"
service keepalived start
service nginx start

Related articles: