Probes and Application Health: Keeping Things Alive
7 min read
Probes and Application Health: Keeping Things Alive 🏥
Welcome, Kubernetes navigators! Today, we're diving into a crucial topic that keeps your applications robust and reliable: Probes. Think of probes as your application's personal health check-up, ensuring everything is running smoothly.
What Are Probes and Why Do You Need Them?
In the world of Kubernetes, a Pod might be in a "Running" state, but that doesn't necessarily mean the application inside it is healthy and ready to do its job. Your application could be stuck in a loop, unable to connect to a database, or simply taking a long time to start. This is where probes come to the rescue!
The Problem: Without a way to check the internal health of a container, Kubernetes might send traffic to a Pod that isn't ready to handle it, or it might not know to restart a container that has become unresponsive. This can lead to errors for your users and a lot of head-scratching for you.
The Solution: Probes are diagnostic checks that the kubelet (the agent on each node) performs on your containers to get a more granular view of their health. They allow Kubernetes to make intelligent decisions about when to restart a container or when to send it traffic.
There are three main types of probes we'll explore:
- Liveness Probes: Is the application still alive and running?
- Readiness Probes: Is the application ready to start accepting traffic?
- Startup Probes: Has the application successfully started up?
1. Liveness Probes: Restarting Unhealthy Containers Automatically 💔➡️❤️
The Problem: Sometimes, an application can be running but in a broken state. It might be in a deadlock, where it's not crashing but also not making any progress. From the outside, the container looks fine, but it's completely unresponsive.
Definition: A Liveness Probe checks if a container is still running as expected. If the liveness probe fails a configured number of times, the kubelet will kill the container and restart it, attempting to bring it back to a healthy state.
Imagine you have a diligent office assistant who, every few minutes, checks if the coffee machine is working. If it's not brewing (unresponsive), they don't just stare at it; they know to turn it off and on again to fix it. That's your Liveness Probe!
How it Works
The kubelet can perform a liveness check in a few ways:
- HTTP GET Request: Sends an HTTP GET request to a specific endpoint in your container. If it gets a response in the 200-399 range, it's considered a success.
- TCP Socket: Tries to open a TCP connection to a specified port. If the connection is successful, the container is considered healthy.
- Exec Command: Executes a command inside the container. If the command exits with a status code of 0, the probe is successful.
Hands-On Example: HTTP Liveness Probe
Let's say you have a web server that should always be responsive. You can add a livenessProbe to your Pod definition like this:
apiVersion: v1
kind: Pod
metadata:
name: liveness-http-example
spec:
containers:
- name: my-app
image: my-web-app
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
failureThreshold: 3Breakdown of the configuration:
httpGet: Specifies an HTTP check.path: /healthz: The endpoint Kubernetes will hit. It's a common convention to have a/healthzendpoint in your application that returns a 200 status code if everything is okay.port: 8080: The port to check on.
initialDelaySeconds: 5: The kubelet will wait 5 seconds after the container starts before performing the first probe. This gives your app some time to get up and running.periodSeconds: 10: The kubelet will perform a liveness probe every 10 seconds.failureThreshold: 3: If the probe fails 3 consecutive times, Kubernetes will restart the container.
A funny exaggeration: Not setting initialDelaySeconds is like asking someone if they're awake the microsecond their alarm goes off. Give your app a moment to stretch and yawn before you start interrogating it!
2. Readiness Probes: Ensuring Traffic Only Hits Ready Pods 🚦
The Problem: Just because an application has started doesn't mean it's ready to serve requests. It might need to load large configuration files, warm up a cache, or connect to a database. If Kubernetes sends traffic to the Pod during this time, users will see errors.
Definition: A Readiness Probe determines if a container is ready to accept traffic. If the readiness probe fails, Kubernetes doesn't restart the container. Instead, it removes the Pod's IP address from the endpoints of all Services, effectively taking it out of the load balancer pool until it's ready.
Think of a new restaurant. The "open" sign (the container is running) is on, but the chefs are still prepping ingredients. The host at the door (the Readiness Probe) won't seat any customers until the kitchen gives the "all clear."
How it Works
Readiness probes use the same three mechanisms as liveness probes: HTTP GET, TCP Socket, and Exec Command.
Hands-On Example: Command Readiness Probe
Imagine your application needs to ensure a specific file exists before it can serve traffic.
apiVersion: v1
kind: Pod
metadata:
name: readiness-command-example
spec:
containers:
- name: my-data-processor
image: my-data-app
readinessProbe:
exec:
command:
- cat
- /tmp/ready
initialDelaySeconds: 5
periodSeconds: 5Breakdown of the configuration:
exec: Specifies a command to be run inside the container.command: The command to execute. In this case, it'scat /tmp/ready. If the file/tmp/readyexists, the command will exit with a code of 0 (success). If it doesn't, it will exit with a non-zero code (failure).
- The other parameters like
initialDelaySecondsandperiodSecondswork just like in liveness probes.
Industry Standard: It's common practice to use both a liveness and a readiness probe. The liveness probe ensures the container is running, while the readiness probe ensures it's actually ready to do work.
3. Startup Probes: Handling Slow-Starting Legacy Applications 🐢
The Problem: Some applications, especially older, monolithic ones, can take a very long time to start up. If you have a liveness probe with a short initialDelaySeconds, Kubernetes might restart the application before it even has a chance to finish starting, leading to an endless cycle of restarts.
Definition: A Startup Probe is used to check if a container has started correctly. If a startup probe is configured, it disables the liveness and readiness checks until it succeeds. This is ideal for slow-starting containers, preventing them from being killed prematurely.
Let's go back to our restaurant analogy. Imagine a grand opening for a very fancy, complicated restaurant. The health inspector (Startup Probe) agrees to wait a full hour for everything to be perfectly in place before starting the regular, more frequent checks (Liveness and Readiness Probes).
How it Works
Startup probes also use the familiar HTTP GET, TCP Socket, and Exec Command mechanisms.
Hands-On Example: Protecting a Slow Starter
Here's how you can protect an application that sometimes takes a couple of minutes to start:
apiVersion: v1
kind: Pod
metadata:
name: startup-probe-example
spec:
containers:
- name: slow-legacy-app
image: my-slow-app
ports:
- containerPort: 8080
livenessProbe:
httpGet:
path: /healthz
port: 8080
failureThreshold: 3
periodSeconds: 10
startupProbe:
httpGet:
path: /healthz
port: 8080
failureThreshold: 30
periodSeconds: 5Breakdown of the configuration:
startupProbe: This configuration tells Kubernetes to use this probe first.failureThreshold: 30periodSeconds: 5
- This combination gives the application
30 * 5 = 150seconds to start up. During this time, thelivenessProbeis disabled. - Once the startup probe succeeds for the first time, the kubelet will switch to using the
livenessProbefor ongoing health checks. If the startup probe fails30times, the container will be restarted.
Interesting Fact: Startup probes were introduced as a beta feature in Kubernetes v1.18 to specifically address the challenge of slow-starting applications.
Summary & Best Practices 📋
- Liveness Probes: Use them to restart a container when it's in an unrecoverable, broken state. Keep the check lightweight.
- Readiness Probes: Use them to signal when your app is ready to accept traffic. This is crucial for zero-downtime deployments.
- Startup Probes: Use them for applications that have a long and unpredictable startup time, to prevent them from being killed by the liveness probe.
By mastering these three types of probes, you'll significantly improve the resilience and availability of your applications running on Kubernetes. Happy probing! 🕵️♂️
References for Extra Reading 📚
- Official Kubernetes Documentation on Probes: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/
- Google Cloud Blog on Readiness vs. Liveness Probes: https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-setting-up-health-checks-with-readiness-and-liveness-probes
Challenge
You are a DevOps engineer responsible for deploying a new mission-critical application. This application is known for having a long startup time and requires careful health check configuration to ensure high availability. Your task is to complete the provided Kubernetes Pod manifest by adding the necessary Startup, Liveness, and Readiness probes based on the following requirements.
Instructions:
Modify the initialCode to add the probe configurations directly under the app-container definition. Do not alter any other part of the manifest.
Probe Requirements:
Startup Probe:
- The application can take up to 90 seconds to initialize.
- Configure an HTTP GET probe to check the
/startupzendpoint on port8080. - The probe should check every
10seconds. - Set the
failureThresholdhigh enough to give the application at least 90 seconds to start successfully.
Liveness Probe:
- After the application starts, its ongoing health is checked via the
/healthzendpoint on port8080. - Configure an HTTP GET probe for this check.
- The first liveness check should run
15seconds after the startup probe succeeds. - Set the
periodSecondsto20. - The container must be restarted after
3consecutive failures.
- After the application starts, its ongoing health is checked via the
Readiness Probe:
- The application is ready to accept traffic only when it can successfully respond on the
/readyzendpoint on port8080. - Configure an HTTP GET probe for this check.
- Set the
periodSecondsto5. - The pod should be removed from service endpoints after
3consecutive failures.
- The application is ready to accept traffic only when it can successfully respond on the
Expected result
The final YAML should contain three properly configured probes within the containers array element. The startupProbe, livenessProbe, and readinessProbe sections should be correctly indented and contain all the specified parameters.
apiVersion: v1
kind: Pod
metadata:
name: mission-critical-app
spec:
containers:
- name: app-container
image: my-company/slow-startup-app:1.2.0
ports:
- containerPort: 8080
startupProbe:
httpGet:
path: /startupz
port: 8080
failureThreshold: 9
periodSeconds: 10
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 15
periodSeconds: 20
failureThreshold: 3
readinessProbe:
httpGet:
path: /readyz
port: 8080
periodSeconds: 5
failureThreshold: 3