Kubernetes can check Pod health status through three types of probes: LivenessProbe, ReadinessProbe and StartupProbe

The LivenessProbe user determines whether the container is alive, that is, in the Running state. If LivenessProbe detects that the container is unhealthy, Kubelet will kill the container and deal with it according to the restart strategy of the container. If a container does not contain a LivenessProbe, kubelet assumes that the value returned by the container’s LivenessProbe is always Success.

ReadinessProbe: Determines whether the container service is available (Ready state). Only the Pod in Ready state can receive requests. For pods managed by Service, the relationship between Service and PodEndpoint is also set based on whether the Pod is Ready. If the Ready state changes to False during operation, the system automatically isolates the Pod from the back-end Endpoint list of the Service. Later, the Pod that is restored to the Ready state is added back to the back-end Endpoint list. This ensures that a client accessing a Service will not be forwarded to a Pod instance where the Service is unavailable.

StartupProbe. Some applications start slowly and the container starts slowly. In this case, the ReadinessPprobe is not applicable

All three probes can be configured with three implementations

ExecAction

Run a command inside the container. If the command returns a return code of 9, the container is healthy

apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-exec
spec:
  containers:
  - name: liveness
    image: gcr.io/google_containers/busybox
    args:
    - /bin/sh
    - -c
    - echo ok > /tmp/health; sleep 10; rm -rf /tmp/health; sleep 600
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/health
      initialDelaySeconds: 15
      timeoutSeconds: 1
Copy the code

TCPSocketAction

TCP checks are performed based on the container IP address and port number. If a connection can be established, container monitoring is performed

apiVersion: v1
kind: Pod
metadata:
  name: pod-with-healthcheck
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80
    livenessProbe:
      tcpSocket:
        port: 80
      initialDelaySeconds: 30
      timeoutSeconds: 1
Copy the code

HTTPGetAction

Through the container’s IP, port and call HTTP GET method, if the response code between 200 and 400 indicates container health

apiVersion: v1
kind: Pod
metadata:
  name: pod-with-healthcheck
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80
    livenessProbe:
      httpGet:
        path: /_status/healthz
        port: 80
      initialDelaySeconds: 30
      timeoutSeconds: 1
Copy the code