This lesson explains implementing and validating readiness and liveness probes on Kubernetes web applications to ensure healthy pod traffic management and automatic recovery.
This lesson explains how to implement and validate readiness and liveness probes on your Kubernetes web application. Follow along as we inspect your deployment, test with HTTP requests, scale your application, and configure probes to ensure only healthy pods receive traffic and that failing pods are automatically recovered.
A provided test script (curl-test.sh) sends HTTP requests to verify the web server’s response. Run the script as shown below:
Copy
Ask AI
root@controlplane ~ ➜ ./curl-test.shMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OK^Croot@controlplane ~ ✗
At this stage, only one pod (simple-webapp-1) is handling the traffic.
To improve scaling, a second pod (simple-webapp-2) is introduced. Run the test script again to observe the behavior:
Copy
Ask AI
root@controlplane ~ ➜ ./curl-test.shMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKFailedFailedMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKFailedFailedFailedFailed
Here, although simple-webapp-1 responds correctly, some requests to simple-webapp-2 fail because it is receiving traffic even though it’s not yet ready.
The readiness probe will ensure that a pod only receives traffic when it is ready, preventing failed requests.
To stop the failed requests, configure a readiness probe for simple-webapp-2. This probe sends an HTTP GET request to the /ready endpoint on port 8080.
Export the current configuration of simple-webapp-2:
Copy
Ask AI
root@controlplane ~ ➜ kubectl get pod simple-webapp-2 -o yaml > webapp2.yaml
Delete the existing pod to prepare for the updated configuration:
Copy
Ask AI
root@controlplane ~ ➜ kubectl delete pod simple-webapp-2
Edit the webapp2.yaml file to include the readiness probe under the container configuration. Insert the following snippet:
Copy
Ask AI
readinessProbe: httpGet: path: /ready port: 8080
Below is an example configuration for simple-webapp-2:
5. Verifying Load Balancing After Applying the Readiness Probe
Re-run the test script to ensure that traffic is now only directed to pods that are ready. Initially, traffic might still be sent to simple-webapp-1, but once simple-webapp-2 becomes ready, load balancing will occur:
Copy
Ask AI
root@controlplane ~ ➜ kubectl apply -f webapp2.yamlpod/simple-webapp-2 createdroot@controlplane ~ ➜ ./curl-test.shMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-1 : I am ready! OKMessage from simple-webapp-2 : I am ready! OK^C
At this point, traffic is successfully balanced between the two pods.
To understand the behavior when a pod fails, a crash script (crash-app.sh) is provided to simulate a failure in simple-webapp-2:
Copy
Ask AI
root@controlplane ~ ➜ ./crash-app.shMessage from simple-webapp-2 : Mayday! Mayday! Going to crash!root@controlplane ~ ➜
After running the crash script, check the pods using:
Copy
Ask AI
root@controlplane ~ ➜ kubectl get pod
The test script (curl-test.sh) will temporarily show that requests are routed only to simple-webapp-1 until Kubernetes restarts the crashed pod automatically.
When a pod crashes, Kubernetes automatically restarts it; however, during the downtime, ensure that your application can handle the temporary loss of a pod.