This chapter describes how to configure health checks for TCP.

Introduction

NGINX and NGINX Plus can continually test your TCP upstream servers, avoid the servers that have failed, and gracefully add the recovered servers into the load-balanced group.

Prerequisites

  • You have configured an upstream group of TCP servers in the stream context, for example:

    stream {
    ...
    upstream stream_backend {
    server backend1.example.com:12345;
    server backend2.example.com:12345;
    server backend3.example.com:12345;
    }
    ...
    }
  • You have configured a server that passes TCP connections to the server group:

    stream {
    ...
    server {
    listen 12345;
    proxy_pass stream_backend;
    }
    ...
    }

Passive TCP Health Checks

If an attempt to connect to an upstream server times out or results in an error, open source NGINX or NGINX Plus can mark the server as unavailable and stop sending requests to it for a defined amount of time. To define the conditions under which NGINX considers an upstream server unavailable, include the following parameters to the server directive

  • fail_timeout – The amount of time within which a specified number of connection attempts must fail for the server to be considered unavailable. Also, the amount of time that NGINX considers the server unavailable after marking it so.
  • max_fails – The number of failed attempts that happen during the specified time for NGINX to consider the server unavailable.
    意思就是:当upstream里的一个server失败次数达到max_fails,该server接下来的fail_timeout时间内停止服务(不会分发连接给它),过了这个时间继续根据负载算法往该server分配连接

The default values are 10 seconds and 1 attempt. So if a connection attempt times out or fails at least once in a 10-second period, NGINX marks the server as unavailable for 10 seconds. The example shows how to set these parameters to 2 failures within 30 seconds:

upstream stream_backend {
server backend1.example.com:12345 weight=5;
server backend2.example.com:12345 max_fails=2 fail_timeout=30s;
server backend3.example.com:12346 max_conns=3;
}

Active TCP Health Checks

Health checks can be configured to test a wide range of failure types. For example, NGINX Plus can continually test upstream servers for responsiveness and avoid servers that have failed.

NGINX Plus sends special health check requests to each upstream server and checks for a response that satisfies certain conditions. If a connection to the server cannot be established, the health check fails, and the server is considered unhealthy. NGINX Plus does not proxy client connections to unhealthy servers. If several health checks are defined for a group of servers, the failure of any one check is enough for the corresponding server be considered unhealthy.

To enable active health checks:

  1. Specify a shared memory zone – a special area where the NGINX Plus worker processes share state information about counters and connections. Add the zone directive to the upstream server group and specify the zone name and the amount of memory:

    stream {
    ...
    upstream stream_backend {
    zone stream_backend 64k;
    server backend1.example.com:12345;
    server backend2.example.com:12345;
    server backend3.example.com:12345;
    }
    ...
    }
  2. Enable health checks for servers in the upstream group. Add the health_check and health_check_timeout directives to the server that proxies connections to the upstream group:

    stream {
    ...
    server {
    listen 12345;
    proxy_pass stream_backend;
    health_check;
    health_check_timeout 5s;
    }
    ...
    }

    The health_check directive enables the health check functionality, while health_check_timeout overrides the proxy_timeout value for health checks, as for health checks this timeout needs to be significantly shorter.

Fine-Tuning TCP Health Checks

By default, NGINX Plus tries to connect to each server in an upstream server group every 5 seconds. If the connection cannot be established, NGINX Plus considers the health check failed, marks the server as unhealthy, and stops forwarding client connections to the server.

To change the default behavior, include parameters to the health_check directive:

  • interval – How often (in seconds) NGINX Plus sends health check requests (default is 5 seconds)
  • passes – Number of consecutive health checks the server must respond to to be considered healthy (default is 1)
  • fails – Number of consecutive health checks the server must fail to respond to to be considered unhealthy (default is 1)
stream {
...
server {
listen 12345;
proxy_pass stream_backend;
health_check interval=10 passes=2 fails=3;
}
...
}

In the example, the time between TCP health checks is increased to 10 seconds, the server is considered unhealthy after 3 consecutive failed health checks, and the server needs to pass 2 consecutive checks to be considered healthy again.

By default, NGINX Plus sends health check messages to the port specified by the server directive in the upstream block. You can specify another port for health checks, which is particularly helpful when monitoring the health of many services on the same host. To override the port, specify the port parameter of the health_check directive:

stream {
...
server {
listen 12345;
proxy_pass stream_backend;
health_check port=8080;
}
...
}

The “match {}” Configuration Block

You can verify server responses to health checks by configuring a number of tests. These tests are defined with the match {} configuration block placed in the stream {}context. Specify the match {} block and set its name, for example, tcp_test:

stream {
...
match tcp_test {
...
}
}

Then refer to the block from the health_check directive by including the match parameter and the name of the match block:

stream {
...
server {
listen 12345;
health_check match=tcp_test;
proxy_pass stream_backend;
}
...
}

The conditions or tests under which a health check succeeds are set with send and expect parameters:

  • send – The text string or hexadecimal literals (“/x” followed by two hex digits) to send to the server
  • expect – Literal string or regular expression that the data returned by the server needs to match

