In Kubernetes, liveness and readiness probes are mechanisms to determine the health of your application. The kubelet uses these probes to know when to restart a container (liveness) and when a container is ready to start accepting traffic (readiness).

Probes Overview

  • Liveness Probe: Checks if the application is alive or dead. If the probe fails, Kubernetes will restart the container.

  • Readiness Probe: Checks if the application is ready to accept requests. If the probe fails, Kubernetes will stop sending traffic to the pod until it passes.

Configuring Liveness Probes

You can define a liveness probe in your Pod configuration YAML file.

Example using an HTTP GET request:

apiVersion: v1
kind: Pod
metadata:
  name: my-app
spec:
  containers:
  - name: my-app-container
    image: my-app-image
    livenessProbe:
      httpGet:
        path: /healthz
        port: 8080

Example using a command:

livenessProbe:
  exec:
    command:
    - cat
    - /app/is_alive

To apply, run:

kubectl apply -f my-app-pod.yaml

Configuring Readiness Probes

Readiness probes are configured similarly to liveness probes.

Example:

readinessProbe:
  httpGet:
    path: /ready
    port: 8080

To apply, run:

kubectl apply -f my-app-pod.yaml

Troubleshooting Probe Failures

  1. Check the Pod events:
kubectl describe pod my-app
  1. Check the kubelet logs on the node where the pod is running:
journalctl -u kubelet

Considerations and Best Practices

  1. Timing: Be cautious with the initialDelaySeconds and timeoutSeconds settings to give your application enough time to start.

  2. Endpoints: Choose meaningful endpoints for HTTP probes. Don’t use heavy endpoints that could slow down your system.

  3. Failure Threshold: Set an appropriate failure threshold before the kubelet takes action.

  4. Resource Consumption: Probes should be lightweight and not consume significant CPU and memory.

Complete Exercise

Setup

  1. Create a YAML file named probe-exercise.yaml for a Pod with both readiness and liveness probes.

Steps

  1. Apply the YAML file:
kubectl apply -f probe-exercise.yaml
  1. Check the Pod status:
kubectl get pod probe-exercise
  1. Trigger a failure artificially and observe the Pod behavior.

Troubleshooting

  • If the Pod is not starting as expected, describe the Pod to see events.

Solution

Check if the Pod restarts when the liveness probe fails and becomes ready when the readiness probe passes.