nginx+keepalived實現負載均衡

環境需求:javascript

hostname:web1    ip:192.168.1.241css

hostname:ha1     ip:192.168.1.243 java

hostname:ha2     ip:192.168.1.242nginx

keepalived        vip:192.168.1.245c++


1.nginx+upstream安裝(ha1和ha2配置同樣)web

[root@ha1 opt]# yum -y install gcc gcc-c++ openssl openssl-develapp

[root@ha1 opt]# useradd nginx -s /sbin/nologintcp

[root@ha1 opt]# tar zxf pcre-8.34.tar.gz ide

[root@ha1 opt]# cd pcre-8.34測試

[root@ha1 pcre-8.34]# ./configure 

[root@ha1 pcre-8.34]# make && make install

[root@ha1 opt]# tar zxf tengine-1.5.1.tar.gz 

[root@ha1 opt]# cd tengine-1.5.1

[root@ha1 tengine-1.5.1]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

[root@ha1 tengine-1.5.1]# make && make install

      

[root@ha1 ~]# mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak

[root@ha1 ~]# vi /usr/local/nginx/conf/nginx.conf(新建nginx配置文件)

添加一下配置

user nginx nginx;

worker_processes auto;

worker_rlimit_nofile 65535;

error_log /var/log/www/error.log;

#pid      logs/nginx.pid;

events {

    use epoll;

    worker_connections  51200;

}

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"';

 


    server_info  off;

    server_tag   off;

    server_name_in_redirect off;

    access_log  /var/log/www/access.log  main;

    client_max_body_size 20m;

    client_header_buffer_size 16k;

    large_client_header_buffers 4 16k;

    sendfile        on;

    tcp_nopush     on;

    keepalive_timeout  65;

    server_tokens on; 

    gzip  on;

    gzip_min_length 1k;

    gzip_buffers 4 16k;

    gzip_proxied   any;

    gzip_http_version 1.1;

    gzip_comp_level 3;

    gzip_types text/plain application/x-javascript text/css application/xml;

    gzip_vary on;

    upstream kim {

consistent_hash $request_uri;

       server 192.168.1.241:80;

check interval=3000 rise=2 fall=5 timeout=1000 type=http;

        check_http_send "GET / HTTP/1.0\r\n\r\n"; 

        check_http_expect_alive http_2xx http_3xx; 

    }

    server {

        listen       80;

        server_name  localhost;

        location / {

        proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504;

        proxy_pass http://kim;

        proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

    }

    }

[root@ha1 conf]# vi /etc/init.d/nginxd(新建nginx啓動文件)

添加如下配置

#! /bin/sh

# chkconfig: 2345 55 25

# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and

# run 'update-rc.d -f nginx defaults', or use the appropriate command on your

# distro. For CentOS/Redhat run: 'chkconfig --add nginx'


### BEGIN INIT INFO

# Provides:          nginx

# Required-Start:    $all

# Required-Stop:     $all

# Default-Start:     2 3 4 5

# Default-Stop:      0 1 6

# Short-Description: starts the nginx web server

# Description:       starts nginx using start-stop-daemon

### END INIT INFO


PATH=/usr/local/nginx/sbin:/usr/local/nginx/bin:/sbin:/bin:/usr/sbin:/usr/bin

DESC="nginx daemon"

NAME=nginx

DAEMON=/usr/local/nginx/sbin/$NAME

CONFIGFILE=/usr/local/nginx//conf/$NAME.conf

PIDFILE=/usr/local/nginx//logs/$NAME.pid

SCRIPTNAME=/etc/init.d/$NAME


set -e

[ -x "$DAEMON" ] || exit 0


do_start() {

 $DAEMON -c $CONFIGFILE || echo -n "nginx already running"

}


do_stop() {

 kill -INT `cat $PIDFILE` || echo -n "nginx not running"

}


do_reload() {

 kill -HUP `cat $PIDFILE` || echo -n "nginx can't reload"

}


