Nginx1.11.10使用upstream_check_module模块实现后端节点健康检查功能

目录

1. 安装nginx的依赖包(pcre和zlib)

1.1安装pcre

1.2安装zlib

2. 安装nginx

2.1上传安装包(可以使用winscp、xftp等工具)

2.2 相关安装命令集如下:

3. 为nginx打补丁包

3.1 安装部署的相关命令集如下

3.2 修改配置文件(/usr/local/nginx/conf/nginx.conf),让nginx_upstream_check_module模块生效

4. 上传中间件进行模拟(我这里是使用Tomcat)

4.1 修改两个Tomcat的端口信息,一个8080,一个8081

4.2 启动两个Tomcat

5. 启动nginx

6. nginx相关命令

7. 效果如下图

8. 相关安装包下载地址


1. 安装nginx的依赖包(pcre和zlib)

1.1安装pcre

cd pcre-8.40
./configure
make
make install

1.2安装zlib

tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install

2. 安装nginx

2.1上传安装包(可以使用winscp、xftp等工具)

2.2 相关安装命令集如下:

tar -zxvf nginx-1.11.10.tar.gz #解压安装介质
cd nginx-1.11.10 #进入到安装目录
./configure --prefix=/usr/local/nginx #安装路径配置
make && make install #安装开始

3. 为nginx打补丁包

3.1 安装部署的相关命令集如下

#下载nginx_upstream_check_module模块

#进入nginx安装目录
cd /usr/local/nginx

#下载nginx_upstream_check_module模块
wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master

#解压
unzip master

#进入nginx的源码目录
cd /gdsfApps/tools/nginx-1.11.10

# -p0,是“当前路径” -p1,是“上一级路径”
patch -p0 < /usr/local/nginx/nginx_upstream_check_module-master/check_1.11.5+.patch

#安装配置
./configure --prefix=/usr/local/nginx --add-module=/usr/local/nginx/nginx_upstream_check_module-master/

#开始安装
make && make install

3.2 修改配置文件(/usr/local/nginx/conf/nginx.conf),让nginx_upstream_check_module模块生效

    upstream zp_name {
        server ip:8080;
        server ip:8081;
        check interval=3000 rise=2 fall=5 timeout=1000 type=http;
    }

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://zp_name;
        }

        location /nstatus{
            check_status;
            access_log off;
        }

添加如上脚本内容。效果如下图所示:

红色框所示的部分即为新增的配置信息

4. 上传中间件进行模拟(我这里是使用Tomcat)

4.1 修改两个Tomcat的端口信息,一个8080,一个8081

4.2 启动两个Tomcat

5. 启动nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

6. nginx相关命令

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf #启动nginx
/usr/local/nginx/sbin/nginx -t #检查配置是否正确
/usr/local/nginx/sbin/nginx -s reload #重新加载配置文件

7. 效果如下图

这时我们关闭其中一个Tomcat再看看效果如下:

8. 相关安装包下载地址

https://download.youkuaiyun.com/download/u011192409/10891359

