Lesson 2.6: Health Probes


Configure Liveness, Readiness and Startup Probes

In Kubernetes, probes are used to monitor the health and availability of containers in a Pod. There are three types of probes:

  1. Liveness Probe
  2. Readiness Probe
  3. Startup Probe

Each probe serves a specific purpose and helps Kubernetes manage the lifecycle of containers effectively. Let’s break down each probe and explain how they work, using the examples you provided.

1. Liveness Probe

The liveness probe determines whether a container is still running and healthy. If the probe fails, Kubernetes restarts the container.

Example: Liveness Probe with exec

[root@master health]# cat liveness-c.yml
apiVersion: v1
kind: Pod
metadata:
  name: liveness-exec
  labels:
    test: liveness
spec:
  containers:
  - name: liveness
    image: busybox:1.28
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 600;
    livenessProbe:
      exec: 
        command: 
        - cat 
        - /tmp/healthy 
      initialDelaySeconds: 5
      periodSeconds: 5
 
[root@master health]# kubectl apply -f liveness-c.yml
pod/liveness-exec created
 
[root@master health]# kubectl get pods
NAME                         READY   STATUS        RESTARTS   AGE
liveness-exec                1/1     Running       0          60s
 
[root@master health]# kubectl get pods
NAME            READY   STATUS    RESTARTS     AGE
liveness-exec   1/1     Running   1 (8s ago)   83s
 
[root@master health]# kubectl get pods
NAME            READY   STATUS    RESTARTS     AGE
liveness-exec   1/1     Running   2 (9s ago)   2m39s
 
[root@master health]# kubectl get pods 
NAME            READY   STATUS    RESTARTS      AGE
liveness-exec   1/1     Running   5 (66s ago)   7m21s

Explanation:

  • The container creates a file /tmp/healthy and deletes it after 30 seconds.
  • The livenessProbe checks for the existence of /tmp/healthy every 5 seconds (periodSeconds: 5), starting after an initial delay of 5 seconds (initialDelaySeconds: 5).
  • When the file is deleted, the probe fails, and Kubernetes restarts the container.
  • Behaviour: The container is restarted multiple times, as seen in the RESTARTS count.

Readiness Probe

The readiness probe determines whether a container is ready to serve traffic. If the probe fails, the container is removed from the Service's load balancer.

Example: Readiness Probe with tcpSocket

[root@master health]# cat liveness-tcp.yml 
apiVersion: v1
kind: Pod
metadata:
  name: goproxy
  labels:
    app: goproxy
spec:
  containers:
  - name: goproxy
    image: registry.k8s.io/goproxy:0.1
    ports:
    - containerPort: 8080
    readinessProbe:
      tcpSocket:
        port: 8080
      initialDelaySeconds: 15
      periodSeconds: 10

Explanation:

  • The readinessProbe checks if the container is listening on port 8080.
  • The probe starts after 15 seconds (initialDelaySeconds: 15) and repeats every 10 seconds (periodSeconds: 10).
  • If the container is not ready to accept connections, it is marked as NotReady and removed from the Service's endpoints.

Startup Probe

The startup probe is used to determine whether the application inside the container has started successfully. It is particularly useful for slow-starting applications.

[root@master health]# cat liveness-http.yml 
apiVersion: v1
kind: Pod
metadata:
  name: hello 
spec:
  containers:
  - name: liveness
    image: registry.k8s.io/e2e-test-images/agnhost:2.40
    args:
    - liveness
    livenessProbe:
      httpGet:
        path: /healthz
        port: 8080 
      initialDelaySeconds: 3
      periodSeconds: 3 
[root@master health]# kubectl apply -f liveness-http.yml 
pod/hello created
 
[root@master health]# kubectl get pods 
NAME    READY   STATUS    RESTARTS   AGE
hello   1/1     Running   0          27s
 
[root@master health]# kubectl get pods 
NAME    READY   STATUS    RESTARTS      AGE
hello   1/1     Running   2 (14s ago)   62s
 
[root@master health]# kubectl get pods 
NAME    READY   STATUS    RESTARTS     AGE
hello   1/1     Running   3 (9s ago)   75s
 
[root@master health]# kubectl get pods 
NAME    READY   STATUS    RESTARTS      AGE
hello   1/1     Running   4 (35s ago)   2m
 
# After continuosly failing - CrashLoopBackOff
[root@master health]# kubectl get pods 
NAME    READY   STATUS             RESTARTS      AGE
hello   0/1     CrashLoopBackOff   5 (47s ago)   3m17s

Explanation:

  • The startupProbe checks the /healthz endpoint on port 8080.
  • It allows up to 30 failures (failureThreshold: 30) with a 10-second interval between checks (periodSeconds: 10).
  • Once the startup probe succeeds, the liveness and readiness probes take over.

Key Parameters for Probes

  • initialDelaySeconds: The number of seconds to wait before performing the first probe after the container starts.
  • periodSeconds: The interval (in seconds) between probe checks.
  • timeoutSeconds: The number of seconds after which the probe times out.
  • successThreshold: The number of consecutive successes required to consider the probe successful.
  • failureThreshold: The number of consecutive failures required to consider the probe failed.

Comparison of Probes

Probe TypePurposeAction on Failure
LivenessChecks if the container is running and healthy.Restarts the container.
ReadinessChecks if the container is ready to serve traffic.Removes the container from the Service's endpoints.
StartupChecks if the application has started successfully.Delays liveness and readiness probes until the startup probe succeeds.

When to Use Each Probe

  • Liveness Probe: Use when you want Kubernetes to restart a container if it becomes unresponsive or enters a broken state.
  • Readiness Probe: Use when you want Kubernetes to stop sending traffic to a container that is not ready to handle requests.
  • Startup Probe: Use for slow-starting applications to prevent Kubernetes from killing the container before it has fully started.
All systems normal

© 2025 2023 Sanjeeb KC. All rights reserved.