【问题标题】:K8s liveness probe behavior when the pod contains more than one container?pod包含多个容器时的K8s liveness探测行为?
【发布时间】:2021-06-09 21:24:06
【问题描述】:

场景:一个 K8S pod 有多个容器,并且为每个容器配置了 liveness/readiness 探针。现在如果 liveness probe 在某些容器上成功而在少数容器上失败,k8s 会做什么。

  1. 是否只重启失败的容器
    或
  2. 它会重新启动整个 pod。

【问题讨论】:

    标签: kubernetes kubernetes-pod readinessprobe livenessprobe


    【解决方案1】:

    将重启容器。

    就 k8s 文档而言:
    kubelet 使用就绪探针来了解容器何时准备好开始接受流量。当 Pod 的所有容器都准备好时,就认为 Pod 准备好了。

    为了执行探测,kubelet 向在容器中运行并侦听端口 8080 的服务器发送 HTTP GET 请求。如果服务器的 /healthz 路径的处理程序返回成功代码,则 kubelet 认为容器活得健康。如果处理程序返回失败代码,kubelet 会杀死容器并重新启动它。

    当 Pod 正在运行时,kubelet 能够重新启动容器以处理某种故障。在 Pod 中,Kubernetes 会跟踪不同的容器状态,并确定采取什么措施来使 Pod 再次健康。

    您可以查看 pod 事件以了解容器是否重新启动。

    参考:k8s doc 和 probes

    【讨论】:

      【解决方案2】:

      如果活性探测在某些容器上成功而在少数容器上失败,k8s 会做什么?

      它只会重启失败的容器。

      在Pod Lifecycle - Container Probes 中,您列出了所有 3 个探测器:liviness、readiness 和 startup。

      livenessProbe:表示container 是否正在运行。 如果 liveness 探测失败,kubelet 会杀死容器,容器会受到其restart policy 的约束。如果 Container 不提供 liveness probe,则默认状态为 Success。

      在Configure Liveness, Readiness and Startup Probes - Define a liveness command 你有例子并且提到:

      如果命令成功,则返回 0,kubelet 认为容器是存活且健康的。如果命令返回一个非零值,kubelet 杀死容器并重新启动它。

      同样的情况是HTTP request liveness probe:

      如果服务器的 /healthz 路径的处理程序返回成功代码,则 kubelet 认为容器处于活动状态且健康。如果处理程序返回失败代码,kubelet 会杀死容器并重新启动它。

      还有TCP liveness probe:

      kubelet 将在容器启动 15 秒后运行第一个 liveness probe。就像readiness probe一样,它会尝试连接goproxy容器的8080端口。如果liveness probe失败,容器会被重启。

      测试

      如果您想创建自己的测试,您可以使用这个 HTTP Liveness 探测示例:

      apiVersion: v1
      kind: Pod
      metadata:
        labels:
          test: liveness
        name: liveness-http-probe
      spec:
        containers:
        - name: liveness
          image: k8s.gcr.io/liveness
          args:
          - /server
          readinessProbe:
            httpGet:
              path: /healthz
              port: 8080
              httpHeaders:
              - name: X-Custom-Header
                value: Awesome
            initialDelaySeconds: 0
            periodSeconds: 5      
            timeoutSeconds: 5
            successThreshold: 1
            failureThreshold: 3
          livenessProbe:
            httpGet:
              path: /healthz
              port: 8080
              httpHeaders:
              - name: X-Custom-Header
                value: Awesome
            initialDelaySeconds: 5
            periodSeconds: 10     
            successThreshold: 1
            failureThreshold: 3 
        - name: nginx
          image: nginx
      

      一段时间后,您将能够看到container 重新启动,restart count 增加,但pod 仍然存在,因为Age 仍在计数。

      $ kubectl get po -w
      NAME                  READY   STATUS    RESTARTS   AGE
      liveness-http-probe   2/2     Running   0          20s
      liveness-http-probe   1/2     Running   0          23s
      liveness-http-probe   1/2     Running   1          42s
      liveness-http-probe   2/2     Running   1          43s
      liveness-http-probe   1/2     Running   1          63s
      ...
      liveness-http-probe   1/2     Running   5          3m23s
      liveness-http-probe   2/2     Running   5          3m23s
      liveness-http-probe   1/2     Running   5          3m43s
      liveness-http-probe   1/2     CrashLoopBackOff   5          4m1s
      liveness-http-probe   1/2     Running            6          5m25s
      liveness-http-probe   2/2     Running            6          5m28s
      liveness-http-probe   1/2     Running            6          5m48s
      liveness-http-probe   1/2     CrashLoopBackOff   6          6m2s
      liveness-http-probe   1/2     Running            7          8m46s
      liveness-http-probe   2/2     Running            7          8m48s
      ...
      liveness-http-probe   2/2     Running   11         21m
      liveness-http-probe   1/2     Running   11         21m
      liveness-http-probe   1/2     CrashLoopBackOff   11         22m
      liveness-http-probe   1/2     Running            12         27m
      ...
      liveness-http-probe   1/2     Running            13         28m
      liveness-http-probe   1/2     CrashLoopBackOff   13         28m
      

      在 pod 描述中,您会看到重复的 Warnings,例如 (x8 over 28m)、(x84 over 24m) 或 (x2 over 28m)。

        Normal   Pulling    28m (x2 over 28m)     kubelet            Pulling image "k8s.gcr.io/liveness"
        Normal   Killing    28m                   kubelet            Container liveness failed liveness probe, will be restarted
        Normal   Started    28m (x2 over 28m)     kubelet            Started container liveness
        Normal   Created    28m (x2 over 28m)     kubelet            Created container liveness
        Normal   Pulled     28m                   kubelet            Successfully pulled image "k8s.gcr.io/liveness" in 561.418121ms
        Warning  Unhealthy  27m (x8 over 28m)     kubelet            Readiness probe failed: HTTP probe failed with statuscode: 500
        Warning  Unhealthy  27m (x4 over 28m)     kubelet            Liveness probe failed: HTTP probe failed with statuscode: 500
        Normal   Pulled     13m (x2 over 14m)     kubelet            (combined from similar events): Successfully pulled image "k8s.gcr.io/liveness" in 508.892628ms
        Warning  BackOff    3m45s (x84 over 24m)  kubelet            Back-off restarting failed container
      

      最近我在线程中使用 liveness 和 readiness 探针进行了一些测试 - Liveness Probe, Readiness Probe not called in expected duration。它可以为您提供更多信息。

      【讨论】:

      • 似乎我错过了看这个......很棒的挖掘+1
      猜你喜欢
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-19
      • 2018-03-01
      • 2021-09-05
      • 1970-01-01
      • 2023-02-11
      相关资源
      最近更新 更多