Mastering Kubernetes Probes: Examples and Best Practices
Kubernetes probes are essential for ensuring the health and liveness of your applications running on the platform. They help Kubernetes understand when to restart, replace, or do nothing with a container. Let's dive into various Kubernetes probe examples and best practices to make the most of these powerful tools.
Understanding Kubernetes Probes
Before we delve into examples, let's briefly understand the three types of probes Kubernetes offers:
- Liveness Probe: Checks if the application is running and responds to traffic. If it fails, Kubernetes restarts the container.
- Readiness Probe: Determines when a container is ready to start receiving traffic. It's used by services to determine when to route traffic to a pod.
- Startup Probe: Helps Kubernetes understand when a new application is available and ready to receive traffic. It's an optional probe that can be used instead of Liveness and Readiness probes.
Kubernetes Probes Examples
Example 1: Simple Liveness and Readiness Probes
Let's start with a basic example using HTTP GET requests for both Liveness and Readiness probes.

apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-http
spec:
containers:
- name: liveness-http
image: gcr.io/google_containers/liveness
args:
- /server
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 15
timeoutSeconds: 1
readinessProbe:
httpGet:
path: /readyz
port: 8080
initialDelaySeconds: 5
timeoutSeconds: 1
Example 2: Using TCP Probes
Sometimes, you might want to use TCP probes to check if a specific port is open. Here's an example:
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness-tcp
name: liveness-tcp
spec:
containers:
- name: liveness-tcp
image: gcr.io/google_containers/liveness
livenessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 15
Example 3: Executing Commands with Exec Probes
You can also use the `exec` action to run a command within the container. Here's an example using the `cat` command to check if a file exists:
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness-exec
name: liveness-exec
spec:
containers:
- name: liveness-exec
image: gcr.io/google_containers/liveness
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 15
Best Practices for Kubernetes Probes
Here are some best practices to keep in mind when using Kubernetes probes:

- Use different ports for Liveness and Readiness probes to avoid conflicts.
- Set appropriate initial delay and timeout values based on your application's behavior.
- Consider using Startup probes for applications with slow startup times.
- Regularly monitor and update your probes to ensure they accurately reflect your application's health.
Probe Failure Policies
Kubernetes offers several failure policies for probes. Here's a table to help you understand each policy:
| Policy | Description |
|---|---|
| Fail | If the probe fails, the kubelet will mark the container as failed and restart it. |
| No action | The kubelet will not take any action if the probe fails. |
| Restart | The kubelet will restart the container if the probe fails. |
That's it! You now have a solid understanding of Kubernetes probes, along with examples and best practices to help you make the most of these powerful tools. Happy probing!























