1. 程式人生 > >實現雙主模型NGINX架構

實現雙主模型NGINX架構

leg ive cast conf kill alt entos echo 權重

實驗拓撲圖

技術分享圖片

主節點配置

yum -y install nginx keepalived psmisc

#修改keepalived配置文件
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 127.0.0.1
   smtp_connect_timeout 30

   router_id LVS1           #用於標識本節點的名稱
   vrrp_garp_interval 0     
   vrrp_gna_interval 0
   vrrp_iptables            #關閉防火墻功能
#  vrrp_mcast_group4 224.0.0.18
}

vrrp_script chk_down {  #定義檢測資源
    script "/bin/bash -c ‘[[ -f /etc/keepalived/down ]]‘ && exit 1 || exit 0"
    interval 1          #間隔檢測時間
    weight -5           #檢測失敗後權重-5
}

vrrp_script chk_nginx { #定義檢測資源
    script "killall -0 nginx && exit 0 || exit 1"
    interval 1          #間隔檢測時間
    weight -5           #檢測失敗後權重-5
}
vrrp_instance VI_1 {    
    state MASTER        #定義狀態為主或從
    interface ens34     #定義對外的網卡接口
    virtual_router_id 88    #虛擬路由ID,相同的IP為一組
    priority 100        #定義主節點的優先級
    advert_int 1
    authentication {    #定義認證信息
        auth_type PASS
        auth_pass rilegou
    }
    virtual_ipaddress { #定義虛擬IP(VIP)
        172.20.29.111
    }
    track_script {      #調用定義的檢測資源腳本
        chk_nginx
        chk_down
    }
}

vrrp_instance VI_2 {
    state BUCKUP
    interface ens34
    virtual_router_id 99
    priority 95
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass centos
    }
    virtual_ipaddress {
            172.20.29.114
    }
    track_script {      #調用定義的檢測資源腳本
        chk_nginx
        chk_down
    }
}

#配置NGINX服務
vim /etc/nginx/conf.d/test.conf
upstream websrvs {
    server 172.20.29.103:80;
    server 172.20.29.104:80;
}
server {
        listen 80 default_server;
        server_name node01.magedu.com;
        root /usr/share/nginx/html;
        location / {
                proxy_pass http://websrvs;
        }
}

#開啟服務
systemctl nginx keepalived

從節點配置

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS2           #用於標識本節點的名稱
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   vrrp_iptables            #關閉防火墻功能
#   vrrp_mcast_group4 172.20.29.77
}
vrrp_script chk_down {      #定義檢測資源
    script "/bin/bash -c ‘[[ -f /etc/keepalived/down ]]‘ && exit 1 || exit 0"
    interval 1              #間隔檢測時間
    weight -5               #檢測失敗後權重-5
}
vrrp_script chk_nginx {     #定義檢測資源
    script "killall -0 nginx && exit 0 || exit 1"
    interval 1              #間隔檢測時間
    weight -5               #檢測失敗後權重-5
}

vrrp_instance VI_1 {
    state BACKUP            #定義狀態為主或從
    interface ens34         #定義外對的網卡接口
    virtual_router_id 88    #虛擬路由ID,相同的ID為一組
    priority 98             #定義從服務器的優先級
    advert_int 1
    authentication {        #認證信息
        auth_type PASS
        auth_pass rilegou
    }
    virtual_ipaddress {     #定義虛擬IP(VIP)
        172.20.29.111
    }
    track_script {          #調用定義的檢測資源腳本
        chk_nginx
        chk_down
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface ens34
    virtual_router_id 99
    priority 96
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass centos
    }
    virtual_ipaddress {
        172.20.29.114
    }
    track_script {      #調用定義的檢測資源腳本
        chk_nginx
        chk_down
    }
}

    #配置NGINX服務
vim /etc/nginx/conf.d/test.conf
upstream websrvs {
    server 172.20.29.103:80;
    server 172.20.29.104:80;
}
server {
        listen 80 default_server;
        server_name node01.magedu.com;
        root /usr/share/nginx/html;
        location / {
                proxy_pass http://websrvs;
        }
}

#開啟服務
systemctl nginx keepalived

web服務器配置

#配置httpd頁面
echo "<h1>`hostname`</h1>" > /var/www/html/index.html
#添加VIP
ip a a 172.20.29.111/32 dev lo

#配置VIP不沖突
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce

#開啟服務
systemctl start httpd

實現雙主模型NGINX架構