nginx负载均衡+keepalived高可用

参考:http://litaotao.blog.51cto.com/6224470/1302100

https://www.cnblogs.com/youzhibing/p/7327342.html

一环境说明

192.168.127.129 (master nginx+keepalived)
192.168.127.130 (backup nginx+keepalived )
192.168.127.134 (web)
192.168.127.138 (web)
vip 192.168.127.100 (实际环境中这个ip需要是个公网地址供客户端访问)

二 要验证的功能有:

1)、Master服务器没挂,则Master占有vip且nginx运行在Master上
2)、Master服务器挂了,则backup抢占vip且在backup上运行nginx服务
3)、如果master服务器上的nginx服务挂了,则vip资源转移到backup服务器上
4)、检测后端服务器的健康状态
说明:
Master和Backup两边都开启nginx服务,无论Master还是Backup,当其中的一个keepalived服务停止后,vip都会漂移到keepalived服务还在的节点上,
如果要想使nginx服务挂了,vip也漂移到另一个节点,则必须用脚本或者在配置文件里面用shell命令来控制。

三 安装nginx和keepalived(主):

3.1 安装keepalived和编译安装nginx

yum -y install keepalived
[ ~]#tar xf nginx-1.4.2.tar.gz
[ ~]#yum -y groupinstall "Development tools" "Server  Platform Development"
[ ~]#yum -y install pcre-devel
[ ~]# cd nginx-1.4.2
[ nginx-1.4.2]# groupadd nginx
[ nginx-1.4.2]# useradd -r -g nginx nginx
[ nginx-1.4.2]#./configure --prefix=/usr--sbin-path=/usr/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-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/--http-proxy-temp-path=/var/tmp/nginx/proxy/--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi--http-scgi-temp-path=/var/tmp/nginx/scgi--with-pcre

make && make install

3.2 编辑nginx的启动脚本

vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"

start() {
        echo -n $"Starting $prog: "
        mkdir -p /dev/shm/nginx_temp
        daemon $NGINX_SBIN -c $NGINX_CONF
        RETVAL=$?
        echo
        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -TERM
        rm -rf /dev/shm/nginx_temp
        RETVAL=$?
        echo
        return $RETVAL
}

reload(){
        echo -n $"Reloading $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -HUP
        RETVAL=$?
        echo
        return $RETVAL
}

restart(){
        stop
        start
}

configtest(){
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}

3.3 修改keepalived.conf配置文件

global_defs {
   notification_email {
     
   }
   notification_email_from 
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LTT
}
vrrp_script chk_nginx {  #检测nginx服务是否在运行有很多方式,比如进程,用脚本检测等等
   script "killall -0 nginx"  #用shell命令检查nginx服务是否存在
   interval 1  #时间间隔为1秒检测一次
   weight -2   #当nginx的服务不存在了,就把当前的权重-2
   fall 2      #测试失败的次数
   rise 1      #测试成功的次数
}
vrrp_instance IN_1 {
    state MASTER
    interface eth0
    virtual_router_id 22
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass aaaa
    }
    virtual_ipaddress {
        192.168.127.100 #虚拟ip
    }
   track_script {
    chk_nginx   #引用上面的vrrp_script定义的脚本名称
}
}

3.4 启动keepalive和nginx服务

/etc/init.d/nginx start
/etc/init.d/keepalived start

四 安装nginx和keepalived(从)

4.1、安装keepalived和编译安装nginx

同master一样(第一步和第二步)

4.2 修改keepalived配置文件

先从master上面拷贝这个配置文件然后更改
vim keepalived.conf #此配置文件是从Master服务器上copy过来,只需小小改动
state BACKUP #把这里原先的MASTER改成BACKUP
priority 99 #把这里原先的100改成99

4.3 启动服务

/etc/init.d/nginx start
/etc/init.d/keepalived start

##############################################################################################################

五 配置两个后端的web服务器

134 :
yum install httpd -y
然后进入到/var/www/html/里面建立一个测试页
vi index.html
134
最后启动服务 /etc/init.d/httpd start

138:
yum install httpd -y
然后进入到/var/www/html/里面建立一个测试页
vi index.html
138
最后启动服务 /etc/init.d/httpd start
注意:两个测试页不同有助于我们分辨是否成功,当然实际环境中都是一样的

六 配置nginx负载均衡

master:
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream apacheweb {  #定义负载均衡的模块
        server 192.168.127.134:80 max_fails=3 fail_timeout=2s;
        server 192.168.127.138:80 max_fails=3 fail_timeout=2s;
     }
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
             proxy_pass http://apacheweb;

        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

backup:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream apacheweb {  #定义负载均衡的模块
        server 192.168.127.134:80 max_fails=3 fail_timeout=2s;
        server 192.168.127.138:80 max_fails=3 fail_timeout=2s;
     }
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://apacheweb;

        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

然后两边分别重启nginx服务

七 测试

1 把master上面的nginx服务停掉,看客户端能否正常访问网站并且查看vip是否会移动到backup上面
2 把master 上面的keepalived 服务停掉 看客户端能否正常访问网站并且查看vip是否会移动到backup上面
3 把其中一个web服务停掉,看是否还能正常访问web
4测试的时候发现每刷新一次页面就切换一次,lvs不是切换页面

相关推荐