歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Nginx配合keepalived實現雙主負載均衡

Nginx配合keepalived實現雙主負載均衡

日期:2017/2/27 15:53:29   编辑:Linux教程
前 言:由於之前一直使用的是主從高可用加後端負載,隨著業務量的增大,前端一台服務在高峰時期有些吃力,所有對之前架構進行了一點改造,把主從升級成了雙 主,可以更充分的利用現有服務器資源,由於實驗環境有限,後端的測試僅僅使用了一個靜態頁面,沒有搭建動態環境,也沒用使用數據庫,如有需要可以參考其他 文檔。

一、架構規劃

1、服務器IP地址規劃

VIP1:192.168.1.149
VIP2:192.168.1.150
Keepalived1:192.168.1.151
Keepalived2:192.168.1.152
WebServer1:192.168.1.201
WebServer2:192.168.1.202

2、服務器操作系統

所使用的操作系統均為CentOS release 6.6 (Final) x86_64,最小化安裝。

3、網絡拓撲圖

wKiom1Ud21zQmnLeAAI0q4It2JA051.jpg

二、配置Nginx代理服務器

此部分Node1與Node2的配置完全相同。

1,准備編譯環境

# yum –y install gccgcc-c++ pcre-devel openssl openssl-devel wget

2,編譯安裝nginx

# ./configure \
 --prefix=/usr/local/nginx \
 --sbin-path=/usr/local/nginx/sbin/nginx \
 --conf-path=/etc/nginx/nginx.conf \
 --error-log-path=/var/log/nginx/error.log \
 --http-log-path=/var/log/nginx/access.log \
 --pid-path=/var/run/nginx/nginx.pid \
 --lock-path=/var/lock/nginx.lock \
 --user=nginx \
 --group=nginx \
 --with-http_ssl_module \
 --with-pcre
# make && make install

3,為nginx提供SysV init腳本:

# vi /etc/rc.d/init.d/nginx

添加如下內容

#!/bin/sh
#
# nginx - this script starts and stopsthe nginx daemon
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no"] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && ./etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
  # make required directories
  user=`nginx -V 2>&1 | grep "configure arguments:" | sed's/[^*]*--user=\([^ ]*\).*/\1/g' -`
  options=`$nginx -V 2>&1 | grep 'configure arguments:'`
  for opt in $options; do
      if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d"=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating"$value
               mkdir -p $value && chown-R $user $value
           fi
      fi
  done
}
start() {
   [ -x $nginx ] || exit 5
   [ -f $NGINX_CONF_FILE ] || exit 6
   make_dirs
   echo -n $"Starting $prog: "
   daemon $nginx -c $NGINX_CONF_FILE
   retval=$?
   echo
   [ $retval -eq 0 ] && touch $lockfile
   return $retval
}
stop() {
   echo -n $"Stopping $prog: "
   killproc $prog -QUIT
   retval=$?
   echo
   [ $retval -eq 0 ] && rm -f $lockfile
   return $retval
}
restart() {
   configtest || return $?
   stop
   sleep 1
   start
}
reload() {
   configtest || return $?
   echo -n $"Reloading $prog: "
   killproc $nginx -HUP
   RETVAL=$?
   echo
}
force_reload() {
   restart
}
configtest() {
 $nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
   status $prog
}
rh_status_q() {
   rh_status >/dev/null 2>&1
}
case "$1" in
   start)
       rh_status_q && exit 0
       $1
       ;;
   stop)
       rh_status_q || exit 0
       $1
       ;;
   restart|configtest)
       $1
       ;;
   reload)
       rh_status_q || exit 7
       $1
       ;;
   force-reload)
       force_reload
        ;;
   status)
       rh_status
       ;;
   condrestart|try-restart)
       rh_status_q || exit 0
            ;;
   *)
       echo $"Usage: $0{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
       exit 2
esac

而後為此腳本賦予執行權限:

# chmod +x /etc/rc.d/init.d/nginx

添加至服務管理列表,並讓其開機自動啟動:

# chkconfig --add nginx
# chkconfig nginx on

4,配置Nginx代理

# cat nginx.conf

user nginx nginx;
    worker_processes 1;
    pid /var/run/nginx/nginx.pid;
    worker_rlimit_nofile 51200;
    events
    {
    use epoll;
    worker_connections 51200;
    }
    http{
    include       mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 8m;
    sendfile on;
    tcp_nopush     on;
    keepalive_timeout 60;
    tcp_nodelay on;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types       text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    upstream backend
    {
    ip_hash;
    server 192.168.1.201:80;
    server 192.168.1.202:80;
    }
    log_format access '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" $http_x_forwarded_for';
    access_log /var/log/nginx/access.log access;
    server {
    listen 80;
    server_name www.test.com;
    location / {
    root /var/www/html ;
    index index.php index.htm index.html;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://backend;
    }
    location /nginx {
    access_log off;
    auth_basic "NginxStatus";
    }
    }
}

