[Kubernetes] Probes

Probes

source : https://medium.com/@extio/deep-dive-into-kubernetes-probes-ensuring-application-health-and-resilience-49a1797a816c

There are three kinds of probes in Kubernetes(k8s): startup, liveness, and readiness.

If the startup probe succeeds, the liveness and readiness probes get started. Containers that takes some time to get started can benefit from startup probe. As long as startup probe is not finished, K8s will never restart the pod. Therefore, the startup probe is useful for applications that need more time for initialization.

Liveness probe checks whether the pod should be restarted. For example, if a container enters an infinit loop, k8s can restart the container by checking the liveness probe.

Readiness probe is used for the containers that take some time to get ready. Since it changes the ready flag of the pod, it can be useful when you want continuous deployment, ensuring that only pods that are ready to server requests are included in the load balancer.

Probe Configuration

To implement these probes, Kubernetes offers three types of configurations: HTTP, TCP Socket, and Exec. Each configuration method allows you to check the status of your containers in different ways:

  • http
  • socket
  • exec

1. http get probe

HTTP probe checks status using GET method. If the designated path returns status within 200~399, probe is considered sucessful.

apiVersion: v1
kind: Pod
metadata:
  name: http-probe-pod
spec:
  containers:
  - name: http-probe-container
    image: your-image
    livenessProbe:
      httpGet:
        path: /healthz
        port: 8080
      initialDelaySeconds: 3
      periodSeconds: 3
    readinessProbe:
      httpGet:
        path: /ready
        port: 8080
      initialDelaySeconds: 5
      periodSeconds: 5

2. tcp socket probe

TCP socket probes checks status by trying tcp connection. If it succeeds, probe is considered successful.

apiVersion: v1
kind: Pod
metadata:
  name: tcp-socket-probe-pod
spec:
  containers:
  - name: tcp-socket-probe-container
    image: your-image
    livenessProbe:
      tcpSocket:
        port: 8080
      initialDelaySeconds: 3
      periodSeconds: 3
    readinessProbe:
      tcpSocket:
        port: 8080
      initialDelaySeconds: 5
      periodSeconds: 5

3. exec probe

Exec probe checks the status by executing commands inside the container. If the commands succeed, probe is considered successful.

apiVersion: v1
kind: Pod
metadata:
  name: exec-probe-pod
spec:
  containers:
  - name: exec-probe-container
    image: your-image
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 3
      periodSeconds: 3
    readinessProbe:
      exec:
        command:
        - cat
        - /tmp/ready
      initialDelaySeconds: 5
      periodSeconds: 5
      
---
apiVersion: v1
kind: Pod
metadata:
  name: exec-probe-pod
spec:
  containers:
  - name: exec-probe-container
    image: your-image
    livenessProbe:
      exec:
        command:
        - test
        - -f
        - /tmp/healthy
      initialDelaySeconds: 3
      periodSeconds: 10
    readinessProbe:
      exec:
        command:
        - test
        - -f
        - /tmp/ready
      initialDelaySeconds: 5
      periodSeconds: 10

InitialDelaySeconds means the probe waits this amount of time before checking the status for the first time.

periodSeconds means the probe checks the status at this interval.

Lifecycle of pods

source: https://andrewlock.net/deploying-asp-net-core-applications-to-kubernetes-part-6-adding-health-checks-with-liveness-readiness-and-startup-probes/?ref=seongjin.me

To handle probes effectively, it is essential to understand the lifecycle of pods in Kubernetes.

As the pod starts, the startup probe checks status. If it succeeds, the readiness and liveness probe checks status. If readiness probe fails, traffic is forbiddened and the endpoint is excluded. If the liveness probe fails, the pod is restarted.

So, by setting readiness probe, you can continuously deploy and updated pods without downtime. you can ensure zero-downtime during updates.

With liveness probe, you can automatically rescue the pod from unexpected code flow.

The startup probe provents the pod from being restarted during lengthy initialization processes, ensuring smooth startup without premature restarts.

Conclusion

In Kubernetes, probes are crucial for monitoring the status of containers within a pod. Probes are essential tools for increasing availability and maintainability. Properly configured probes help infrastructure engineers cope with unexpected situations.

Liveness probe checks whether the container is working properly. If not, it restarts the pod.

Readiness probe checks whether the container is ready to receive requests from outside. If not, the pod is excluded from the service endpoint.

Startup probe is essential for pods that take a long time to initialize. The pod is never restarted until the startup probe returns success.

To practice and understand the usefulness of probes, you must try to apapt probe to your pods.

If you have any questions, just comment below!

Leave a Reply

Your email address will not be published. Required fields are marked *