preface

LNMP Distributed Cluster deployment Practice

  • Nginx+PHP Platform Construction and Load Balancing Configuration
  • (2) NFS File Server Setup and File Buffer Upload Configuration
  • (3) : Setup of MySQL Primary and Secondary Database Server
  • (4) : Memcached Server Construction
  • (5) ThinkPHP Project Deployment
  • Keepalived High Availability Solution (VI)

The high availability of a cluster is measured by the absence of a single point of failure, where the failure of any one of the servers does not disrupt the entire service. In the previous LNMP distributed cluster practice, it was obvious that while using Nginx as a load balancer, upstream could detect the availability of back-end servers and if one of the servers went down, Nginx could automatically move to a healthy back-end server to keep the system available. However, there is only one load balancer server, once the failure of this server, the entire cluster service will be interrupted, so keepalived is used to deploy a standby server to implement failover. Keepalived has built-in Virtual Router Redundancy Protocol (VRRP) for static routes with single point of failure. Keepalived uses IP multicast to communicate with each other when a route is faulty. Replace the standby route with the active route through an election policy to continue to provide services. Keepalived implements the automatic switch between a Master server and a Backup server, using a Virtual IP address. When it fails, the Backup monitoring it will compete to take over the Virtual IP to continue the external service through the priority mechanism, and the other unsuccessful will continue to monitor the current Virtual IP server.

Servers in a high availability environment:

role Real IP (RIP) Virtual IP (VIP) instructions
Master 192.168.177.21 192.168.177.11 Nginx + Keepalived
Backup 192.168.177.22 192.168.177.11 Nginx + Keepalived
192.168.177.1 X Back-end server clusters

Then we made a change to the previous LNMP distributed cluster architecture:

LNMP high availability distributed cluster!

But to demonstrate that we don’t cluster, use two simple back-end servers, RIP 192.168.177.23 and 192.168.177.24, Keepalived service virtual IP 192.168.177.20:

role Real IP (RIP) Virtual IP (VIP) instructions
Master 192.168.177.21 192.168.177.20 Nginx + Keepalived
Backup 192.168.177.22 192.168.177.20 Nginx + Keepalived
192.168.177.23 Back-end server 1
192.168.177.24 Back-end server 2

The deployment of

Install and configure keepalived service

Deploy and configure the primary server

cd /usr/local/nginx/html
echo 'This is Master' > index.html
curl http://localhost

tar -zxvf keepalived-1.2.24.tar.gz
cdKeepalived-1.2.24./configure make && make install &&cd.cd /usr/local/etc/rc.d/init.d
cp keepalived /etc/init.d/keepalived
chmod +x /etc/init.d/keepalived
chkconfig keepalived on
cd /usr/local/etc/sysconfig
ln -s `pwd`/keepalived /etc/sysconfig/keepalived
cd /usr/local/sbin
ln -s `pwd`/keepalived /usr/sbin/keepalived

# Configure keepalived for the primary server

less /usr/local/etc/keepalived/keepalived.conf  View the configuration file template
mkdir /etc/keepalived  Create a default loaded configuration file manually
vi /etc/keepalived/keepalived.conf

vrrp_instance VI_1 {  Configure a virtual route
    state MASTER
    interface ens33 # specify the network adapter to monitor
    virtual_router_id 21  The id of the virtual route must be the same as that of the Master and Backup VRRP groupsMcAst_src_ip 192.168.177.21# set real IP, can be omitted, there is default automatic use of primary IP
    priority 100  # Priority, weight (the host with the highest weight will take over the Virtual IP), range 0-254
    advert_int 1  The time interval between MASTER and BACKUP synchronization check, in seconds
    authentication {
        auth_type PASS  Authentication type: PASS indicates password authentication
        auth_pass 123456  # set password
    }
    virtual_ipaddress {  Configure a virtual IP address pool
        192.168.177.20  Set the same virtual IP for MASTER and Backup}}# Start keepalived serviceService keepalived start ps aux | grep keepalived IP a | grep 192.168.177.20#### Configure Keepalived for the standby serverExample Clone a standby server whose IP address is 192.168.177.22 based on the active server. Then turn on keepalived configuration: bash vrrp_instance VI_1 {state BACKUPChange the id to BACKUP
    priority 90  # change priority to 90 (lower than MASTER)... }The Master and Backup communicate through PORT 112 of VRRP
iptables -I INPUT -s192.168.177.21 -p112 -jACCEPT
service iptables save
iptables -I INPUT -s192.168.177.22 -p112 -jACCEPT
service iptables save

service keepalived start Start Keepalived service for Backup server

# distinguish which server is currently accessed
cd /usr/local/nginx/html
echo 'This is Backup' > index.html

# test, access 192.168.177.20
service network stop
service network start
Copy the code

The deployment is complete.

Monitor native service using Keepalived

In addition to monitoring whether Keepalived is normal in other servers, you can also monitor whether a service in the machine is normal:

Monitor native Nginx services

vrrp_script chk_nginx {
    script "/chk_nginx.sh"Interval 2 weight-20} vrrp_instance VI_1 {...... track_script { chk_nginx } } vi /chk_nginx.shCreate a monitoring script

# !/bin/bash
if [ `ps -C nginx --no-header |wc -l` -eq0];then
    service nginx start
    sleep 2
    if [ `ps -C nginx --no-header |wc -l` -eq0];then
        service keepalived stop
    fi
fi

chmod +x /chk_nginx.sh
service keepalived reload
Copy the code

High availability testing

service nginx stop  # Manual shutdown
ps -C nginx --no-header  Wait 2 seconds and check whether the system is restarted

# create a script that stops the Nginx service and immediately revokes the execution permission of the Nginx program
cd /usr/local/nginx/sbin
vi test.sh

#! /bin/bash
service nginx stop
chmod -x nginx

chmod +x test.sh
./test.sh
See if you can switch to the standby server automatically
Copy the code

LNMP Distributed Cluster deployment Practice

  • Nginx+PHP Platform Construction and Load Balancing Configuration
  • (2) NFS File Server Setup and File Buffer Upload Configuration
  • (3) : Setup of MySQL Primary and Secondary Database Server
  • (4) : Memcached Server Construction
  • (5) ThinkPHP Project Deployment
  • Keepalived High Availability Solution (VI)

dffaceCopyright notice: All articles are valid unless otherwise statedCC BY – NC – SA 4.0License agreement. Reproduced please indicate the source, commercial use is strictly prohibited!