case "$1" in

 start)

 echo -n "Starting $DESC: $NAME"

 do_start

 echo "."

 ;;

 stop)

 echo -n "Stopping $DESC: $NAME"

 do_stop

 echo "."

 ;;

 reload|graceful)

 echo -n "Reloading $DESC configuration..."

 do_reload

 echo "."

 ;;

 restart)

 echo -n "Restarting $DESC: $NAME"

 do_stop

 do_start

 echo "."

 ;;

 *)

 echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2

 exit 3

 ;;

esac


exit 0


[root@ha1 keepalived]# chmod o+x /etc/init.d/nginxd  (賦予執行權限)

[root@ha1 keepalived]# /etc/init.d/nginxd start

2.測試nginx

(1)測試web1正常訪問

wKioL1VmxNqwD18YAACk3MlGN5s622.jpg

(3)測試ha1負載web1

wKiom1Vmw1ix29HXAACoBusvuWQ263.jpg

(3)測試ha2負載web1

wKiom1Vmw4jCYH4aAACh1ZJ6LVM207.jpg

3.安裝keepalived(ha1與ha2同樣配置)

[root@ha1 ~]# yum -y install keepalived (要保證ha1與ha2上的keepalived版本一致)

[root@ha1 ~]# mkdir /etc/keepalived/script (新建nginx檢車腳本存放目錄)

[root@ha1 ~]# vi /etc/keepalived/script/check_nginx.sh  (新建nginx檢測腳本)

添加如下內容

!/bin/sh

# check nginx server status

NGINX=/usr/local/nginx/sbin/nginx

PORT=80


nmap localhost -p $PORT | grep "$PORT/tcp open"

#echo $?

if [ $? -ne 0 ];then

    $NGINX -s stop

    sleep 3

    nmap localhost -p $PORT | grep "$PORT/tcp open"

    [ $? -ne 0 ] && /etc/init.d/keepalived stop

fi


[root@ha1 ~]# chmod o+x /etc/keepalived/script/check_nginx.sh (賦予執行權限)

(1)新建keepalived配置文件(在ha1上配置)

[root@ha1 ~]# mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

[root@ha1 ~]# vi /etc/keepalived/keepalived.conf

添加如下配置

! Configuration File for keepalived

global_defs {

   notification_email {

    root@localhost

}

   notification_email_from kim@163.com

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

vrrp_script chk_http_port {

    script "/etc/keepalived/script/check_nginx.sh"

    interval 2

    weight 2

}

vrrp_instance VI_1 {

    state MASTER

    interface eth1

    virtual_router_id 56

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1234

    }

track_script {

    chk_http_port

}

    virtual_ipaddress {

    192.168.1.245

    }

}


(2)新建keepalived配置文件(在ha2上配置)

[root@ha2 ~]# mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

[root@ha2 ~]# vi /etc/keepalived/keepalived.conf

添加如下配置

! Configuration File for keepalived

global_defs {

   notification_email {

    root@localhost

}

   notification_email_from kim@163.com

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

vrrp_script chk_http_port {

    script "/etc/keepalived/script/check_nginx.sh"

    interval 2

    weight 2

}


vrrp_instance VI_1 {

    state BACKUP

    interface eth1

    virtual_router_id 56

    priority 99

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1234

    }

track_script {

    chk_http_port

}

    virtual_ipaddress {

    192.168.1.245

    }

}


4.測試keepalived

wKioL1VmxTSAxH9RAAIH0R2uIE4625.jpg

wKiom1Vmw_Wwlp5XAAGdD2D5xAI033.jpg


以上能夠看出虛擬ip在ha1上

wKiom1VmxCTxxe0eAAIoSNqKyPQ264.jpg

wKioL1VmxbaCddJQAAHKiUrbR3c276.jpg

以上能夠看出,nginx斷開後,虛擬ip漂移到ha2上

wKiom1VmxEvyQSarAAClbPtLPCs060.jpg

由上圖可見,利用vip訪問,負載正常

相關文章
相關標籤/搜索