Relevant Documentation

Container Health

Kubernetes (K8s) provides several features to build robust solutions, including the ability to automatically restart unhealthy containers. To leverage these features effectively, it is essential for K8s to accurately determine the status of your applications. This requires actively monitoring container health.

Liveness Probes

Liveness probes enable automatic detection of whether a container application is in a healthy state or not. By default, K8s considers a container to be "down" only if the container process stops. However, liveness probes allow customization of this detection mechanism, making it more sophisticated.

Startup Probes

Startup probes are similar to liveness probes, but with a different timing. While liveness probes run continuously on a schedule, startup probes only run at container startup and stop once they succeed. These probes are useful for determining when an application has successfully started, particularly for legacy applications that may have long startup times.

Readiness Probes

Readiness probes are used to determine when a container is ready to accept requests. When a service is backed by multiple container endpoints, user traffic is only directed to a particular pod when all its containers pass the readiness checks defined by their readiness probes. Readiness probes help prevent user traffic from being sent to pods that are still in the process of starting up.

Utilizing liveness probes, startup probes, and readiness probes collectively ensures that containerized applications in Kubernetes maintain optimal health and availability.


Lesson Reference

Create a pod with a command-based liveness probe.

  1. Create a YAML file named liveness-pod.yml and open it for editing:
vi liveness-pod.yml
  1. Add the following content to the file:
apiVersion: v1
kind: Pod
metadata:
  name: liveness-pod
spec:
  containers:
  - name: busybox
    image: busybox
    command: ['sh', '-c', 'while true; do sleep 3600; done']
    livenessProbe:
      exec:
        command: ["echo", "Hello, world!"]
      initialDelaySeconds: 5
      periodSeconds: 5