Nginx + keepalived high availability hot standby (master and slave modes)

Environment: centos7.6 minimize installation

Main: 10.11.1.32

From: 10.11.1.33

VIP:10.11.1.130

Modify the primary host name:

hostnamectl set-hostname web_balance_01

Modify the host name from the node:

hostnamectl set-hostname web_balance_02

A mounting Nginx (two nodes need to be installed)

Configuring the official nginx source

paint /etc/yum.repos.d/nginx.repo 
[nginx] 
name = nginx repo 
baseurl = http: //nginx.org/packages/centos/7/$basearch/ 
gpgcheck = 0 
enabled = 1

 yum install nginx way

yum install nginx

 Second, the installation keepalived (two nodes need to be installed)

Dependent on the installation environment

yum -y install libnl libnl-devel

 Download keepalived pack and unpack

cd /tmp
wget https://www.keepalived.org/software/keepalived-2.0.19.tar.gz
tar -xf keepalived-2.0.19.tar.gz

 Compile and install

 ./configure --prefix=/usr/local/keepalived
make -j2
make install

 Under the copy startup script to /etc/init.d/ directory

cd /tmp/keepalived-2.0.19
cp keepalived/etc/init.d/keepalived /etc/init.d/

 

cp keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp etc/keepalived/keepalived.conf /etc/keepalived/

Fourth, modify the master node configuration file keepalived.conf

cd /etc/keepalived/
cp -r keepalived.conf keepalived.conf_bak
vi keepalived.conf

 Keepalived.conf master node configured as follows:

! Keepalived the Configuration File for 
 
global_defs { 
    the router_id of the web_balance_01 # identification name of the node, typically hostname 
} 
 
analyzed ## keepalived periodically script execution script execution result and dynamically adjusts the priority of vrrp_instance. 
## If the script execution result is 0, and the value of greater than 0 weight configuration, the priority corresponding increase. If the script execution result is nonzero, 
the value and the weight ## is disposed less than 0, the priority corresponding reduction. Other cases, the priority of maintaining the original configuration, i.e. the value corresponding to the priority in the configuration file. 
 
{chk_nginx vrrp_script 
       Script "/etc/keepalived/nginx_check.sh" 
       interval The # 2 once every 2 seconds nginx detected operating state of 
       weight -20 # failed once, their priority -20 
} 
 
vrrp_instance VI_1 { 
    State # the MASTER state, the main node MASTER, the backup node to the bACKUP 
    interface enp175s0f0 # bindings VIP network interface, see your network interface ifconfig 
    virtual_router_id 51 # virtual route ID number, set up two nodes must be the same, use the optional IP last paragraph, the same VRID as a group, he will determine the multicast MAC address 
    mcast_src_ip 10.11.1.32 # Local IP address
    priority 100 # node priority, value range 0 ~ 254, MASTER BACKUP higher than 
    advert_int 1 # multicast transmission time interval, the same two nodes must be set, the default is one second 
    nopreempt non-preemptive mode using #   
 
    # Set authentication information, two nodes must match 
    authentication { 
        AUTH_TYPE the PASS 
        AUTH_PASS 1111 
    } 
 
    # virtual IP, the same two nodes must be provided. Plurality may be provided, a write line 
    virtual_ipaddress { 
        10.11.1.130 
    } 
 
    track_script { 
       chk_nginx # Nginx viable state detection script 
    } 
}

 Keepalived.conf slave node configured as follows:

! Keepalived the Configuration File for 
 
global_defs { 
    the router_id of the web_balance_02 # identification name of the node, typically hostname 
} 
 
## keepalived periodically script execution result is analyzed and executed by the script, to dynamically adjust the priority vrrp_instance. 
## If the script execution result is 0, and the value of greater than 0 weight configuration, the priority corresponding increase. If the script execution result is nonzero, 
the value and the weight ## is disposed less than 0, the priority corresponding reduction. Other cases, the priority of maintaining the original configuration, i.e. the value corresponding to the priority in the configuration file. 
 
{chk_nginx vrrp_script 
       Script "/etc/keepalived/nginx_check.sh" 
       interval The # 2 once every 2 seconds nginx detected operating state of 
       weight -20 # failed once, their priority -20 
} 
 
vrrp_instance VI_1 { 
    State # the BACKUP status, primary node MASTER, the backup node to the bACKUP 
    interface enp175s0f0 # bindings VIP network interface, see your network interface ifconfig 
    virtual_router_id 51 # virtual route ID number, set up two nodes must be the same, use the optional IP last paragraph, the same VRID as a group, he will determine the multicast MAC address 
    mcast_src_ip 10.11.1.33 # Local IP address
    priority 90 # node priority, value range 0 ~ 254, MASTER BACKUP higher than 
    advert_int 1 # multicast transmission time interval, the same two nodes must be set, the default is one second 
    nopreempt non-preemptive mode using #   
 
    # Set authentication information, two nodes must match 
    authentication { 
        AUTH_TYPE the PASS 
        AUTH_PASS 1111 
    } 
 
    # virtual IP, the same two nodes must be provided. Plurality may be provided, a write line 
    virtual_ipaddress { 
        10.11.1.130 
    } 
 
    track_script { 
       chk_nginx # Nginx viable state detection script 
    } 
}

 Fifth, create nginx presence status detection script (two nodes need to create)

CD / etc / keepalived 
More nginx_check.sh 
! # / bin / the bash 
A = `PS -C nginx --no-header | WC -l` 
IF [$ A -eq 0]; the then 
    systemctl # try to restart the restart nginx nginx 
    sleep 2 # sleep two seconds 
    IF [PS -C nginx `--no-header | WC -l` -eq 0]; the then 
        killall # keepalived failed to start the service keepalived killed. The drift to other backup nodes vip 
    Fi 
Fi

 Add the script execution permissions

chmod +x /etc/keepalived/nginx_check.sh

 Sixth, in turn starts from the master node and nodes nginx and services keepalived

systemctl start nginx
systemctl enable nginx
systemctl start keepalived
systemctl enable keepalived

 Seven inquiries VIP binding on the primary node

 

 

 Eight, to stop any node nginx service, check whether keepalived nginx service starts automatically via script

 

 

 Nine, stop the master node keepalived services can be seen from the VIP has been bound to the node

 

 

 

 

Guess you like

Origin www.cnblogs.com/caidingyu/p/11797224.html