歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux管理 >> Linux配置

Nginx+Keepalived高可用架構平台

Nginx高性能、穩定性成為IT人士青睐的http和反向代理服務器。今天我們來一起來溫習一下Nginx+Keepalived高可用配置,鑒於互聯網類似的優秀文章很多,這裡僅供參考和交流。

一、實驗環境:

系統環境:CentOS 6.0 x86_64
Nginx版本:nginx v1.2.4
Keepalived版本:keepalived v1.2.1
Nginx-1:192.9.117.160 (Master)
Nginx-2:192.9.117.161 (Backup)

二、Nginx安裝配置:

分別在兩台服務器安裝Nginx、keepalived,如下:
yum install -y pcre-devel   安裝perl 兼容的正規表達式庫
tar -xzf nginx-1.2.4.tar.gz && cd nginx-1.2.4 ; sed -i -e 's/1.2.2//g' -e 's/nginx\//TDTWS/g' -e 's/"NGINX"/"TDTWS"/g' src/core/nginx.h &&./configure --prefix=/usr/local/nginx --user=www --group=www  --with-http_stub_status_module --with-http_ssl_module

三、Keepalived安裝配置:

tar -xzvf keepalived-1.2.1.tar.gz &&cd keepalived-1.2.1 && ./configure && make && make install
if
[ $? -eq 0 ];then
echo "Install keepalived success,please waiting configure keepalived ..............."
else
echo "Install keepalived failed ,please check install version !"
exit 0
fi
DIR=/usr/local/ ;cp $DIR/etc/rc.d/init.d/keepalived  /etc/rc.d/init.d/ ; cp $DIR/etc/sysconfig/keepalived /etc/sysconfig/ ; mkdir -p /etc/keepalived ; cp $DIR/sbin/keepalived /usr/sbin/ 

Nginx、Keepalived軟件安裝完畢,接下來進行詳細配置。

 四、配置Keepalived:

兩台服務器端keepalived.conf內容都為如下,都設置為backup,不搶占,注意修改優先級不同:

! Configuration File for keepalived
 global_defs {
  notification_email {
      [email protected]
 }
    notification_email_from [email protected]
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
 }
 vrrp_script chk_nginx {
    script "/data/sh/check_nginx.sh"
    interval 2
    weight 2
 }
 # VIP1
 vrrp_instance VI_1 {
     state BACKUP
     interface eth0
     lvs_sync_daemon_inteface eth0
     virtual_router_id 151
     priority 100
     advert_int 5
     nopreempt
     authentication {
         auth_typePASS
         auth_pass  1111
     }
     virtual_ipaddress {
         192.9.117.162
     }
     track_script {
     chk_nginx
    }
 }

如上配置,我們需要自己建立check_nginx腳本,以方便檢查本地Nginx是否存活更好的切換。Check_nginx.sh腳本內容如下:

#!/bin/bash
#auto check nginx  process
#2012-10-16 wugk
 killall  -0   nginx
 if
 [[ $? -ne 0 ]];then
 /etc/init.d/keepalived stop
 fi

五、Nginx 配置:

在兩台Nginx服務器分別新建index.html測試頁面,然後啟動Nginx服務測試。
訪問VIP http://192.9.117.162 如下:
Down掉192.9.117.160的Nginx服務,分別查看keepalived後台日志和ping VIP日志如下:
Oct 16 16:47:48 localhost Keepalived: Stopping Keepalived v1.2.1 (10/16,2012)
Oct 16 16:47:49 localhost Keepalived_vrrp: Terminating VRRP child process on signal
Copyright © Linux教程網 All Rights Reserved