keepalived+nginx to build dual host active standby + Dual host hot standby

Preparation

One is 192.168.31.210, the other is 192.168.31.211

Install nginx, modify index to distinguish

 

1, Install preserved

The only difference between the two is the keepalived configuration file

https://keepalived.org/

 

 

Download and unzip

tar -zxvf keepalived-2.0.18.tar.gz

Enter keepalived-2.0.18

implement

./configure --prefix=/usr/local/keepalived --sysconf=/etc

Install and compile

make && make install

If this error is reported, install libnl/libnl-3

Need to be installed in advance

yum -y install libnl-devel

 

You can use systemctl to start it. If you want to enter keepalived directly, you can also start it directly

./keepalived 

 

 

Current directory entry

cd keepalived-2.0.18/keepalived/etc

Put the keepalived configuration in etc

cp init.d/keepalived /etc/init.d/ &&
cp sysconfig/keepalived /etc/sysconfig/

Reload profile

systemctl daemon-reload

Start keepalived

systemctl start keepalived.service

 

Do the same for another server

 

2, Dual host standby

Principle access through virtual ip once the host hangs, use the standby

The disadvantage is that the standby machine doesn't do other things, which is a waste of resources

Configure the keepalived core configuration file

192.168.31.210 server

Check ﹣ nginx ﹣ alive ﹣ or ﹣ not.sh is the auto start nginx shell script

keepalived.conf is the keepalived core configuration file

 

 

1,vim check_nginx_alive_or_not.sh

Here / usr/local/nginx/sbin/nginx is modified according to its own nginx path

#!/bin/bash

A=`ps -C nginx --no-header |wc -l`
# Judge whether nginx is down. If it is down, try to restart it
if [ $A -eq 0 ];then
	/usr/local/nginx/sbin/nginx
	# Wait for a short time and check nginx here. If it fails to start, stop keepalived to start the standby machine
	sleep 3
	if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
		killall keepalived
	fi
fi

2. Then Chmod + X check ﹣ nginx ﹣ alive ﹣ or ﹣ not.sh add permission

3,vim keepalived.conf

Here, the network card configuration interface ens33 is viewed with ip addr

! Configuration File for keepalived

global_defs {
   #Routing id: identifier of the currently installed keepalived node host, globally unique
   router_id keep_nginx1
}

#Execute nginx startup judgment script
vrrp_script check_nginx_alive {
	script "/etc/keepalived/check_nginx_alive_or_not.sh"
	interval 2 #Run the previous script every two seconds
	weight 10	#Upgrade weight + 10 if the script runs successfully
	#weight -10	#If the script fails to run, reduce the weight - 10
}

#Computer node
vrrp_instance VI_1 {
    
    #The status of the identification, the master node of the current nginx, MASTER/BACKUP
    state MASTER
    #Network card bound by current instance
    interface ens33
    
    #Ensure the consistency of primary and secondary nodes
    virtual_router_id 51
    
    #Priority / weight: who has the highest priority will become the MASTER after MASTER hangs up
    priority 100
    
    #The time interval of synchronous check between the master and the standby is 1s by default
    advert_int 1

    #Password of authentication and authorization to prevent the entry of illegal nodes
    authentication {
        auth_type PASS
        auth_pass 1111
    }

	#Call script
    track_script {
	check_nginx_alive #Tracing nginx scripts
    }
 
    #Here, the virtual ip field should be in the same field as its own server. For example, my server ip is 192.168.31.210 
    #All the first three are the same 192.168.31
    virtual_ipaddress {
        192.168.31.161
    }
}

The host is configured

The configuration of the standby machine is the same as above. The only difference is the weight and status

Another 192.168.31.211 server configuration

! Configuration File for keepalived

global_defs {
   #Routing id: identifier of the currently installed keepalived node host, globally unique
   router_id keep_nginx2
}

vrrp_script check_nginx_alive {
	script "/etc/keepalived/check_nginx_alive_or_not.sh"
	interval 2 #Run the previous script every two seconds
	weight 10	#Upgrade weight + 10 if the script runs successfully
	#weight -10	#If the script fails to run, reduce the weight - 10
}

#Computer node
vrrp_instance VI_1 {
    
    #The status of the identification, the master node of the current nginx, MASTER/BACKUP
    state BACKUP
    #Network card bound by current instance
    interface ens33
    
    #Package primary and secondary nodes are consistent
    virtual_router_id 51
    
    #Priority / weight: who has the highest priority will become the MASTER after MASTER hangs up
    priority 80
    
    #The time interval of synchronous check between the master and the standby is 1s by default
    advert_int 1

    #Password of authentication and authorization to prevent the entry of illegal nodes
    authentication {
        auth_type PASS
        auth_pass 1111
    }
 
    track_script {
        check_nginx_alive #Tracing nginx scripts
    }
   
    virtual_ipaddress {
        192.168.31.161
    }
}

 

 

