1. 程式人生 > >利用keepalived實現高可用nginx

利用keepalived實現高可用nginx

keepalived 高可用nginx

實驗拓撲圖
技術分享圖片
(1)本次基於VMware Workstation搭建一個四臺Linux(CentOS 7.4)系統所構成的一個服務器集群,其中兩臺nginx做前端調度服務器(一臺為主機,另一臺為備機),另外兩臺作為真實的Web服務器(向外部提供http服務,這裏僅僅使用了CentOS默認自帶的http服務,沒有安裝其他的類似Tomcat、Jexus服務)。

  (2)本次實驗設置了一個VIP(Virtual IP)為172.18.38.99,用戶只需要訪問這個IP地址即可獲得網頁服務。其中,nginx主機為172.18.38.100,備機為172.18.38.101。Web服務器A為172.18.38.200,Web服務器B為172.18.38.201。

一,配置nginx反向代理服務器,在兩臺nginx上都做一遍下面的操作

    1,在http語句塊中定義調度規則
    vim /etc/nginx/nginx.conf
        http {
             .....

             upstream webser {
                        server 172.18.38.200:80;
                        server 172.18.38.201:80;
                  }

             .....
        }

    2,而後在server中調用
    vim /etc/nginx/conf.d/vhost.conf
        server {
                listen 172.18.38.99:80;
                server_name www.a.com;
                location / {
                        proxy_pass http://webser;
                }
        }

2,重啟nginx,使配置生效

    systemctl restart nginx

二,配置keepalived+nginx_master服務器**

1,安裝keepalived

    yum install keepalived

2,修改keepalived配置文件

 vim /etc/keepalived/keepalived.conf

    ! Configuration File for keepalived
    global_defs {
       notification_email {
         root@localhost
       }
       notification_email_from keepalived@localhost
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
       router_id proxy1
       vrrp_mcast_group4 224.1.1.1
    }

    vrrp_script chk_nginx {
    script "killall -0 nginx && exit 0 || exit 1"
    interval 1
    weight -30
    fall 2
    rise 2
    }
    vrrp_instance VI_1 {
        state MASTER
        interface ens37
        virtual_router_id 66
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 123456
        }
        virtual_ipaddress {
            172.18.38.99/16
        }
        track_script {
            chk_nginx
        }
    }

    關鍵配置:
        1,定義nginx健康檢查腳本
        vrrp_script chk_nginx {
        script "killall -0 nginx && exit 0 || exit 1"
        interval 1     #以秒觸發一次
        weight -30     #nginx宕機就立馬減去有限級30
        fall 2         #檢查兩次如果都是宕機就表示nginx掛掉了
        rise 2         #宕機之後兩次檢查nginx是活著的就重新+30優先級
        }

        2,調用
        track_script {
            chk_nginx

3,啟動keepalived

    systemctl  start keepalived
    systemctl enable keepalived

三,配置keepalived+nginx_slave服務器

 vim /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived
    global_defs {
       notification_email {
         root@localhost
       }
       notification_email_from keepalived@localhost
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
       router_id proxy2
       vrrp_mcast_group4 224.1.1.1
    }

    vrrp_instance VI_1 {
        state BACKUP
        interface ens37
        virtual_router_id 66
        priority 80
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 123456
        }
        virtual_ipaddress {
            172.18.38.99/16
        }

    }

7,啟動keepalived

    systemctl  start keepalived
    systemctl enable keepalived

四,配置後端web服務器

1,安裝httpd軟件

    1,安裝httpd軟件
    yum install http
    2,啟動服務
        systemctl start httpd
        systemctl enable httpd
    2,生成web頁面,
        A主機
            echo web_server_A > /var/www/html/index.html
        b主機
            echo web_server_B > /var/www/html/index.html

五,測試

1,不當任何服務

    [root@testsrvr ~]# for i in {1..10};do sleep 0.5;curl 172.18.38.99;done
    web_server_B
    web_server_B
    web_server_A
    web_server_A
    web_server_B
    web_server_B
    web_server_A
    web_server_A
    web_server_B
    web_server_B

2,宕一臺keepalived服務

    [root@ke_nginx_S ~]# systemctl stop keepalived.service 

    [root@testsrvr  ~]# for i in {1..10};do sleep 0.5;curl 172.18.38.99;done
    web_server_B
    web_server_B
    web_server_A
    web_server_A
    web_server_B
    web_server_B
    web_server_A
    web_server_A
    web_server_B
    web_server_A

3,宕掉nginx_master服務器

    [root@ke_nginx-M ~]# systemctl stop nginx 

    [root@testsrvr ~]# for i in {1..100};do sleep 0.5;curl 172.18.38.99;done
    web_server_A
    web_server_A
    web_server_B
    web_server_B
    web_server_A
    curl: (7) couldn‘t connect to host
    curl: (7) couldn‘t connect to host
    curl: (7) couldn‘t connect to host
    curl: (7) couldn‘t connect to host
    web_server_B
    web_server_B
    web_server_A
    web_server_A

利用keepalived實現高可用nginx