1. 程式人生 > >nginx高可用之keepalived

nginx高可用之keepalived

                                搭建nginx高可用-keepalived

Keepalived高可用故障切換轉移原理

Keepalived高可用服務對之間的故障切換轉移,是通過VRRP來實現的。在keepalived服務工作時,主Master節點會不斷地向備節點發送(多播的方式)心跳訊息,用來告訴備Backup節點自己還活著。當主節點發生故障時,就無法傳送心跳的訊息了,備節點也因此無法繼續檢測到來自主節點的心跳了。於是就會呼叫自身的接管程式,接管主節點的IP資源和服務。當主節點恢復時,備節點又會釋放主節點故障時自身接管的IP資源和服務,恢復到原來的備用角色。

方案規劃

VIP

IP

nginx埠

主從

10.216.80.2

10.216.80.46

80

MASTER

10.216.80.2

10.216.80.99

80

BACKUP

  1. 安裝編譯nginx所需要的依賴包

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

可能會出現yum未註冊的問題

出現該情況按照以下步驟解決,未出現請忽略

  • 解除安裝yum相關安裝包

rpm -qa|grep yum|xargs rpm -e --nodeps

  • 安裝yum相關安裝包

      rpm -ivh python-*

      rpm -ivh yum-*

  • 新建repo配置檔案

vi /etc/yum.repos.d/CentOS-Base.repo

進入編輯模式(鍵盤輸入i)貼上以下配置資訊

#CentOS-Base.repo

#

# The mirror system uses the connecting IP address of the client and the

# update status of each mirror to pick mirrors that are updated to and

# geographically close to the client.  You should use this for CentOS updates

# unless you are manually picking other mirrors.

#

# If the mirrorlist= does not work for you, as a fall back you can try the

# remarked out baseurl= line instead.

#

#

[base]

name=CentOS-$7 - Base - 163.com

#mirrorlist=http://mirrorlist.centos.org/?release=$7&arch=$basearch&repo=os

baseurl=http://mirrors.163.com/centos/7/os/$basearch/

gpgcheck=1

gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

 

#released updates

[updates]

name=CentOS-$7 - Updates - 163.com

#mirrorlist=http://mirrorlist.centos.org/?release=$7&arch=$basearch&repo=updates

baseurl=http://mirrors.163.com/centos/7/updates/$basearch/

gpgcheck=1

gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

 

#additional packages that may be useful

[extras]

name=CentOS-$7 - Extras - 163.com

#mirrorlist=http://mirrorlist.centos.org/?release=$7&arch=$basearch&repo=extras

baseurl=http://mirrors.163.com/centos/7/extras/$basearch/

gpgcheck=1

gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

 

#additional packages that extend functionality of existing packages

[centosplus]

name=CentOS-$7 - Plus - 163.com

baseurl=http://mirrors.163.com/centos/7/centosplus/$basearch/

gpgcheck=1

enabled=0

gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

esc鍵 輸入 :wq 儲存退出

  • 清楚快取

yum clean all

  • 安裝nginx相關依賴包

中間可能會出現以下情形,輸入y繼續安裝

 

安裝nginx(離線)

  • 將nginx-1.10.2.tar.gz安裝包移動至/usr/local/src/目錄下(可以藉助ssh工具)
  • 編譯安裝nginx
    • cd /usr/local/src/
    • tar -zxvf nginx-1.10.2.tar.gz
    • cd nginx-1.10.2
    • ./configure --prefix=/usr/local/nginx
    • make& make install

為了方便測試,將nginx的index.html稍作修改以便區分

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

安裝keepalived(離線)

  • 將keepalived-2.0.10.tar.gz安裝包移動至/usr/local/src/目錄
  • 編譯安裝keepalived
    • cd /usr/local/src/
    • tar -zxvf keepalived-2.0.10.tar.gz
    • cd keepalived-2.0.10
    • ./configure --prefix=/usr/local/keepalived
    • make && make install
  • 將keepalived安裝成linux服務
    • 複製keepalived服務指令碼到系統服務地址
      • mkdir /etc/keepalived

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

      • cp /usr/local/src/keepalived-2.0.10/keepalived/etc/init.d/keepalived  /etc/init.d/

      • cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

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

    • 可以設定keepalived服務開機啟動
      • chkconfig keepalived on

  • 配置說明
    • MASTER節點配置檔案(10.216.80.46)

vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

   router_id 10.216.80.46

}

vrrp_script chk_nginx {

script "/etc/keepalived/nginx_check.sh"

interval 3

weight -20

}

vrrp_instance VI_1 {

    state MASTER

    interface ens33

    virtual_router_id 15

    priority 150

    unicast_src_ip  10.216.80.46

    unicast_peer {

            10.216.80.99

    }   

    advert_int 2

    authentication {

        auth_type PASS

        auth_pass 1234

    }

    virtual_ipaddress {

        10.216.80.2

    }

    track_script {

        chk_nginx

    }

}

  • BACKUP節點配置檔案(10.216.80.99)

vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

   router_id 10.216.80.99

}

vrrp_script chk_nginx {

script "/etc/keepalived/nginx_check.sh"

interval 3

weight -20

}

vrrp_instance VI_1 {

    state BACKUP

    interface ens33

    virtual_router_id 15

    priority 100

    unicast_src_ip  10.216.80.99

    unicast_peer {

            10.216.80.46

    }   

    advert_int 2

    authentication {

        auth_type PASS

        auth_pass 1234

    }

    virtual_ipaddress {

        10.216.80.2

    }

track_script {

        chk_nginx

    }

}

注意事項:

  1. Interface 後面的值是當前使用的網絡卡資訊

檢視本機網絡卡命令 : watch cat /proc/net/dev

哪張網絡卡的流量變化大一般就是哪張網絡卡是線上使用的

  • 編寫nginx狀態檢測指令碼

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

B=`ps -C nginx --no-header |wc -l`

if [ $B -eq 0 ];then

service keepalived stop

else

exit 0

fi

fi

Keepalived+nginx高可用測試

同時啟動10.216.80.46和10.216.80.99的keepalived,nginx會通過檢測指令碼自動啟動

service keepalived start

通過10.216.80.2訪問,如下

模擬主節點nginx掛掉後自動重啟nginx

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

2秒內通過10.216.80.2訪問,如下

2秒後通過10.216.80.2訪問,如下

nginx恢復

模擬主節點nginx掛掉後自動重啟失敗,2秒內多次執行命令

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

再次通過10.216.80.2訪問,如下

自動切換至從節點服務

模擬主節點keepalived宕機 ,關閉keepalived,nginx服務

service keepalived stop

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

再次通過10.216.80.2訪問,如下

成功切換從節點

模擬主節點keepalived重新啟動

service keepalived start

再次通過10.216.80.2訪問,如下

成功切換主節點