• Nginx安装负载均衡配置 fair check扩展


    前言

    本文主要是针对Nginx安装、负载均衡配置,以及fair智能选举、check后端节点检查扩展功能如何扩展,进行讲解说明。

    fair模块: upstream-fair,“公平的”Nginx 负载均衡模块,增强了Nginx 提供的round-robin负载均衡算法,可以跟踪后端服务器的负载来分发请求。

    chek模块:nginx_upstream_check_module,更专业的负载均衡器内节点的健康检查。这个由淘宝技术团队开发的 nginx 模块 nginx_upstream_check_module,通过它可以用来检测后端 realserver 的健康状态。如果后端 realserver 不可用,则所有的请求就不会转发到该节点上。

    
    

    目录

    • 前期准备
    • upstream-fair模块
    • upstream_check_module后台服务器健康检测模块
    • Nginx安装
    • 负载均衡配置
    • 心得

    前期准备

    程序

    为了方便安装,所有的源码下载,都放在 /tmp/ 目录下。

    nginx-1.14.0 nginx程序源码

    下载地址:http://nginx.org/download/nginx-1.14.0.tar.gz

    nginx-upstream-fair-master fair模块源码

    官方github下载地址:https://github.com/gnosek/nginx-upstream-fair
    说明:如果从github下载最新版本,在安装到nginx 1.14.0版本时,会报出编译错误。需要对源码做一些修改,修改参照(如果你看到这篇文章时,github主已经修改了该bug,或者你用的是nginx 1.14.0以下版本,请忽视...):https://github.com/gnosek/nginx-upstream-fair/pull/27/commits/ff979a48a0ccb9217437021b5eb9378448c2bd9e
    对于比较懒的童鞋,这里提供了已经修改好的源码包:https://files.cnblogs.com/files/ztlsir/nginx-upstream-fair-master.zip

    nginx_upstream_check_module-master check模块源码

    下载地址:https://github.com/yaoweibin/nginx_upstream_check_module

    Nginx负载均衡反向代理选举方式

    • 轮询(默认)
    • ip_hash
    • weight
    • fair(第三方)
    • url_hash(第三方)。

     

    系统

    Ubuntu 14.04

    upstream-fair模块

    解压 upstream-fair 源码包

    ztlsir@ztlsir-Virtual-Machine:/tmp$ unzip nginx-upstream-fair-master.zip  

     

    upstream_check_module后台服务器健康检测模块

    解压 upstream_check_module 源码包

    ztlsir@ztlsir-Virtual-Machine:/tmp$ unzip nginx_upstream_check_module-master.zip

    给 upstream-fair 扩展 upstream_check_module 的 upstream_fair.patch 补丁。否则,当负载均衡采用fair模式时,fair将会不支持check

    ztlsir@ztlsir-Virtual-Machine:/tmp$ cd nginx-upstream-fair-master/ #进入才将解压的upstream-fair源码目录 
    ztlsir@ztlsir-Virtual-Machine:/tmp/nginx-upstream-fair-master$ patch -p1 < ../nginx_upstream_check_module-master/upstream_fair.patch #打补丁,对patch有疑问的童鞋,自行百度

    Nginx安装

    解压 Nginx 1.14.0 源码包

    ztlsir@ztlsir-Virtual-Machine:/tmp$ tar zxvf nginx-1.14.0.tar.gz

    给Nginx扩展 upstream_check_module 的 check_1.12.1+.patch 补丁。

    ztlsir@ztlsir-Virtual-Machine:/tmp$ cd nginx-1.14.0/ #进入才将解压的nginx源码目录 
    ztlsir@ztlsir-Virtual-Machine:/tmp/nginx-1.14.0$ patch -p1 < ../nginx_upstream_check_module-master/check_1.12.1+.patch #打补丁

    安装Nginx

    ztlsir@ztlsir-Virtual-Machine:/tmp/nginx-1.14.0$ ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --add-module=/tmp/nginx_upstream_check_module-master --add-module=/tmp/nginx-upstream-fair-master  #配置,安装目录:/usr/local/nginx
    ztlsir@ztlsir-Virtual-Machine:/tmp/nginx-1.14.0$ sudo make && sudo make install  #编译、安装,此处用sudo,因为在"/usr/local/"下创建nginx目录需要root权限

    在配置和安装时,可能会出现pcre、ssl、complier、zlib等相关错误,可以参考:Linux Nginx安装以及可能出现错误

    负载均衡配置

    修改安装目录的拥有用户

    ztlsir@ztlsir-Virtual-Machine:/tmp$ cd /usr/local/  #进入到local目录
    ztlsir@ztlsir-Virtual-Machine:/usr/local$ sudo chown -R ztlsir nginx/  #修改nginx安装目录的所有者为ztlsir

     

    修改配置文件

    /usr/local/nginx/nginx.conf:

    #user  nobody;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        check_shm_size 10M;
        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"';
    
        #access_log  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        proxy_next_upstream error timeout invalid_header;
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        #引用vhosts文件夹下的配置文件
        include vhosts/*.conf;
     
    }

    /usr/local/nginx/vhosts的目录文件

    ztlsir@ztlsir-Virtual-Machine:/usr/local/nginx/conf/vhosts$ ls
    1001.conf  1002.conf  1003.conf

    vhosts是自定义的文件夹。在该文件夹下,是根据端口进行负载配置的,此处选取其中一个文件进行展示,其他文件都大同小异。

    /usr/local/nginx/vhosts/1001.conf:

        upstream localhost1001 {
                fair;
                server  192.168.120.10:1001;
                server  192.168.120.11:1001;
                check interval=3000 rise=2 fall=5 timeout=1000 type=http;
                #在Web服务配置了默认文档时,可以使用如下配置
                #但当服务没有配置时,需要修改为:check_http_send "GET /temp.html HTTP/1.0
    
    ";
                #其中“/temp.html”表示根目录下的一个temp.html文件
                check_http_send "HEAD / HTTP/1.0
    
    ";
                check_http_expect_alive http_2xx http_3xx;
        }
        server {
            listen       1001;
            server_name  localhost;
    
            #charset koi8-r;
        
            location / {
                root   html;
                index  index.html index.htm;
                proxy_pass  http://localhost1001;
                proxy_redirect  off;
                proxy_set_header Host $host:1001;
                proxy_connect_timeout       100s;
                proxy_read_timeout          150s;
                proxy_send_timeout          100s;
            }
            #error_page  404              /404.html;
            # redirect server error pages to the static page /50x.html
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        } 

     在如上配置中,负载了两个后端节点: 192.168.120.10:1001 和192.168.120.11:1001 ,负载选举模式采用 fair 

     check 配置了健康检查的基本规则。 interval=3000 每隔3秒(3000毫秒)检测一次, rise=2 请求2次正常则标记 realserver状态为up, fall=5 检测 5 次都失败则标记 realserver的状态为down, timeout=1000 超时时间为1秒,超时即失败。以下为详细说明:

    Syntax: check interval=milliseconds [fall=count] [rise=count] [timeout=milliseconds] [default_down=true|false] [type=tcp|http|ssl_hello|mysql|ajp] [port=check_port]
    Default: 如果没有配置参数,默认值是:interval=30000 fall=5 rise=2 timeout=1000 default_down=true type=tcp
    Context: upstream

    Check指令参数意义: - interval:向后端发送的健康检查包的间隔。 - fall(fall_count): 如果连续失败次数达到fall_count,服务器就被认为是down。 - rise(rise_count): 如果连续成功次数达到rise_count,服务器就被认为是up。 - timeout: 后端健康请求的超时时间。 - default_down: 设定初始时服务器的状态,如果是true,就说明默认是down的,如果是false,就是up的。默认值是true,也就是一开始服务器认为是不可用,要等健康检查包达到一定成功次数以后才会被认为是健康的。 - type:健康检查包的类型,现在支持以下多种类型 - tcp:简单的tcp连接,如果连接成功,就说明后端正常。 - ssl_hello:发送一个初始的SSL hello包并接受服务器的SSL hello包。 - http:发送HTTP请求,通过后端的回复包的状态来判断后端是否存活。 - mysql: 向mysql服务器连接,通过接收服务器的greeting包来判断后端是否存活。 - ajp:向后端发送AJP协议的Cping包,通过接收Cpong包来判断后端是否存活。 - port: 指定后端服务器的检查端口。你可以指定不同于真实服务的后端服务器的端口,比如后端提供的是443端口的应用,你可以去检查80端口的状态来判断后端健康状况。默认是0,表示跟后端server提供真实服务的端口一样。该选项出现于Tengine-
    1.4.0。

     check_http_send 配置了健康检查包发送的内容。 "HEAD / HTTP/1.0 " 表示采用 HEAD 方法,访问路径为 / 程序根目录(默认文档)。以下为详细说明:

    Syntax: check_http_send http_packet
    Default: "GET / HTTP/1.0
    
    "
    Context: upstream
    
        -http_packet:当采用长连接进行健康检查时,需在该指令中添加keep-alive请求头,如:"HEAD / HTTP/1.1
    Connection: keep-alive
    
    "。 同时,在采用"GET"方法的情况下,请求uri的size不宜过大,确保可以在1个interval内传输完成,否则会被健康检查模块视为后端服务器或网络异常

     check_http_expect_alive 指定HTTP回复的成功状态,默认认为2XX和3XX的状态是健康的。以下为详细说明:

    Syntax: check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]
    Default: http_2xx | http_3xx
    Context: upstream

    关于 check 模块的详细说明,可以参照:ngx_http_upstream_check_module

    心得

    在实现fair和check模块时,走了很多坑。最初想着直接用淘宝的Tengine就不用安装check模块了,但是在安装上fair模块后,运行Tengine总是报错。就算是修改了fair代码,也还是不行,最后不得已又换回了Nginx。

    如果

  • 相关阅读:
    Python字典为什么快?为什么是乱序?
    对Routers的理解
    对drf视图集的理解
    对drf视图的理解
    C++中的随机数函数 分类: C/C++ 2015-07-27 12:24 10人阅读 评论(0) 收藏
    windows bat脚本编写 2015-07-27 11:27 5人阅读 评论(0) 收藏
    windows下bat批处理文件语法 2015-07-27 11:25 8人阅读 评论(0) 收藏
    shell版俄罗斯方块二:界面绘制 分类: shell版俄罗斯方块 2015-07-27 09:14 116人阅读 评论(0) 收藏
    shell版俄罗斯方块一:方向键获取 分类: shell版俄罗斯方块 2015-07-27 09:00 114人阅读 评论(0) 收藏
    echo的高级用法-颜色输出与光标定位 分类: Ubuntu学习笔记 2015-07-26 22:54 17人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/ztlsir/p/8945043.html
Copyright © 2020-2023  润新知