Keepalived + Nginx實現高可用Web負載均衡

1、場景需求:html

2、Keepalived簡要介紹前端

Keepalived 是一種高性能的服務器高可用或熱備解決方案,Keepalived能夠用來防止服務器單點故障的發生,經過配合Nginx能夠實現web前端服務的高可用。nginx

Keepalived以VRRP協議爲實現基礎,用VRRP協議來實現高可用性(HA)。VRRP(Virtual Router Redundancy Protocol)協議是用於實現路由器冗餘的協議,VRRP協議將兩臺或多臺路由器設備虛擬成一個設備,對外提供虛擬路由器IP(一個或多個),而在路由器組內部,若是實際擁有這個對外IP的路由器若是工做正常的話就是MASTER,或者是經過算法選舉產生,MASTER實現針對虛擬路由器IP的各類網絡功能,如ARP請求,ICMP,以及數據的轉發等;其餘設備不擁有該虛擬IP,狀態是BACKUP,除了接收MASTER的VRRP狀態通告信息外,不執行對外的網絡功能。當主機失效時,BACKUP將接管原先MASTER的網絡功能。c++

VRRP協議使用多播數據來傳輸VRRP數據,VRRP數據使用特殊的虛擬源MAC地址發送數據而不是自身網卡的MAC地址,VRRP運行時只有MASTER路由器定時發送VRRP通告信息,表示MASTER工做正常以及虛擬路由器IP(組),BACKUP只接收VRRP數據,不發送數據,若是必定時間內沒有接收到MASTER的通告信息,各BACKUP將宣告本身成爲MASTER,發送通告信息,從新進行MASTER選舉狀態。web

 

 

3、方案規劃算法

VIPbash

IP服務器

主機名網絡

Nginx端口app

默認主從

192.168.186.50

192.168.186.129

dubbo-provider-01

88

MASTER

  192.168.186.132 dubbo-provider-02

88

CentOS 6.6 x64

     keepalived-1.2.18.tar.gz

     nginx-1.6.2.tar.gz

 

4、安裝Nginx

一、安裝編譯Nginx所需的依賴包

# yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel

 

二、上傳Nginx(nginx-1.6.2.tar.gz)到 /usr/local/src 目錄

 

三、編譯安裝Nginx

# cd /usr/local/src/

# tar -zxvf nginx-1.6.2.tar.gz

# cd nginx-1.6.2

# ./configure --prefix=/usr/local/nginx

# make && make install

 

四、配置Nginx

# vi /usr/local/nginx/conf/nginx.conf

user  root;

worker_processes  1;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;

    #tcp_nopush     on;

    #keepalive_timeout  0;

    keepalive_timeout  65;

    #gzip  on;

    server {

        listen       88;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html;

            index  index.html index.htm;

        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

}

 

修改Nginx歡迎首頁內容(用於後面測試,用於區分兩個節點的Nginx):

# vi /usr/local/nginx/html/index.html

192.168.1.51中的標題加 1

<h1>Welcome to nginx! 1</h1>

192.168.1.52中的標題加 2

<h1>Welcome to nginx! 2</h1>

 

五、系統防火牆打開對應的端口88

# vi /etc/sysconfig/iptables

## Nginx

-A INPUT -m state --state NEW -m tcp -p tcp --dport 88 -j ACCEPT

# service iptables restart

 

六、測試Nginx是否安裝成功

# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

 

七、啓動Nginx

# /usr/local/nginx/sbin/nginx

重啓Nginx

# /usr/local/nginx/sbin/nginx -s reload

 

八、設置Nginx開機啓動

# vi /etc/rc.local

加入:

/usr/local/nginx/sbin/nginx

九、分別訪問兩個Nginx

 

5、安裝Keepalived ( http://www.keepalived.org/download.html

一、上傳或下載keepalived(keepalived-1.2.18.tar.gz)到 /usr/local/src 目錄

 

二、解壓安裝

# cd /usr/local/src

# tar -zxvf keepalived-1.2.18.tar.gz

# cd keepalived-1.2.18

# ./configure --prefix=/usr/local/keepalived

# make && make install

 

三、將keepalived安裝成Linux系統服務:

由於沒有使用keepalived的默認路徑安裝(默認是/usr/local),安裝完成以後,須要作一些工做

複製默認配置文件到默認路徑

# mkdir /etc/keepalived

# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

複製keepalived服務腳本到默認的地址

# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

# ln -s /usr/local/sbin/keepalived /usr/sbin/

# ln -s /usr/local/keepalived/sbin/keepalived /sbin/

設置keepalived服務開機啓動

# chkconfig keepalived on

 

 

四、修改Keepalived配置文件

(1) MASTER節點配置文件(192.168.186.129)

# vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

   ## keepalived自帶的郵件提醒須要開啓sendmail服務。建議用獨立的監控或第三方SMTP

   router_id dubbo-provider-01   ## 標識本節點的字條串,一般爲hostname

}

