keepalived+nginx安裝與配置

第一步:html

下載keepalived地址:http://www.keepalived.org/download.htmlnginx

解壓:ubuntu

tar -zxvf keepalived-1.2.18.tar.gz -C /usr/local/vim

(須要安裝依賴軟件包)bash

yum install -y openssl openssl-devel服務器

網絡

(sudo apt-get install openssl && sudo apt-get install libssl-dev)測試

執行安裝:router

cd keepalived-1.2.18/ && ./configure --prefix=/usr/local/keepalivedhtm

make && make install

第二步:

將keepalived安裝成Linux系統服務,由於沒有使用keepalived的默認安裝路徑(默認路徑:/usr/local),安裝完成以後,須要作一些修改工做:

首先建立文件夾,將keepalived配置文件進行復制:

mkdir /etc/keepalived

cp /usr/local/keepalived-1.2.18/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/

能夠設置開機啓動:chkconfig keepalived on,到此咱們安裝完畢!

 

第三步:對配置文件進行修改:vim /etc/keepalived/keepalived.conf

keepalived.conf配置文件說明:

(一)Master   (主節點keepalived.conf配置)

! Configuration File for keepalived

global_defs {

   router_id lee ##標識節點的字符串,一般爲hostname

}

## keepalived 會定時執行腳本而且對腳本的執行結果進行分析,動態調整vrrp_instance的優先級。這裏的權重weight 是與下面的優先級priority有關,若是執行了一次檢查腳本成功,則權重會-20,也就是由100 - 20 變成了80,Master 的優先級爲80 就低於了Backup的優先級90,那麼會進行自動的主備切換。

##若是腳本執行結果爲0而且weight配置的值大於0,則優先級會相應增長。

##若是腳本執行結果不爲0 而且weight配置的值小於0,則優先級會相應減小。

vrrp_script chk_nginx {

    script "/etc/keepalived/nginx_check.sh"  ##執行腳本位置

    interval 2 ##檢測時間間隔

    weight -20 ## 若是條件成立則權重減20(-20)

}

## 定義虛擬路由 VI_1爲自定義標識。

vrrp_instance VI_1 {

state MASTER   ## 主節點爲MASTER,備份節點爲BACKUP

    ## 綁定虛擬IP的網絡接口(網卡),與本機IP地址所在的網絡接口相同(我這裏是eth0)

interface eth0

virtual_router_id 123  ## 虛擬路由ID號

    mcast_src_ip 192.168.1.123 ## 本機ip地址

    priority 100  ##優先級配置(0-254的值)

    Nopreempt  ##

    advert_int 1 ## 組播信息發送間隔,倆個節點必須配置一致,默認1s

    authentication { 

        auth_type PASS

        auth_pass bhz ## 真實生產環境下對密碼進行匹配

    }

    track_script {

        chk_nginx

    }

    virtual_ipaddress {

        192.168.1.125 ## 虛擬ip(vip),能夠指定多個

    }

}

 

(二)Backup(從節點配置文件keepalived.conf)

! Configuration File for keepalived

global_defs {

   router_id ubuntu

}

vrrp_script chk_nginx {

    script "/etc/keepalived/nginx_check.sh"

    interval 2

    weight -20

}

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 124

    mcast_src_ip 192.168.1.124

    priority 90 ##優先級配置

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass bhz

    }

    track_script {

        chk_nginx

    }

    virtual_ipaddress {

        192.168.1.125

    }

}

 

(三)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

 

(四)

把master的keepalived配置文件 copy到master機器(172)的 /etc/keepalived/ 文件夾下,

在把backup的keepalived配置文件copy到backup機器(173)的 /etc/keepalived/ 文件夾下,

最後把nginx_check.sh腳本分別copy到兩臺機器的 /etc/keepalived/文件夾下。

 

(五)

nginx_check.sh腳本受權。賦予可執行權限:chmod +x /etc/keepalived/nginx_check.sh

 

(六)

啓動2臺機器的nginx以後。咱們啓動兩臺機器的keepalived

  /usr/local/nginx/sbin/nginx

  service keepalived start

  ps -ef | grep nginx

  ps -ef | grep keepalived

  能夠進行測試,首先看一下倆臺機器的ip a 命令下 都會出現一個虛擬ip,咱們能夠停掉  一個機器的keepalived,而後測試,命令:service keepalived stop。結果發現當前停掉的機器已經不可用,keepalived會自動切換到另外一臺機器上

 

 

(七)

咱們能夠測試在nginx出現問題的狀況下,實現切換,這個時候咱們只須要把nginx的配置文件進行修改,讓其變得不可用,而後強殺掉nginx進程便可,發現也會實現自動切換服務器節點。

相關文章
相關標籤/搜索