配置 Endpoint Health Checker

概述

Endpoint Health Checker 是一个集群插件,用于监控和管理 k8s 集群中 service endpoint 的健康状态。它会自动从 service 中移除不健康的 endpoint,确保流量仅路由到健康的实例,从而提升整体服务的可靠性和可用性。

主要功能

  • 自动健康监控:持续监控 k8s 集群中 service endpoint 的健康状态
  • 负载均衡集成:自动从 service 中移除不健康的 endpoint
  • 服务可用性:确保流量仅定向到健康、可用的 endpoint
  • 快速故障切换:将节点断电期间的 endpoint 切换时间从 40 秒缩短到 10 秒

安装

通过 Marketplace 安装

  1. 导航到 管理员 > Marketplace > 集群插件

  2. 在插件列表中搜索 "Alauda Container Platform Endpoint Health Checker"。

  3. 单击 安装,打开安装配置页面。

  4. 在部署配置对话框中,你可以选择性地配置以下参数:

    参数描述
    节点选择器配置标签选择器,以指定 Endpoint Health Checker 组件应运行在哪些节点上。单击 添加 可添加多个标签键值对。
    节点容忍度配置容忍度,以允许将 Endpoint Health Checker 组件调度到具有特定污点的节点上。单击 添加 可添加多个包含 Key、Value 和 Type 的容忍度。
  5. 单击 安装 部署插件。

  6. 等待插件状态变为 "Ready"。

工作原理

健康检查机制

Endpoint Health Checker 是一个专用的健康监控组件,可确保只有健康的 endpoint 才能接收流量。它通过监控 service endpoint,并自动管理其可用性状态来运行。

核心功能

Endpoint Health Checker 的工作方式如下:

  1. 服务发现:识别集群中配置了健康监控的服务和 pod。
  2. Pod 健康监控:监控为 service endpoint 提供后端支持的 pod 的 readiness 和 liveness 探针状态
  3. 主动健康检查:使用可配置的标准执行主动健康评估:
    • TCP 连通性检查:建立 TCP 连接以验证端口可访问性
  4. Endpoint 管理:自动从 service endpoint 列表中移除不健康的 endpoint,以防止流量路由到失效实例

健康检查流程

健康检查流程包括:

  • 探针集成:利用 Kubernetes readiness 和 liveness 探针结果作为初始健康指标
  • 网络连通性:向目标 endpoint 端口发送 TCP 数据包以验证可访问性
  • 响应校验:评估响应状态、时延和内容,以确定 endpoint 的健康状况
  • 自动故障切换:从 service endpoint 列表中移除无响应或失败的 endpoint

性能提升

  • 旧方式:依赖 kubelet 心跳检测,最多有 40 秒延迟
  • 当前方式:主动 endpoint 健康检查,检测和切换时间为 10 秒
  • 提升:显著提高了 ALB + MetalLB 环境中节点故障期间的服务可用性

如何启用

健康检查可通过两种方式启用:

Pod 级注解(推荐)

对于 ALB

ALB2 设置 alb.cpaas.io/pod-annotations 注解

apiVersion: crd.alauda.io/v2
kind: ALB2
metadata:
  annotations:
    alb.cpaas.io/pod-annotations: '{"endpoint-health-checker.io/enabled":"true"}'
  name: demo-alb
spec:
  config:
    loadbalancerName: demo-alb
    nodeSelector:
      ingress: 'true'
    replicas: 1
  type: nginx

对于 IngressNginx

  1. 安装 ingress-nginx
  2. IngressNginx.spec.controller.podAnnotations 中设置 podAnnotations
    apiVersion: ingress-nginx.alauda.io/v1
    kind: IngressNginx
    metadata:
      name: demo
      namespace: ingress-nginx-operator
    spec:
      controller:
        replicaCount: 1
        podAnnotations:
          endpoint-health-checker.io/enabled: 'true'

对于 EnvoyGateway

按照 如何在 Envoy Gateway 中添加 Pod 注解 中的说明进行操作,将注解设置为 endpoint-health-checker.io/enabled: 'true'

对于自定义部署

Deployment.spec.template.metadata.annotations 中设置 annotations

apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo
  template:
    metadata:
      labels:
        app: demo
      annotations:
        endpoint-health-checker.io/enabled: 'true'
    spec:
      containers:
        - name: container
          ports:
            - containerPort: 8080
          livenessProbe:
            tcpSocket:
              port: 8080
            initialDelaySeconds: 15
            periodSeconds: 10
          readinessProbe:
            tcpSocket:
              port: 8080
            initialDelaySeconds: 5
            periodSeconds: 5

Pod 级 readinessGates(旧版)

为旧版本在 pod spec 中配置 readinessGates:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pod-legacy
  namespace: cpaas-system
spec:
  replicas: 3
  selector:
    matchLabels:
      app: pod-legacy
  template:
    metadata:
      labels:
        app: pod-legacy
    spec:
      readinessGates:
        - conditionType: 'endpointHealthCheckSuccess'
      containers:
        - name: container
          image: your-image:latest
          ports:
            - containerPort: 8080
          livenessProbe:
            tcpSocket:
              port: 8080
            initialDelaySeconds: 15
            periodSeconds: 10
          readinessProbe:
            tcpSocket:
              port: 8080
            initialDelaySeconds: 5
            periodSeconds: 5

注意:readinessGates 配置来自旧版本。对于新的部署,建议使用 pod 注解 endpoint-health-checker.io/enabled: 'true'

卸载

要卸载 Endpoint Health Checker:

  1. 导航到 管理员 > Marketplace > 集群插件

  2. 找到已安装的 "Endpoint Health Checker" 插件。

  3. 单击选项菜单并选择 卸载

  4. 在提示时确认卸载。