nginx + keepalived live configuration bis

First, install nginx

Our side is installed by default nginx version 1.12.2, so we need to install the 1.16.1 version of nginx, we go, so we are here to update yum source, follow these steps:

1, yum source added:

[root@shtw-nk08 sbin]# cd /etc/yum.repos.d/
[root@shtw-nk08 sbin]# vim nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key

2, yum install nginx

[root@shtw-nk08 nginx]#yum install  nginx

3, see the nginx version

[root@shtw-nk08 yum.repos.d]# nginx -v
nginx version: nginx/1.16.1

4, restart, shut down, test nginx

[root@shtw-nk08 nginx]# nginx  -s  reload  #重启nginx
[root@shtw-nk08 nginx]# nginx  -s  stop  #停止nginx
[root@shtw-nk08 nginx]# nginx  -t  #测试
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

5, if nginx -s reload error, and then execute the following script:

[root@shtw-nk08 nginx]# nginx -c /etc/nginx/nginx.conf

Second, the installation keepalived

1, IP preparations

Main (nginx)  10.55.202.213
 From (nginx)  10.55.202.214
 Virtual ip (main)  10.55.202.107
 Virtual ip (from)  10.55.202.108

 

2, installation keepalived

Description: need to install standby:

[root@shtw-nk08 keepalived]# yum install keepalived

3, modify the configuration file

The main nginx keepalived Review:

[root@shtw-nk08 keepalived]# cd /etc/keepalived/
[root@shtw-nk08 keepalived]# mv keepalived.conf  keepalived.conf.bak
[root@shtw-nk08 keepalived]# vim keepalived.conf
! Configuration File for keepalived

group {
   
    VI_1
 
}

vrrp_script chk_http_port {
    script "/usr/local/sbin/check_ng.sh"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state MASTER      #表示主的nginx
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    mcast_src_ip 10.55.202.213    #主nginx的ip地址
    authentication {
        auth_type PASS
        auth_pass 1111
    } 
    Track_script { 
       chk_http_port 
    } 
    virtual_ipaddress { 
        10.55.202.107 # virtual master ip 
    } 
} 

vrrp_instance VI_2 { 
    State mean BACKUP # backup 
    interface eth0 
    virtual_router_id 61 is 
    priority 99 
    advert_int. 1 
    mcast_src_ip 10.55.202.213 ip address # is the primary 
    authentication { 
        AUTH_TYPE the PASS 
        AUTH_PASS 1111 
    } 
    track_script { 
       chk_http_port 
    } 
    virtual_ipaddress { 
        10.55.202.108 # backup virtual ip address 
    } 
}

keepalived from the nginx configuration of:

[root@shtw-nk08 keepalived]# cd /etc/keepalived/
[root@shtw-nk08 keepalived]# mv keepalived.conf  keepalived.conf.bak
[root@shtw-nk08 keepalived]# vim keepalived.conf
! Configuration File for keepalived

group {
   
    VI_1
 
}

vrrp_script chk_http_port {
    script "/usr/local/sbin/check_ng.sh"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state BACKUP   #表示从的nginx
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    mcast_src_ip 10.55.202.214  #从的ip地址
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
       chk_http_port 
    } 
    virtual_ipaddress { 
        10.55.202.107 
    } 
} 


vrrp_instance VI_2 { 
    State from the MASTER # represents the 
    interface eth0 
    virtual_router_id 61 is 
    priority 100 
    advert_int. 1 
    mcast_src_ip 10.55.202.214 # ip address from 
    authentication { 
        AUTH_TYPE the PASS 
        AUTH_PASS 1111 
    }    
    track_script { 
       chk_http_port 
    }   
    virtual_ipaddress { 
        10.55. 202.108 # from virtual ip address 
    }    
}

4, active-active configuration script keepalived

Description: The main script needs to be configured from both machines, Once configured, save (wq) and exit

[@ shtw the root-nk05 sbin] # CD / usr / local / sbin 
[@ shtw the root-nk05 sbin] Vim check_ng.sh # 
#! / bin / the bash 
# time variable, for logging 
d = `date --date the Y +% m% Today% D_% H:% m:% S ' 
# calculation process nginx 
n-PS -C = `nginx --no-heading | WC -l` 
# If the process is 0, nginx is started, and again detected number nginx processes 
# 0 if it is described nginx not start at this time needs to close keepalived 
IF [$ n--eq "0"]; the then 
        /etc/init.d/nginx start 
        N2 = `nginx PS -C -heading --no | WC -l` 
        IF [$ N2 -eq "0"]; the then 
                echo "$ D Nginx Down, Will keepalived STOP" >> /var/log/check_ng.log 
                systemctl STOP keepalived 
        Fi 
Fi

5, start keepalived

[root@shtw-nk08 keepalived]# systemctl  start  keepalived  #启动keepalived

[root@shtw-nk08 keepalived]# systemctl  status  keepalived  #查看keepalived状态

Figure:

 

 

 6 Start authentication

After nginx and keepalived all started, test it in your browser:

1, first enter 10.55.202.213 => successful visit

2, input 10.55.202.214 => successful visit

3, enter 10.55.202.107 and 10.55.202.108 => successful visit

4, after shutting down 10.55.202.213 test => access fails, enter 10.55.202.107 => successful visit

Guess you like

Origin www.cnblogs.com/zhangqigao/p/12053901.html