Understanding Kubernetes Probes
Kubernetes Probes are essential diagnostic checks that Kubernetes uses to understand the health of your containers. They allow the container orchestrator to make intelligent decisions about when to restart a failing container, when to send traffic to a newly started container, or when a container is still initializing. Without probes, Kubernetes would have to rely on more basic, often less effective, mechanisms to manage container lifecycles, potentially leading to service disruptions or inefficient resource utilization.
At its core, a probe is a diagnostic action that a Kubelet performs on a container. Based on the result of the probe, Kubelet can take action. There are three types of probes defined by Kubernetes:
- Liveness Probes: Determine if a container is running. If the liveness probe fails, the Kubelet kills the container and restarts it.
- Readiness Probes: Determine if a container is ready to serve traffic. If the readiness probe fails, the endpoint is removed from all Services targeting that Pod.
- Startup Probes: Determine if a container has started up and is ready to serve traffic. If the startup probe fails, the Kubelet restarts the container. Startup probes are useful for applications that have a long startup time.
These probes are crucial for maintaining high availability and ensuring that your applications remain responsive and accessible. They provide a more sophisticated way to manage container health than simply checking if a process is running.
Liveness Probes: When to Restart
The Liveness Probe is your first line of defense against unresponsive containers. When a container starts, Kubernetes begins executing the Liveness Probe at a configured interval. If the probe fails, Kubernetes assumes the container is in an unrecoverable state and restarts it. This is critical for applications that might hang or enter a deadlock state where the process is still running but no longer functional.
A Liveness Probe can be configured in three ways:
execprobe: Executes a command inside the container. A non-zero exit code signals failure.httpGetprobe: Performs an HTTP GET request to a specified path and port on the container's IP address. A non-2xx or non-3xx status code signals failure.tcpSocketprobe: Performs a TCP connection to a specified port on the container's IP address. If the port is open, the probe succeeds.
Key parameters for Liveness Probes include:
initialDelaySeconds: The number of seconds after the container has started before liveness probes begin. This is important to give your application time to start up without triggering a false failure.periodSeconds: How often (in seconds) to perform the probe.timeoutSeconds: How long to wait (in seconds) for the probe to complete. A timeout is considered a failure.successThreshold: Minimum consecutive successes for the probe to be considered successful after having failed. Defaults to 1.failureThreshold: Minimum consecutive failures for the probe to be considered failed. After a failure, the container will be restarted. Defaults to 3.
Consider an example where a web server process might be running, but it has stopped responding to requests due to a memory leak or a deadlock in its request handling logic. A Liveness Probe configured to check if the web server can serve a basic health endpoint would detect this failure and trigger a restart, ensuring that users don't encounter a perpetually broken service.
Readiness Probes: When to Send Traffic
While Liveness Probes ensure a container is running, Readiness Probes determine if a container is ready to accept traffic. A container might be running (passing its Liveness Probe) but still not be ready to serve requests. This is common during application startup, when a service might need to connect to a database, load configuration, or perform other initialization steps.
If a Readiness Probe fails, Kubernetes removes the Pod's IP address from the endpoints of any Service that selects the Pod. This effectively takes the Pod out of rotation, preventing it from receiving new traffic until it becomes ready again. Once the probe starts succeeding, the Pod's IP is added back to the Service endpoints.
The configuration options for Readiness Probes are identical to Liveness Probes (exec, httpGet, tcpSocket) and share the same key parameters like initialDelaySeconds, periodSeconds, timeoutSeconds, successThreshold, and failureThreshold.
A common scenario for Readiness Probes is an application that depends on an external service, like a database. When the application container starts, it might attempt to connect to the database. If the database is temporarily unavailable or slow to respond, the application might not be ready to serve requests. A Readiness Probe that checks the application's ability to successfully query the database will prevent traffic from being sent to the application until it can reliably interact with its dependencies. This is like a restaurant kitchen that is open and the lights are on (liveness), but the chef isn't ready to take orders because the ingredients haven't arrived yet (readiness).
Referenced Sources
- verified
