Nginx + keepalived 實現雙機熱備

Nginx+Keepalived雙機熱備說明

    理論

    介紹

    nginx:是一款工作在應用層的web服務器,主要用於反向代理、高併發,前臺處理靜態頁面功能。

    Keepalived:是一個很有效的作爲HA的軟件,可以確保線上所有服務的運行狀態以及出現問題能夠及時解決的開源產品。

    爲什麼要用Nngix+Keepalived

    假如,我的前端有兩個nginx做爲處理高併發的數據應用,當大量訪問請求出現時,兩臺nginx誰先誰後都不能很好的處理,而且其中一臺down掉,還得需要運維人員手動處理服務的開關,並且不能實時地監測服務的負載。因此,才需要keepalived這款高性能的HA產品來解決處理這些問題

   實踐

   兩臺服務器作爲前端的nginxIP分別爲:

             192.168.1.100  mast

             192.168.1.102  backup

   虛擬IP爲:192.168.1.24

   開始安裝配置:

   爲兩臺服務器安裝相應的依賴包:

   [root@localhost home]# yum -y install gcc* openssl*

   

下載並安裝nginx

  因爲nginx需要pcre模塊的支持,所以先安裝pcre後裝nginx 

  [root@localhosthome]#wget http://blog.s135.com/soft/linux/nginx_php/pcre/pcre/pcre-8.20.tar.gz

   [root@localhost home]# tar -zxvf pcre-8.20.tar.gz

   [root@localhost home]# cd pcre-8.20

   [root@localhost pcre-8.20]# ./configure

   [root@localhost pcre-8.20]# make && make install

   [root@localhost home]#wget http://wiki.nginx.org/NginxChs/nginx-1.2.3

   [root@localhost home]# tar -zxvf nginx-1.2.3.tar.gz

   [root@localhost home]#groupadd www

   [root@localhost home]#useradd -g  www  www

   [root@localhost home]#chown -R www:www /data/logs/

   [root@localhost nginx-1.2.3]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/home/pcre-8.20/ --with-pcre-jit

  [root@localhost nginx-1.2.3]#make && make install

  

配置nginx.conf

   vim /usr/local/nginx/conf/nginx.conf

user  www www; #運行用戶、組 此爲上面建立的用戶名、用戶組

worker_processes  8;# 啓動進程,一般設置成跟服務器的CPU核數相同或者比它高一些也行

pid        /usr/local/nginx/logs/nginx.pid;#nginx的pid路徑

events {  #工作模式及連接數上限

    worker_connections  1024; ;#單個後臺worker process進程的最大併發鏈接數

    use epoll; #epoll是多路複用IO(I/O Multiplexing)中的一種方式,但是僅用於linux2.6以上內核,可以大大提高nginx的性能

    

}

http { #設定http服務器,利用它的反向代理功能提供負載均衡支持

    include       mime.types; #設定mime類型,類型由mime.type文件定義

    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;

#指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,
    #必須設爲 on,如果用來進行下載等應用磁盤IO重負載應用,可設置爲 off,以平衡磁盤與網絡I/O處理速度,降低系統的uptime.

    tcp_nopush     on; 

    tcp_nodelay on;

    #keepalive_timeout  0;

    keepalive_timeout  65; #連接超時時間

    gzip  on; #開啓gzip壓縮

    upstream srtweb {  #指定負載的Ip

    server 192.168.1.24;

}

    server { 

        listen       80; #指定監聽的端口

        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;

        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

        #

        #location ~ \.php$ {

        #    proxy_pass   http://127.0.0.1;

        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #

        #location ~ \.php$ {

        #    root           html;

        #    fastcgi_pass   127.0.0.1:9000;

        #    fastcgi_index  index.php;

        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

        #    include        fastcgi_params;

        #}

        # deny access to .htaccess files, if Apache's document root

        # concurs with nginx's one

        #

        #location ~ /\.ht {

        #    deny  all;

        #}

    }

    # another virtual host using mix of IP-, name-, and port-based configuration

    #

    #server {

    #    listen       8000;

    #    listen       somename:8080;

    #    server_name  somename  alias  another.alias;

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

    # HTTPS server

    #

    #server {

    #    listen       443;

    #    server_name  localhost;

    #    ssl                  on;

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;

    #    ssl_prefer_server_ciphers   on;

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

}

2.更改完nginx.conf後,執行

/usr/local/nginx/sbin/nginx

再執行losf -i:80 查看nginx是否已啓動

3.安裝keepalived

[root@localhost home]# wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz

[root@localhost home]# tar -zxvf keepalived-1.2.7.tar.gz

[root@localhost home]# cd keepalived-1.2.7

[root@localhost home]# ./configure --prefix=/usr/local/keepalived

[root@localhost home]# make && make install

[root@localhost home]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin

[root@localhost home]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

[root@localhost home]# cp /usr/local/keepalived/etc/rc.d/init.d/

4.分別設置主和備nginx上的keepalived配置文件。

  [root@localhost home]# mkdir /etc/keepalived

  [root@localhost home]# cd /etc/keepalived/

  [root@localhost keepalived]# vim keepalived.conf(此爲新的)

  ! Configuration File for keepalived

     global_defs {

     router_id nginx-proxy-ha

     }

 

vrrp_script check_nginx {

     script "/nginx_ip.sh" #監測腳本

     interval 2

     weight 2

     }

 

vrrp_instance VI_1 {

     state BAKEUP #主備名稱

     interface eth0 #監測網卡

     virtual_router_id 51 #認證的ID

     priority 20 #優先級,值越高越優先

     advert_int 1

     authentication {

     auth_type PASS

     auth_pass 1234 #認證密碼

     }

 

track_interface { 

     eth0  

     } 

 

track_script {

     check_nginx

     }

 

virtual_ipaddress {

     192.168.1.24 #vip地址

     }

}

在這裏,我分別在主備兩臺keepalived機器上嵌入了腳本,得以完善

主機:

[root@localhost /]# cat nginxpid.sh 

#!/bin/bash

while :

do 

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

if [ $nginxpid -eq 0 ];then

/usr/local/nginx/sbin/nginx

sleep 3

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

if [ $nginxpid -eq 0 ];then

/etc/init.d/keepalived stop

fi

fi

sleep 3

done

備機:

[root@localhost /]# cat nginx_ip.sh 

#!/bin/bash

while :

do

for((i=1;i<=3;i++));do

ping -c1 192.168.1.23 >& /dev/null

if [ $? -eq 0 ];

then 

break

/etc/init.d/keepalived restart

fi

done

sleep 5

done

5.分別重啓兩臺nginx上的keepalived

[root@localhost /]# /etc/init.d/keepalived restart

這樣nginx+keepalived就配置好了

測試,當我把主nginx關掉以後(keepalived保持開啓狀態)

測試成功!

測試表明:當主nginx遇到不明原因關閉時,keepalived會自動把請求跳轉到備機上,再運用主、備兩機的腳本進行監測,如果主nginx重啓成功,則請求會自動轉到主nginx上。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章