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

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



1、環境nginx

5臺虛擬機,分別是:apache

1臺測試機(192.168.2.83);vim

2臺nginx/keepalived(192.168.2.235/192.168.2.236);tomcat

2臺Web Servers(192.168.2.237/192.168.2.238);安全

注:VIP設置爲 192.168.2.229;bash


2、安裝配置Web Server服務器

因爲Web Server的安裝與配置很是簡單,根據本身喜愛,安裝一個便可;好比:apache、nginx、tomcat等等。在此就再也不詳述;app


3、安裝配置Nginx負載均衡

yum -y install gcc vim lrzsz pcre-devel kernel-devel openssl-devel wget

wget http://nginx.org/download/nginx-1.9.0.tar.gz

tar xzvf nginx-1.9.0.tar.gz

cd nginx-1.9.0

./configure --prefix=/usr/local/nginx --with-debug --with-http_stub_status_module --with-stream --with-http_ssl_module

make;make install

ln -s /usr/local/nginx/sbin/nginx  /sbin/

cd /usr/local/nginx/conf

cp nginx.conf nginx.conf.bak

編輯nginx主配置文件nginx.conf,內容大體以下:

worker_processes  10;
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"';
    sendfile        on;
    keepalive_timeout  65;
    upstream html_pool {

          ip_hash;

        server 192.168.2.237:80;
        server 192.168.2.238:80;

          #server 192.168.2.239:80 backup;

          #server 192.168.2.240:80 down;

        }
    server {
        listen       80;
        server_name  localhost;
        location / {
         proxy_pass     http://html_pool;
         proxy_set_header   Host             $host;
         proxy_set_header   X-Real-IP        $remote_addr;
         proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}


注:以上的操做,兩臺都須要操做;到此,咱們的Nginx就安裝及配置好了。


4、安裝配置Keepalived

wget  http://www.keepalived.org/software/keepalived-1.2.15.tar.gz
tar xzvf keepalived-1.2.15.tar.gz
cd keepalived-1.2.15
./configure --sysconf=/etc/ --with-kernel-dir=/usr/src/kernels/2.6.32-504.23.4.el6.x86_64/

#內核參數,根據不一樣的操做系統,會不同,注意更改×××器上的內核路徑便可;

make
make install
ln -s /usr/local/sbin/keepalived /sbin/
cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak

編輯keepalive.conf主配置文件,內容大體以下:

! Configuration File for keepalived

global_defs {
   router_id LVS_DEVEL
}

vrrp_script chk_nginx {
        script "/etc/keepalived/check_nginx.sh"
        interval 2
        weight 2
        }

vrrp_instance VI_1 {
    state MASTER/BACKUP        #前主後備
    interface eth0
    virtual_router_id 60
    priority 100/80        #前主後備
    advert_int 3
    authentication {
        auth_type PASS
        auth_pass 33333
    }
    virtual_ipaddress {
        192.168.2.229
    }
        track_script {
                chk_nginx
        }
}


由於主配置文件裏有用到一個檢測nginx狀態的腳本,因此另外須要建立腳本,內容以下:

# cat check_ngix.sh

#!/bin/bash
nginx_start=`ps -C nginx --no-header |wc -l`
if [ $nginx_start = 0 ]; then
#/etc/init.d/nginx start
/usr/local/nginx/sbin/nginx
sleep 3
nginx_status=`ps -C nginx --no-header |wc -l`
if [ $nginx_status = 0 ]; then
/etc/init.d/keepalived stop
fi
fi


chmod +x /etc/keepalived/check_nginx.sh

注:一樣,以上的全部操做,須要兩臺服務器上同樣操做,注意更改主備及優先級便可;到此,keepalive的相關安裝與配置即OK了;


5、校驗及測試

  1. 啓動keepalived(兩臺):/etc/init.d/keepalived start

  2. 在主服務器上隨意把nginx或者keepalived服務關閉,觀察服務的可用性;

  3. 若是飄到備服務器上了,一樣,在備服務器上隨意把nginx或者keepalived服務關閉,再觀察;

  4. 最後須要注意的是,若是服務器上有啓用安全軟件,好比ipatalbes,必定要讓服務器間相互訪問,不單單是ping通;如:iptables -I INPUT -s 192.168.2.0/24 -j ACCEPT

相關文章
相關標籤/搜索