3, Dual main hot standby

He made up for the shortcomings of dual hosts, which can run both

After the user accesses, DNS accesses the round training and randomly accesses the virtual ip

Here we use two virtual IPS, one virtual ip, to bind two nginx. When one is kept alive, the other is bound to two

It can be realized that neither server wastes resources

 

192.168.31.210 server configuration

There is an extra VI 2

! Configuration File for keepalived

global_defs {
   #Routing id: identifier of the currently installed keepalived node host, globally unique
   router_id keep_nginx1
}

vrrp_script check_nginx_alive {
	script "/etc/keepalived/check_nginx_alive_or_not.sh"
	interval 2 #Run the previous script every two seconds
	weight 10	#Upgrade weight + 10 if the script runs successfully
	#weight -10	#If the script fails to run, reduce the weight - 10
}

#Computer node
vrrp_instance VI_1 {
    
    #The status of the identification, the master node of the current nginx, MASTER/BACKUP
    state MASTER
    #Network card bound by current instance
    interface ens33
    
    #Package primary and secondary nodes are consistent
    virtual_router_id 51
    
    #Priority / weight: who has the highest priority will become the MASTER after MASTER hangs up
    priority 100
    
    #The time interval of synchronous check between the master and the standby is 1s by default
    advert_int 1

    #Password for authentication and authorization to prevent the entry of illegal nodes
    authentication {
        auth_type PASS
        auth_pass 1111
    }
   
    #Call script
    track_script {
	check_nginx_alive #Tracing nginx scripts
    }
   
    virtual_ipaddress {
        192.168.31.161
    }
}

#Computer node
vrrp_instance VI_2 {

    #The status of the identification, the master node of the current nginx, MASTER/BACKUP
    state BACKUP
    #Network card bound by current instance
    interface ens33

    #Package primary and secondary nodes are consistent
    virtual_router_id 52

    #Priority / weight: who has the highest priority will become the MASTER after MASTER hangs up
    priority 80

    #The time interval of synchronous check between the master and the standby is 1s by default
    advert_int 1

    #Password of authentication and authorization to prevent the entry of illegal nodes
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    #Call script
    track_script {
        check_nginx_alive #Tracing nginx scripts
    }   
    
    virtual_ipaddress {
        192.168.31.162
    }
}

restart

systemctl restart keepalived.service

 

192.168.31.211 server configuration

! Configuration File for keepalived

global_defs {
   #Routing id: identifier of the currently installed keepalived node host, globally unique
   router_id keep_nginx2
}

vrrp_script check_nginx_alive {
	script "/etc/keepalived/check_nginx_alive_or_not.sh"
	interval 2 #Run the previous script every two seconds
	weight 10	#Upgrade weight + 10 if the script runs successfully
	#weight -10	#If the script fails to run, reduce the weight - 10
}

#Computer node
vrrp_instance VI_1 {
    
    #The status of the identification, the master node of the current nginx, MASTER/BACKUP
    state BACKUP
    #Network card bound by current instance
    interface ens33
    
    #Package primary and secondary nodes are consistent
    virtual_router_id 51
    
    #Priority / weight: who has the highest priority will become the MASTER after MASTER hangs up
    priority 80
    
    #The time interval of synchronous check between the master and the standby is 1s by default
    advert_int 1

    #Password for authentication and authorization to prevent the entry of illegal nodes
    authentication {
        auth_type PASS
        auth_pass 1111
    }
 
    track_script {
        check_nginx_alive #Tracing nginx scripts
    }
   
    virtual_ipaddress {
        192.168.31.161
    }
}


vrrp_instance VI_2 {

    #The status of the identification, the master node of the current nginx, MASTER/BACKUP
    state MASTER

    interface ens33

    #Package primary and secondary nodes are consistent
    virtual_router_id 52

    #Priority / weight: who has the highest priority will become the MASTER after MASTER hangs up
    priority 100

    #The time interval of synchronous check between the master and the standby is 1s by default
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass 1111
    }

    track_script {
        check_nginx_alive #Tracing nginx scripts
    }

    virtual_ipaddress {
        192.168.31.162
    }
}

restart

systemctl restart keepalived.service

 

Test simulation

Accessing two virtual IPS is displaying

 

Then we shut down one. I shut down 211 servers

Both servers display normally

But the difference is 192.168.31.162. He's visiting the content of another nginx, so there's no problem in our simulation

systemctl stop keepalived.service

Published 124 original articles, won praise 27, visited 20000+
Private letter follow

Tags: Nginx network vim yum

Posted on Wed, 05 Feb 2020 01:54:15 -0500 by Whyme