Day 79: Health Checks: Implementing Kubernetes liveness/readiness probes.
Day 79: Health Checks: Implementing Kubernetes Liveness and Readiness Probes
Welcome back, architects and engineers! Today, we’re peeling back another layer of high-scale system resilience. In our journey to build a PHP CMS capable of handling hundreds of millions of requests per second, we've discussed consistent hashing and the persistent runtime model. Now, let's talk about how your system knows when it's truly fit for duty, and more importantly, when it's not.
This isn't about a simple "ping" check. We're diving into Kubernetes Liveness and Readiness probes – mechanisms that, when wielded correctly, are the unsung heroes preventing service disruptions, traffic black holes, and the dreaded "zombie" processes that plague less robust systems.
The "Silent Failure" Problem: Why Basic Health Checks Fall Short
Imagine your PHP-FPM process. It's running. It responds to SIGTERM. A basic HTTP GET / returns a 200 OK. Great, right? Not so fast. What if your application can't connect to the database? Or the caching layer is down? Or a critical third-party API is unreachable? Your PHP-FPM process is "alive" in the OS sense, but your application is effectively dead to your users. This is the "silent failure" problem, and it's a common pitfall in systems that rely on naive health checks.
This is where Kubernetes probes shine. They allow us to define two distinct, yet complementary, dimensions of health:
Liveness Probes: Is my container alive and healthy? If not, Kubernetes should restart it. Think of it as a defibrillator. If the heart stops, shock it back to life.
Readiness Probes: Is my container ready to serve traffic? If not, Kubernetes should temporarily remove it from the load balancer until it is. Think of it as a bouncer. Don't let anyone in until the stage is set and the band is ready to play.
Core Concepts: Beyond "Up" or "Down"
Liveness Probes: Detecting the Undead
A liveness probe tells Kubernetes when to restart a container. If your application gets into a state where it's running but can't make progress (e.g., a deadlock, an out-of-memory condition, or a corrupted internal state), a liveness probe will detect this and trigger a restart.
The Nuance: The most critical insight here is about graceful shutdown. When Kubernetes decides to terminate a pod (due to a failed liveness probe, a deployment update, or node drain), it sends a SIGTERM signal. Your application needs time to gracefully finish requests, flush buffers, and release resources. If your liveness probe fails during this graceful shutdown period, Kubernetes might prematurely restart a pod that was already correctly shutting down.
Keep going with the full lesson
The rest of this lesson — plus its implementation guide, runnable scripts and source code — is included with the course or an All Access plan. Create a free account to start the free lessons in every course.
Already a member? Sign in