码迷,mamicode.com
首页 > 其他好文 > 详细

nginx+keepalived实现负载均衡

时间:2015-05-28 18:25:54      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:lnmp

环境需求:

hostname:web1    ip:192.168.1.241

hostname:ha1     ip:192.168.1.243 

hostname:ha2     ip:192.168.1.242

keepalived        vip:192.168.1.245


1.nginx+upstream安装(ha1和ha2配置一样)

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

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

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

[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正常访问

技术分享

(3)测试ha1负载web1

技术分享

(3)测试ha2负载web1

技术分享

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

技术分享

技术分享


以上可以看出虚拟ip在ha1上

技术分享

技术分享

以上可以看出,nginx断开后,虚拟ip漂移到ha2上

技术分享

由上图可见,利用vip访问,负载正常
























本文出自 “Linux艺术(Q群:1991706)” 博客,请务必保留此出处http://304076020.blog.51cto.com/7503470/1655982

nginx+keepalived实现负载均衡

标签:lnmp

原文地址:http://304076020.blog.51cto.com/7503470/1655982

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!