gpt4 book ai didi

kubernetes - 当 Pod 包含多个容器时,K8s 的 liveness 探测行为?

转载 作者:行者123 更新时间:2023-12-05 01:31:02 24 4
gpt4 key购买 nike

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

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

最佳答案

if the liveness probe is succeeding on some containers and failing on few containers, what will k8s do?

它只会重启失败的容器。

Pod Lifecycle - Container Probes您已经列出了所有 3 个探测器:livinessreadinessstartup

livenessProbe: Indicates whether the container is running. If the liveness probe fails, the kubelet kills the container, and the container is subjected to its restart policy. If a Container does not provide a liveness probe, the default state is Success.

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

If the command succeeds, it returns 0, and the kubelet considers the container to be alive and healthy. If the command returns a non-zero value, the kubelet kills the container and restarts it.

同样的情况是HTTP request liveness probe :

If the handler for the server's /healthz path returns a success code, the kubelet considers the container to be alive and healthy. If the handler returns a failure code, the kubelet kills the container and restarts it.

还有TCP liveness probe :

The kubelet will run the first liveness probe 15 seconds after the container starts. Just like the readiness probe, this will attempt to connect to the goproxy container on port 8080. If the liveness probe fails, the container will be restarted.

测试

如果您想创建自己的测试,您可以使用这个 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 描述中,您将看到重复的 警告,例如 (x8 over 28m)(x84 over 24m) (x2 超过 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

最近我在线程中用 livenessreadiness 探测器做了一些测试 - Liveness Probe, Readiness Probe not called in expected duration .它可以为您提供额外的信息。

关于kubernetes - 当 Pod 包含多个容器时,K8s 的 liveness 探测行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66590086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com