文档

使用ReadinessGate实现ALB Ingress后端Pod滚动升级时平滑上线

更新时间:

在Pod上线阶段,需要确保Pod在接收流量之前真正达到可服务状态,您可以通过配置ReadinessGate来保证Pod上线阶段的可用性。ALB Ingress Controller支持启用ReadinessGate功能,持续监控ACK集群Pod的状态,等待Pod状态变为可用状态后再将完全启动的Pod挂载到后端服务器组,并向该Pod转发流量。

前提条件

原理解析

K8s可以通过三类可用性探针,包括存活(LivenessProbe)、就绪(ReadinessProbe)、启动(StartupProbe),来检查Pod的健康状态、服务可用性和启用条件,提高应用的可用性。您可以通过HTTP请求、TCP Socket连接或命令行方式配置这三类探针,设置执行探测的频率、超时时间、健康阈值和不健康阈值等参数。更多信息,请参见配置存活/就绪和启动探针

探针种类

作用

存活探针(LivenessProbe)

用于判断容器是否存活,如果LivenessProbe探针探测到容器不健康,kubelet将kill掉该容器,并根据容器的重启策略做相应的处理。如果一个容器不包含LivenessProbe探针,那么kubelet认为该容器的LivenessProbe探针返回的值永远是Success,即容器存活。

就绪探针(ReadinessProbe)

用于判断容器服务是否可用,达到Ready状态的Pod才可以接收请求。Service与Endpoint的关联关系也根据Pod的Ready状态设置:

  • 当Pod的Ready状态为False时,K8s将Pod IP从Service关联的后端Endpoint列表中隔离。

  • 等待Pod的Ready状态变更为True时,K8s将Pod IP重新加入到Service关联的Endpoint列表中。

启动探针(StartupProbe)

用于判断容器何时开始启动,可以控制容器在启动成功后再进行存活性和就绪性检查。可以使用该探针对慢启动容器进行存活性检测,避免它们在启动运行之前被kill掉。

可用性探针模式下,Pod的Ready状态仅由kubelet根据容器状态判断。然而,对于复杂应用程序来说,往往需要更精细地控制容器内服务的可用性,需要具备控制Pod的Ready状态的能力。

通过ReadinessGate机制,您可以设置一个或多个自定义的Pod可用性探测来判断Pod是否可用。新增的自定义Condition状态将由外部Controller控制器设置,K8s将在判断全部ReadinessGate条件都为true时,才将Pod设置为服务可用状态。

ALB Ingress Controller支持配置ReadinessGate,使ACK集群持续监控Pod状态,直到状态为可用状态才认为Pod已经启动并挂载至后端服务器组上,并将流量转发至该Pod节点。

说明

在Deployment中配置ReadinessGate自定义Condition:target-health.alb.k8s.alibabacloud后,ACK集群持续监控Pod状态。当ALB Ingress Controller将Pod成功添加到ALB后端服务器组后,集群才标记Pod状态可用并向Pod转发流量。

操作步骤

  1. 创建tea-service.yaml,配置ReadinessGate自定义Condition。

    创建tea-service.yaml文件并拷贝以下内容到文件中,用于部署名称为tea的Deployment以及名称为tea-svc的Service。在Deployment中配置ReadinessGate自定义Condition:target-health.alb.k8s.alibabacloud,使ACK集群持续监控Pod的状态,直到状态为可用状态才认为Pod已经启动并挂载到后端服务器组上。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: tea
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: tea
      template:
        metadata:
          labels:
            app: tea
        spec:
          containers:
          - name: tea
            image: registry.cn-hangzhou.aliyuncs.com/acs-sample/nginxdemos:latest
            ports:
            - containerPort: 80
    # 配置readinessGate
          readinessGates:
            - conditionType: target-health.alb.k8s.alibabacloud
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: tea-svc
    spec:
      ports:
      - port: 80
        targetPort: 80
        protocol: TCP
      selector:
        app: tea
      type: NodePort
  2. 执行以下命令,部署Deployment和Service。

    kubectl apply -f tea-service.yaml
  3. 执行以下命令,检查ReadinessGate配置是否生效。

    kubectl get pods -o yaml |grep 'target-health'

    预期输出:

        -conditionType:target-health.alb.k8s.alibabacloud 
         message:correspondingconditionofpodreadinessgate"target-health.alb.k8s.alibabacLoud" 

相关文档

  • 本页导读 (1)
文档反馈