These parameters can be used in different combinations, but no more than one send and one expect parameter can be specified at a time:

  • If no send or expect parameters are specified, the ability to connect to the server is tested.

  • If the expect parameter is specified, the server is expected to unconditionally send data first:

    match pop3 {
    expect ~* "\+OK";
    }
  • If the send parameter is specified, it is expected that the connection will be successfully established and the specified string will be sent to the server:

    match pop_quit {
    send QUIT;
    }
  • If both the send and expect parameters are specified, then the string from the send parameter must match the regular expression from the expect parameter:

    stream {
    ...
    upstream stream_backend {
    zone upstream_backend 64k;
    server backend1.example.com:12345;
    }
    match http {
    send "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n";
    expect ~* "200 OK";
    }
    server {
    listen 12345;
    health_check match=http;
    proxy_pass stream_backend;
    }
    }

    The example shows that in order for a health check to pass, the HTTP request must be sent to the server, and the expected result from the server contains 200 OK to indicate a successful HTTP response.

 

TCP Health Checks的更多相关文章

  1. Kong(V1.0.2) Health Checks and Circuit Breakers Reference

    介绍 您可以让Kong代理的API使用ring-balancer,通过添加包含一个或多个目标实体的 upstream 实体进行配置,每个 target指向不同的IP地址(或主机名)和端口.ring-b ...

  2. UDP Health Checks

    This chapter describes how to configure different types of health checks for UDP servers in a load-b ...

  3. HTTP Health Checks

    This article describes how to configure and use HTTP health checks in NGINX Plus and open source NGI ...

  4. Service Discovery And Health Checks In ASP.NET Core With Consul

    在这篇文章中,我们将快速了解一下服务发现是什么,使用Consul在ASP.NET Core MVC框架中,并结合DnsClient.NET实现基于Dns的客户端服务发现 这篇文章的所有源代码都可以在G ...

  5. Using HAProxy as an API Gateway, Part 3 [Health Checks]

    转自:https://www.haproxy.com/blog/using-haproxy-as-an-api-gateway-part-3-health-checks/ Achieving high ...

  6. Zookeeper Health Checks

    Short Description: The article talks about the basic health checks to be performed when working on i ...

  7. NGINX Load Balancing – TCP and UDP Load Balancer

    This chapter describes how to use NGINX Plus and open source NGINX to proxy and load balance TCP and ...

  8. 11g新特性:Health Monitor Checks

    一.什么是Health Monitor ChecksHealth Monitor Checks能够发现文件损坏,物理.逻辑块损坏,undo.redo损坏,数据字典损坏等等.Health Monitor ...

  9. About Health Monitor Checks

    About Health Monitor Checks Health Monitor checks (also known as checkers, health checks, or checks) ...

随机推荐

  1. 跨域学习笔记2--WebApi 跨域问题解决方案:CORS

    自己并不懂,在此先记录下来,留待以后学习... 正文 前言:上篇总结了下WebApi的接口测试工具的使用,这篇接着来看看WebAPI的另一个常见问题:跨域问题.本篇主要从实例的角度分享下CORS解决跨 ...

  2. sudo: cd: command not found

    事件起因 今天在aws ubutun上忽然发现的一个问题,执行sudo cd 时出现 sudo: cd: command not found 原因 shell shell是一个命令解析器 所谓shel ...

  3. SpringBoot史前简述

    背景 大约20年前,程序员们使用“企业级Java Bean”(EJB)开发企业应用,需要配置复杂的XML. 在二十世纪初期,新兴Java技术——Spring,横空出世.使用极简XML和POJO(普通J ...

  4. Javascript继承5:如虎添翼----寄生式继承

    /* * 寄生式继承 * 其实就是对原型继承的第二次封装,在封装过程中对继承的对象进行了扩展. * 也存在原型继承的缺点!! * 这种思想的作用也是为了寄生组合式继承模式的实现. */ //声明基对象 ...

  5. OOP设计模式在路上(一)——简单工厂模式

    前言 目前以LabVIEW为主要开发工具,熟悉常规开发框架(队列+状态机),个人用得比较多也感觉比较好用和强大的(JKI,AMC),也用它们开发过一些测试平台,但感觉到了一个瓶颈期,想寻求突破,提升L ...

  6. js/es6 元素拖动

    元素事件:鼠标按下事件/鼠标移动事件/鼠标松开事件 元素样式:让元素脱离文档流,采用绝对定位的方式. 一.鼠标按下事件 当鼠标在元素上面按下时,保存元素的初始偏移量和鼠标按下时的坐标,然后在状态变量里 ...

  7. Laravel 系列入门教程(二)【最适合中国人的 Laravel 教程】

    本篇文章中,我将跟大家一起体验 Laravel 框架最重要的部分——路由系统. 如果你读过 2015 版的教程,你会发现那篇文章里大书特书的 Auth 系统构建已经被 Laravel 捎带手给解决了. ...

  8. CNN中,1X1卷积核到底有什么作用呢?

    CNN中,1X1卷积核到底有什么作用呢? https://www.jianshu.com/p/ba51f8c6e348 Question: 从NIN 到Googlenet mrsa net 都是用了这 ...

  9. 24.Odoo产品分析 (三) – 人力资源板块(5) – 出勤(1)

    查看Odoo产品分析系列--目录 安装"出勤"模块,管理员工的上下班打卡. 1. 签到与退签 安装完模块后,点击"出勤"主菜单:  点击中间的签到按钮,实现签到 ...

  10. ViewPager结合Fragment进行无限滑动

    实现ViewPager结合Fragment实现无限循环切换,这里也是在适配器里面进行的,当然使用滑动监听也能够实现 import android.support.v4.app.Fragment; im ...