zoukankan      html  css  js  c++  java
  • Nginx负载均衡+监控状态检测

    Nginx负载均衡+监控状态检测

    想用Nginx或者Tengine替代LVS,即能做七层的负载均衡,又能做监控状态检测,一旦发现后面的realserver挂了就自动剔除,恢复后自动加入服务池里,可以用Tengine的ngx_http_upstream_check_module模块。该模块在Tengine-1.4.0版本以前没有默认开启,它可以在配置编译选项的时候开启:./configure --with-http_upstream_check_module。

    Nginx.conf 配置

    1.  
      http {
    2.  
      upstream fire_server{
    3.  
      ip_hash;
    4.  
      server 192.168.1.1:80;
    5.  
      server 192.168.1.2:80;
    6.  
       
    7.  
      check interval=3000 rise=2 fall=5 timeout=1000 type=http ;
    8.  
      check_http_send "GET /status.html HTTP/1.1 Host: 127.0.0.1 ";
    9.  
      check_http_expect_alive http_2xx http_3xx ;
    10.  
      }
    11.  
       
    12.  
      server {
    13.  
      listen 80;
    14.  
      server_name localhost default;
    15.  
       
    16.  
      location / {
    17.  
      proxy_pass http://fire_server;
    18.  
      access_log logs/fire_server_access.log main;
    19.  
      error_log logs/error.log debug;
    20.  
      }
    21.  
       
    22.  
      error_page 500 502 503 504 /50x.html;
    23.  
      location = /50x.html {
    24.  
      root html;
    25.  
      }
    26.  
      }
    27.  
      }
    • check interval 指令可以打开后端服务器的健康检查功能。
    1.  
      指令后面的参数意义是:
    2.  
       
    3.  
      interval:向后端发送的健康检查包的间隔。
    4.  
      fall(fall_count): 如果连续失败次数达到fall_count,服务器就被认为是down。
    5.  
      rise(rise_count): 如果连续成功次数达到rise_count,服务器就被认为是up。
    6.  
      timeout: 后端健康请求的超时时间。
    7.  
      default_down: 设定初始时服务器的状态,如果是true,就说明默认是down的,如果是false,就是up的。默认值是true,也就是一开始服务器认为是不可用,要等健康检查包达到一定成功次数以后才会被认为是健康的。
    8.  
      type:健康检查包的类型,现在支持以下多种类型
    9.  
      tcp:简单的tcp连接,如果连接成功,就说明后端正常。
    10.  
      ssl_hello:发送一个初始的SSL hello包并接受服务器的SSL hello包。
    11.  
      http:发送HTTP请求,通过后端的回复包的状态来判断后端是否存活。
    12.  
      mysql: 向mysql服务器连接,通过接收服务器的greeting包来判断后端是否存活。
    13.  
      ajp:向后端发送AJP协议的Cping包,通过接收Cpong包来判断后端是否存活。
    14.  
      port: 指定后端服务器的检查端口。
    • check_http_send 指令
    该指令可以让负载均衡器模拟向后端realserver发送,监控检测的http包,模拟LVS的检测。
    
    • check_http_expect_alive 指令
    1.  
      check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]
    2.  
      返回指定HTTP code,符合预期就算检测成功
    3.  
       

    RealServer配置

    1.  
      location = /status.html {
    2.  
      root html;
    3.  
      access_log logs/access.log main;
    4.  
      }

    后端realserver配置,只需要保证 curl http://realserver_ip/status.html 能访问到即可。

    测试

    • 移除realserver的status.html即可模拟服务不可用,负载均衡器会在N次检测后发现realserver不服务,error_log里会打印。移回status.html即立马恢复服务。
    1.  
      2015/04/04 22:00:42 [error] 13051#0: check protocol http error with peer: 192.168.1.1:80
    2.  
      2015/04/04 22:00:43 [error] 13051#0: check protocol http error with peer: 192.168.1.1:80
    3.  
      2015/04/04 22:00:44 [error] 13051#0: check protocol http error with peer: 192.168.1.1:80
    4.  
      ...
    5.  
      enable check peer: 192.168.1.1:80

    参考: http://tengine.taobao.org/document_cn/http_upstream_check_cn.html

    转载于:https://www.cnblogs.com/DjangoBlog/p/7133174.html

  • 相关阅读:
    标准maven配置setting文件
    zxing生成和解析二维码工具类
    Postman上传文件
    sql server工具类
    springboot开启token校验一直报错No 'Access-Control-Allow-Origin' header is present on the requested resource
    Jpa分页查询
    Restful接口调用统一异常处理
    npm install --save 、--save-dev 、-D、-S 的区别与NODE_ENV的配置(转载)
    element-ui el-table 组件实现跨表格多选
    微信小程序设置页面背景色的方式(全局或单页面)
  • 原文地址:https://www.cnblogs.com/telwanggs/p/15152514.html
Copyright © 2011-2022 走看看