当 kubernetes readiness-probe 返回 false 时会发生什么?要等多久?

J.J*_*eam 0 kubernetes kubernetes-pod readinessprobe

当 Kubernetes 返回 false 时会发生什么readiness-probe?超时后 Kubernetes 是否会重新启动该 pod?Kubernetes 等待准备就绪需要多长时间?

jab*_*son 5

就绪探针不会重新启动 Pod/容器,就绪探针确定容器已准备好为流量提供服务。如果容器被探测并被认为未“准备好”,则容器将从端点中删除,并且流量不会发送到它,直到它再次准备好为止。

[1] https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/#define-readiness-probes

[2] https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#container-probes

[3]kubectl explain pod.spec.containers.readinessProbe

KIND:     Pod
VERSION:  v1

RESOURCE: readinessProbe <Object>

DESCRIPTION:
     Periodic probe of container service readiness. Container will be removed
     from service endpoints if the probe fails. Cannot be updated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

     Probe describes a health check to be performed against a container to
     determine whether it is alive or ready to receive traffic.

FIELDS:
   exec <Object>
     One and only one of the following should be specified. Exec specifies the
     action to take.

   failureThreshold <integer>
     Minimum consecutive failures for the probe to be considered failed after
     having succeeded. Defaults to 3. Minimum value is 1.

   httpGet  <Object>
     HTTPGet specifies the http request to perform.

   initialDelaySeconds  <integer>
     Number of seconds after the container has started before liveness probes
     are initiated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

   periodSeconds    <integer>
     How often (in seconds) to perform the probe. Default to 10 seconds. Minimum
     value is 1.

   successThreshold <integer>
     Minimum consecutive successes for the probe to be considered successful
     after having failed. Defaults to 1. Must be 1 for liveness and startup.
     Minimum value is 1.

   tcpSocket    <Object>
     TCPSocket specifies an action involving a TCP port. TCP hooks not yet
     supported

   timeoutSeconds   <integer>
     Number of seconds after which the probe times out. Defaults to 1 second.
     Minimum value is 1. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
Run Code Online (Sandbox Code Playgroud)

让我们使用文档中的默认就绪探针:

cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - containerPort: 80
    readinessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 5
Run Code Online (Sandbox Code Playgroud)

为了执行探测,kubelet 在目标容器中执行命令 cat /tmp/healthy。如果命令成功,则返回 0,则容器已准备好并可以“提供服务”。如果该命令返回除 0 以外的任何内容,则容器不健康。

由于这个文件从一开始就不存在于容器中,当 pod 启动时,它会变得非常不健康。

date && k get pods nginx
Thu  2 Dec 2021 19:08:43 AST
NAME    READY   STATUS    RESTARTS   AGE
nginx   0/1     Running   0          66s
Run Code Online (Sandbox Code Playgroud)

现在,让我们执行它并创建文件,以便命令成功。

k exec -it nginx -- bash
root@nginx:/# touch /tmp/healthy
root@nginx:/# exit
exit
Run Code Online (Sandbox Code Playgroud)

再次检查:

date && k get pods nginx
Thu  2 Dec 2021 19:09:26 AST
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          110s
Run Code Online (Sandbox Code Playgroud)

再次删除:

k exec -it nginx -- bash
root@nginx:/# rm /tmp/healthy
root@nginx:/# exit
exit
Run Code Online (Sandbox Code Playgroud)

检查:

date && k get pods nginx
Thu  2 Dec 2021 19:09:53 AST
NAME    READY   STATUS    RESTARTS   AGE
nginx   0/1     Running   0          2m17s
Run Code Online (Sandbox Code Playgroud)