Nginx and high availability keepalived

Set the main keepalived

#安装keepalived
[root@localhost ~]# yum -y install keepalived
#安装nginx
[root@localhost ~]# yum -y install nginx
——————————————————————————————————
#keepalived配置文件
[root@localhost ~]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30 
   the router_id of the LVS_DEVEL 
   vrrp_skip_check_adv_addr 
   vrrp_strict 
   vrrp_garp_interval 0 
   vrrp_gna_interval 0 
} 

vrrp_script check_nginx {# introduced script file 
    Script " /shell/nginx_check.sh " 
    interval The 2 
    weight - 20 is 
} 

vrrp_instance VI_1 { 
    State the MASTER # master 
    interface eno16777728 heartbeat NIC # 
    virtual_router_id 51 is 
    priority 100 # priority level 
    advert_int . 1 
    authentication { 
        AUTH_TYPE the PASS 
        AUTH_PASS1111 
    } 
    virtual_ipaddress { 
        192.168 . 200.201 
    } 
    track_script { 
    check_nginx # referenced script 
    } 
} 
______________________________________________________ 
# test equipment file 
[root @ localhost ~] # echo " 1111111 " > /usr/share/nginx/html/index.html

Set from keepalived

#安装keepalived
[root@localhost ~]# yum -y install keepalived
#安装nginx
[root@localhost ~]# yum -y install nginx
______________________________________________________________
#keepalived配置文件
[root@localhost ~]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_nginx {
    script "/shell/nginx_check.sh"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface eno16777728
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111 
    } 
    virtual_ipaddress { 
        192.168 . 200.201 
    } 
    track_script { 
    check_nginx     
    } 
} 

----------------------------------- 
# preparation test file 
[root @ localhost ~] # echo " 22222 " > /usr/share/nginx/html/index.html

Third, test
the primary server work
Here Insert Picture Description
when working from the server
Here Insert Picture Description

Four, nginx_check.sh shell file, configured for periodic tasks

! # / bin / the bash 
COUNT = " $ (PS -C Nginx --no-header | WC -l) " 

IF [$ COUNT -eq 0 ]; the then 
        systemctl the restart Nginx 
        SLEEP 2 
        IF [PS -C --no Nginx -header | WC the -l -eq 0 ]; the then 
                systemctl STOP keepalived 
        fi 
fi 
# script to add execute permissions 
chmod + the X-/shell/nginx_check.sh

 

Guess you like

Origin www.cnblogs.com/canflyfish/p/11634958.html