三、安裝與配置keepalived

# wgethttp://www.keepalived.org/software/keepalived-1.2.16.tar.gz  
# yum -y install libnl-devel
# ./configure --prefix=/usr/local/keepalived  
# make && make install  
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ 
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ 
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ 
# mkdir /etc/keepalived  
# cd /etc/keepalived/
# vim keepalived.conf

以上步驟在兩台keepalived機器上都需要進行

下面分別是兩節點的配置文件

節點一

! Configuration File for keepalived  
  
global_defs {  
   notification_email {  
         [email protected] #接收警報的email地址,可以添加多個
   }  
   notification_email_from [email protected] #發件人地址
   smtp_connect_timeout 3  #超時時間
   smtp_server 127.0.0.1  #發送郵件的服務器
   router_id LVS_DEVEL  #load balancer的標識ID,用於email警報
}  
vrrp_instance VI_1 {  
    state MASTER
    interface eth0
    virtual_router_id 51 
    priority 100    # 權值要比 back 高
    advert_int 1
    authentication {  
        auth_type PASS  
        auth_pass 123456
    }  
    virtual_ipaddress {  
        192.168.1.149 #vip的地址
  }
}  
vrrp_instance VI_2 {  
    state BACKUP
    interface eth0  
    virtual_router_id 52 
    priority 90
    advert_int 1 
    authentication {  
        auth_type PASS  
        auth_pass 123456 
    }  
    virtual_ipaddress {  
        192.168.1.150
    } 
}

節點二

! Configuration File for keepalived
global_defs {
   notification_email {
         [email protected]
   }
   notification_email_from [email protected]
   smtp_connect_timeout 3
   smtp_server 127.0.0.1
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth1
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.1.149
  }
}
vrrp_instance VI_2 { 
    state MASTER
    interface eth1 
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.1.150
    }
}

為兩節點添加nginx狀態監控腳本,由於keepalived本身不能檢測到nginx的存活狀態,需要借助於第三方腳本來實現,下面是出自余洪春前輩的一個檢測腳本,在這裡借用一下。

# vim /home/nginx_chk.sh

#!/bin/bash
while  :
do
nginxpid=`ps -C nginx --no-header | wc -l`
 if [ $nginxpid -eq 0 ];then
  /usr/local/nginx/sbin/nginx
  sleep 5
nginxpid=`ps -C nginx --no-header | wc -l`
  echo $nginxpid
    if [ $nginxpid -eq 0 ];then
 /etc/init.d/keepalived stop
   fi
 fi
 sleep 5
done

# chmod +x /home/nginx_chk.sh

後台執行該腳本

# nohup sh /home/nginx_chk.sh &

四、安裝web server

這裡為了測試我直接使用yum安裝兩台服務器上的nginx服務。

增加額外資源庫

# yum -y install yum-priorities
# rpm -Uvh http://mirrors.yun-idc.com/epel/6/x86_64/epel-release-6-8.noarch.rpm 
# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm 
# yum -y install nginx
# /etc/init.d/nginx start


五、啟動測試

首先測試兩台WebServer的可用性

wKioL1UeKUrBMA17AACxu0FghFw261.jpg

wKiom1UeKA3gk3ONAACbtMQm3xQ194.jpg

可以看到兩台WebServer都運行正常

查看兩台keepalived節點的IP情況

wKioL1UeKjmAY_MZAAHOKwQeJhQ530.jpg

wKiom1UeKP6jnUNYAAFKBrJEyuk453.jpg

使用兩個VIP進行訪問

wKiom1UeKYmzned7AACZ7IAvtZo213.jpg

wKioL1UeKsyD-5wWAACw1aWUjJE531.jpg

停掉一台keepalived服務,查看IP

wKioL1UeK0XTGGLgAAHThCn72xU269.jpg

wKiom1UeKgmSVREoAAFAnyLntVY888.jpg

可以看到VIP已經成功流轉到另一節點上,再使用VIP進行訪問,依然可以正常訪問。

wKiom1UeRG_CqgfjAACvK8ka7gQ243.jpg

wKioL1UeRbOAnqLaAACY8tJoJ44482.jpg

這時重新啟動節點一上的keepalived服務,可以看到屬於節點一的VIP又重新回到了節點一上。

wKiom1UeRH2AWF6YAAH_68Uoqww985.jpg

基本配置到這裡就完成了。


原文:http://zhangjianjian.blog.51cto.com/2944771/1627995
Copyright © Linux教程網 All Rights Reserved