http主动健康检查

1. 功能说明

主动健康检查通过API接口进行动态配置,实现健康检查由独立进程完成,对正在使用的进程无影响,完成后修改共享内存完成upstream状态回写。

2. 依赖模块

健康检查功能依赖模块:

load_module modules/njt_http_sendmsg_module.so;
load_module modules/njt_helper_health_check_module.so;

3. 指令说明

3.1 数据面指令说明

health_check

语法: health_check mandatory | persistent;

默认值: —

允许配置位置: upstream

配置项 必填 配置说明
health_check 主动发起请求测试上游服务是否正常,如不正常则及时剔除对应服务
mandatory 配置后,静态文件server,api,域名解析出的server 初始状态都是checking 状态(不做业务,等待健康检查),然后会使用默认主动健康检查配置立即进行健康检查,然后更新结果为 health 或 unhealth
persistent 配置之后reload 后,会保留上次的健康状态。 直到下次健康检查。

3.2 配置示例

    upstream backend1 {
    
         zone backend1 128k;
         
         server 192.168.40.144:5555;

         health_check mandatory persistent;

    }

3.3 API说明

查询全部健康检查列表:

GET http://ip+port/api/v1/hc/http/

查询具体upstream名为backend的健康检查配置:

GET http://ip+port/api/v1/hc/http/backend

向具体upstream名为backend添加健康检查:

POST http://ip+port/api/v1/hc/http/backend

删除具体upstream名为backend的健康检查:

DELETE http://ip+port/api/v1/hc/http/backend

格式说明健康检查配置项参数说明

{
    "interval": "3s",   #必填
    "visit_interval": "2s",
    "jitter": "1s",    #必填
    "timeout": "10s",  #必填
    "passes": 2,       #必填
    "fails": 1,        #必填
    "port": 13470
}
配置项 必填 配置说明
interval 主动健康检查频率 (必填)
visit_interval 健康检查时,如果指定的时间间隔内该server被客户端访问过,则该server跳过此次健康检查,interval > visit_interval
jitter 设置健康检查项定时器最大偏差。防止所有检查项同时触发。 (必填)
timeout 超时时间 (必填)
passes server_body server 块内的指令集,每条指令用分号分隔。server_body内容可以为空。
fails 连续不通过fails次检测,更新peer为unhealthy状态   (必填)
port 指定健康检查的端口,如果不指定,使用upstream中设置的端口

3.4 HTTP健康检查

{
    "interval": "3s",
    "jitter": "1s",
    "timeout": "10s",
    "passes": 2,
    "fails": 1,
    "port": 13470
    "http": {
        "header":["host"],
        "uri": "/body",
        "body": "~ ok",
        "status": "200 204"
  }
}

部分api字段补充

健康检查 HTTP > status 配置方式

配置内容 判定为健康的描述
200 http返回状态码为200
! 500 http返回状态码不是500.!与500之间需要有空格符
200 204 http返回状态码是200 或者 204
! 301 302 http返回状态码不是301且不是302。!与status之间需要有空格符
200-399 http返回状态码是200到399的,包含200和399
!400-599 http返回状态码不是400到599的,包含400和599 !与status之间需要有空格符
301-303 307 http返回状态码是301到303的,包含301和303,或者307
配置内容判定为健康的描述
200http返回状态码为200
! 500http返回状态码不是500
200 204http返回状态码是200 或者 204
! 301 302http返回状态码不是301且不是302
 200-399http返回状态码是200到399的,包含200和399
 !400-599http返回状态码不是400到599的,包含400和599
301-303 307http返回状态码是301到303的,包含301和303,或者307

健康检查 http > header 配置方式

配置内容 判定为健康的描述
Content-Type = text/html 返回header中存在"Content-Type" header,且header内容为"text/html"
Content-Type != text/html 返回header中存在"Content-Type" header,且值为除“text/html"外任意内容。
Connection ~ close 返回header中存在 Connection,且值可以匹配表达式"~ close"
Connection !~ close 返回 header中存在 Connection,且值可以不匹配表达式"~ close“
Host 返回 header中存在 Host
!X-Accel-Redirect 返回 header中不存在 X-Accel-Redirect

健康检查 http > body配置方式

配置内容 判定为健康的描述
~ Welcome 返回body内容匹配表达式”~ Welcome“
!~ Welcome 返回body内容不匹配表达式”!~ Welcome“

4. 调用样例

4.1 增加新的http健康检查

请求

POST http://127.0.0.1:8081/api/v1/hc/http/demo
Content-Type: application/json

 {
  "interval": "3s",
  "jitter": "1s",
  "timeout": "10s",
  "passes": 2,
  "fails": 1,
  "http": {
    "header": [
      "Host"
    ],
    "uri": "/test",
    "body": "~ ok",
    "status": "200 204"
  },
  "ssl": {
    "enable": false
  }
 }

返回值

HTTP/1.1 200 OK
Server: njet/1.23.1
Date: Fri, 10 Feb 2023 13:06:24 GMT
Content-Type: application/json
Content-Length: 37
Connection: keep-alive

{
  "code": 0,
  "msg": "success"
}

4.2 删除健康检查

请求

DELETE http://127.0.0.1:8081/api/v1/hc/http/demo

返回值

HTTP/1.1 200 OK
Server: njet/1.23.1
Date: Fri, 10 Feb 2023 13:06:21 GMT
Content-Type: application/json
Content-Length: 37
Connection: keep-alive

{
  "code": 0,
  "msg": "success"
}

4.3 查看健康检查列表

请求

GET http://127.0.0.1:8081/api/v1/hc

返回值

HTTP/1.1 200 OK
Server: njet/1.23.1
Date: Fri, 10 Feb 2023 13:07:12 GMT
Content-Type: application/json
Content-Length: 44
Connection: keep-alive

[
  {
    "upstream": "demo",
    "type": "http"
  }
]

4.4 查看健康检查配置详情

请求

GET http://127.0.0.1:8081/api/v1/hc/http/demo

返回值

HTTP/1.1 200 OK
Server: njet/1.23.1
Date: Fri, 10 Feb 2023 13:07:14 GMT
Content-Type: application/json
Content-Length: 157
Connection: keep-alive

{
  "interval": "3s",
  "visit_interval": "2s",
  "jitter": "1s",
  "timeout": "10s",
  "passes": 2,
  "fails": 1,
  "http": {
    "uri": "/test",
    "status": "200",
    "body": "~ ok",
    "header": [
      "Host"
    ]
  }
}

4.5 http健康检查标密SSL配置

请求

POST http://127.0.0.1:8081/api/v1/hc/http/demos
Content-Type: application/json

{
  "interval": "3s",
  "jitter": "1s",
  "timeout": "10s",
  "passes": 2,
  "fails": 1,
  "http": {
    "uri": "/ssl",
    "status": "200"
  },
  "ssl": {
    "enable": true,
    "ntls": false 
}

返回

HTTP/1.1 200 OK
Server: njet/1.23.1
Date: Mon, 13 Feb 2023 07:35:27 GMT
Content-Type: application/json
Content-Length: 37
Connection: keep-alive

{
  "code": 0,
  "msg": "success"
}