Detailed explanation of Kubernetes health check and detection mechanism

1. The default health check mechanism in K8s

k8s has a powerful self-healing capability, and the default self-healing implementation is to automatically restart the failed container.

So how does k8s discover container failures? Each container will execute a process when it starts, and this process is specified by the CMD or ENTRYPOINT of the Dockerfile. If the return code is non-zero when the process exits, the container is considered to be faulty, and K8s will restart the container according to the restartPolicy policy.

Let's use an example to demonstrate this situation: first write a file to create a pod, the content is as follows:

[root@master pod]# more healthcheck-pod.yml 
apiVersion: v1
kind: Pod
metadata:
 name: pod-healthcheck
 labels:
   test: healthcheck-test
spec:
  restartPolicy: OnFailure
  containers:
  - name: healthcheck
    image: busybox
    args:
    - /bin/sh
    - -c
    - sleep 10; exit 1   	#sleep 10; exit 1 模拟容器启动 10 秒后发生故障

Next, execute the pod to check the status of the pod:

[root@master pod]# kubectl  apply -f healthcheck-pod.yml 
[root@master pod]# kubectl  get pod
NAME                  READY                STATUS                  RESTARTS         AGE
pod-healthcheck       0/1                 CrashLoopBackOff          5               8m

<

Guess you like

Origin blog.csdn.net/qq_35029061/article/details/132201033