## keepalived會定時執行腳本並對腳本執行的結果進行分析,動態調整vrrp_instance的優先級。若是腳本執行結果爲0,而且weight配置的值大於0,則優先級相應的增長。若是腳本執行結果非0,而且weight配置的值小於0,則優先級相應的減小。其餘狀況,維持本來配置的優先級,即配置文件中priority對應的值。

vrrp_script chk_nginx {

    script "/etc/keepalived/nginx_check.sh"    ## 檢測nginx狀態的腳本路徑

    interval 2     ## 檢測時間間隔

    weight -20     ## 若是條件成立,權重-20

}

## 定義虛擬路由,VI_1 爲虛擬路由的標示符,本身定義名稱

vrrp_instance VI_1 {

    state BACKUP   ## 主節點爲BACKUP,對應的備份節點也爲BACKUP,主要靠priority值來決定誰是master。

    interface eth1 ## 綁定虛擬IP的網絡接口,與本機IP地址所在的網絡接口相同,個人是eth1

    virtual_router_id 51    ## 虛擬路由的ID號,兩個節點設置必須同樣,可選IP最後一段使用, 相同的VRID爲一個組,他將決定多播的MAC地址

    #mcast_src_ip 192.168.186.129    ## 本機IP地址

    priority 120   ## 節點優先級,值範圍0-254,MASTER要比BACKUP高

nopreempt ## 優先級高的設置nopreempt解決異常恢復後再次搶佔的問題

advert_int 1   ## 組播信息發送間隔,兩個節點設置必須同樣,默認1s

## 設置驗證信息,兩個節點必須一致

authentication {

        auth_type PASS

        auth_pass huangkejie ## 真實生產,按需求對應該過來

}

## 將track_script塊加入instance 配置塊

    track_script {

        chk_nginx  ## 執行Nginx監控的服務

}

## 虛擬IP池, 兩個節點設置必須同樣

    virtual_ipaddress {

        192.168.186.50   ## 虛擬ip,能夠定義多個,IP不能隨意定,因爲個人虛擬機IP是192.168.186開頭,因此只能以

                                  ## 192.168.186.*開頭,*我定義爲50

    }

}

 

(2)BACKUP節點配置文件(192.168.1.52):

# vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

   router_id dubbo-provider-02

}

 

vrrp_script chk_nginx {

    script "/etc/keepalived/nginx_check.sh"

    interval 2

    weight -20

}

 

vrrp_instance VI_1 {

    state BACKUP

    interface eth1

    virtual_router_id 51

    #mcast_src_ip 192.168.186.132

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass huangkejie

    }

    track_script {

        chk_nginx

    }

    virtual_ipaddress {

        192.168.186.50

    }

}

 

五、編寫Nginx狀態檢測腳本 /etc/keepalived/nginx_check.sh (已在keepalived.conf中配置)

腳本要求:若是nginx中止運行,嘗試啓動,若是沒法啓動則殺死本機的keepalived進程,keepalied將虛擬ip綁定到BACKUP機器上。內容以下:

# vi /etc/keepalived/nginx_check.sh

#!/bin/bash

A=`ps -C nginx –no-header |wc -l`

if [ $A -eq 0 ];then

    /usr/local/nginx/sbin/nginx

    sleep 2

    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then

        killall keepalived

    fi

fi

 

保存後,給腳本賦執行權限:

# chmod +x /etc/keepalived/nginx_check.sh

nginx_check.sh腳本以下

#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
    /usr/local/nginx/sbin/nginx
    sleep 2
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        killall keepalived
    fi
fi

六、分別在主備機器上設置防火牆端口

防火牆配置容許組播(主、備設備上都須要配置,keepalived使用224.0.0.18做爲Master和Backup健康檢查的通訊IP)
# iptables -I INPUT -i eth1 -d 224.0.0.0/8 -p vrrp -j ACCEPT
# iptables -I OUTPUT -o eth1 -d 224.0.0.0/8 -p vrrp -j ACCEPT
(eth1爲主機的網卡設備名稱,生產環境服務器能夠用獨立網卡來處理組播和心跳檢測等)
# service iptables save
重啓防火牆:    
# service iptables restart
 

七、啓動Keepalived

# service keepalived start

Starting keepalived: [  OK  ]

 

八、Keepalived+Nginx的高可用測試

(1)關閉192.168.186.129中的Nginx,Keepalived會將它從新啓動

# /usr/local/nginx/sbin/nginx -s stop

(2)關閉192.168.186.129中的Keepalived,VIP會切換到192.168.186.132中

# service keepalived stop

Keepalived中止後,該節點的網絡接口中的VIP將消失

Keepalived服務管理命令:

中止:service keepalived stop

啓動:service keepalived start

重啓:service keepalived restart

查看狀態:service keepalived status

相關文章
相關標籤/搜索