Lua-Resty-Checkups是一个基于lua的upstream管理和健康检查模块,由又拍云开源。特点:支持周期性upstream服务管理操作支持管理和健康检查支持upstream动态更新有利于加权轮询或哈希平衡支持 Nginx C upstream同步操作可使用级别和键值实现集群使用简介:-- config.lua _M = {} _M.global = {     checkup_timer_interval = 15,     checkup_shd_sync_enable = true,     shd_config_timer_interval = 1, } _M.ups1 = {     cluster = {         {             servers = {                 {host="127.0.0.1", port=4444, weight=10, max_fails=3, fail_timeout=10},             }         },     }, }lua_package_path "/path/to/lua-resty-checkups/lib/checkups/?.lua;/path/to/config.lua;;"; lua_shared_dict state 10m; lua_shared_dict mutex 1m; lua_shared_dict locks 1m; lua_shared_dict config 10m; server {     listen 12350;     return 200 12350; } server {     listen 12351;     return 200 12351; } init_worker_by_lua_block {     local config = require "config"     local checkups = require "resty.checkups.api"     checkups.prepare_checker(config)     checkups.create_checker() } server {     location = /12350 {         proxy_pass http://127.0.0.1:12350/;     }     location = /12351 {         proxy_pass http://127.0.0.1:12351/;     }     location = /t {         content_by_lua_block {             local checkups = require "resty.checkups.api"             local callback = function(host, port)                 local res = ngx.location.capture("/" .. port)                 ngx.say(res.body)                 return 1             end             local ok, err             -- connect to a dead server, no upstream available             ok, err = checkups.ready_ok("ups1", callback)             if err then ngx.say(err) end             -- add server to ups1             ok, err = checkups.update_upstream("ups1", {                     {                         servers = {                             {host="127.0.0.1", port=12350, weight=10, max_fails=3, fail_timeout=10},                         }                     },                 })             if err then ngx.say(err) end             ngx.sleep(1)             ok, err = checkups.ready_ok("ups1", callback)             if err then ngx.say(err) end             ok, err = checkups.ready_ok("ups1", callback)             if err then ngx.say(err) end             -- add server to new upstream             ok, err = checkups.update_upstream("ups2", {                     {                         servers = {                             {host="127.0.0.1", port=12351},                         }                     },                 })             if err then ngx.say(err) end             ngx.sleep(1)             ok, err = checkups.ready_ok("ups2", callback)             if err then ngx.say(err) end             -- add server to ups2, reset rr state             ok, err = checkups.update_upstream("ups2", {                     {                         servers = {                             {host="127.0.0.1", port=12350, weight=10, max_fails=3, fail_timeout=10},                             {host="127.0.0.1", port=12351, weight=10, max_fails=3, fail_timeout=10},                         }                     },                 })             if err then ngx.say(err) end             ngx.sleep(1)             ok, err = checkups.ready_ok("ups2", callback)             if err then ngx.say(err) end             ok, err = checkups.ready_ok("ups2", callback)             if err then ngx.say(err) end     } }Lua 配置示例:_M = {} -- Here is the global part _M.global = {     checkup_timer_interval = 15,     checkup_timer_overtime = 60,     default_heartbeat_enable = true,     checkup_shd_sync_enable = true,     shd_config_timer_interval = 1, } -- The rests parts are cluster configurations _M.redis = {     enable = true,     typ = "redis",     timeout = 2,     read_timeout = 15,     send_timeout = 15,     protected = true,     cluster = {         {   -- level 1             try = 2,             servers = {                 { host = "192.168.0.1", port = 6379, weight=10, max_fails=3, fail_timeout=10 },                 { host = "192.168.0.2", port = 6379, weight=10, max_fails=3, fail_timeout=10 },             }         },         {   -- level 2             servers = {                 { host = "192.168.0.3", port = 6379, weight=10, max_fails=3, fail_timeout=10 },             }         },     }, } _M.api = {     enable = false,     typ = "http",     http_opts = {         query = "GET /status HTTP/1.1\r\nHost: localhost\r\n\r\n",         statuses = {             [500] = false,             [502] = false,             [503] = false,             [504] = false,         },     },     mode = "hash",     cluster = {         dc1 = {             servers = {                 { host = "192.168.1.1", port = 1234, weight=10, max_fails=3, fail_timeout=10 },             }         },         dc2 = {             servers = {                 { host = "192.168.1.2", port = 1234, weight=10, max_fails=3, fail_timeout=10 },             }         }     } } _M.ups_from_nginx = {     timeout = 2,     cluster = {         {   -- level 1             upstream = "api.com",         },         {   -- level 2             upstream = "api.com",             upstream_only_backup = true,         },     }, } return _M
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JAIR_FOREVER

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值