Lab23 - Liveness and Readiness Probes
Lab23 - Liveness and Readiness Probes
Introduction:
Liveness: - Liveness probes let Kubernetes know if your app is alive or dead. If your app is alive, then
Kubernetes leaves it alone. If your app is dead, Kubernetes removes the Pod and starts a new one to
replace it.
Readiness: - Readiness probes are designed to let Kubernetes know when your app is ready to serve
traffic. Kubernetes makes sure the readiness probe passes before allowing a service to send traffic to
the pod. If a readiness probe starts to fail, Kubernetes stops sending traffic to the pod until it passes.
• Liveness Probes
• Readiness Probes
• Clean up
1.1 Let us clone the git repository which contains manifests required for this exercise, by
executing the below command.
Note: We have created a deployment that starts Nginx server in the background and a while
loop that keeps the container up and running, even if the Nginx Service fails.
Output:
1.5 Let us capture the cluster ip of the service, by executing the below commands.
# kubectl get svc nginx
# CLUSTER_IP=$(kubectl get svc nginx -o
jsonpath='{.spec.clusterIP}')
# echo $CLUSTER_IP
Output:
Let us kill the Nginx process to simulate a broken POD, that still lives in the sense that the
container is up and running. For that, let us find the process ID.
1.8 Here, we can see that the process ID of the nginx process is 6. Let us kill the process now:
1.11 To overcome this issue, let us add a Liveness Probe and re-deploy the application. First
let us delete the old deployment, by executing the below command.
# kubectl delete -f ~/k8s-probes/deployment.yaml
1.12 Let us view the manifest file, by executing the below command.
# cat -n ~/k8s-probes/deployment_live_probe.yaml
Output: Truncated output, Liveness probe is added to previous deployment.
Output:
Note: Let us wait for 20 seconds and run the next command.
1.19 Let us list the pod, by executing the below command.
1.20 Now the let us access the application, by executing the below command.
# curl -s -v $CLUSTER_IP 2>&1 | head -n 10
Output:
Note: With the help of the Liveness Probe, the problem has healed itself. Any POD that does
not respond properly is restarted automatically.
Readiness probes, in turn, are used to detect how long a container needs for booting up the
application properly. Without a readiness probe, A rollout of a non-functional version of a
ReplicaSet or Deployment will stop the rollout after detecting that the first new POD is non -
functional. With that, the service is not degraded, giving the administrator a chance to mitigate
the problem. We want to see, what happens to an exposed service, if a POD/container starts
with a long boot time. For this, we create a service with two PODs, one of which has a boot
time of 120 sec. We simulate this situation by running a 120 seconds sleep command before we
run the actual application Therefore, statistically, every second curl request will fail. This is
because the HTTP requests are load-balanced between the POD that is working already and the
one, which is still in the boot process, leading to a „failed “status. The problem is caused by
both endpoints being added the service right away, even though one of the PODs is not
responsive yet.
2.1 Let us view the manifest file, by executing the below command.
# cat -n ~/k8s-probes/pods.yaml
Output:
Output:
2.3 Let us capture the cluster ip of the service, by executing the below command.
Output:
Ctrl+c
If you wait long enough (> 2 minutes), then all curl commands will be successful again,
indicating that the slower nginx POD is ready as well.
In the next step, we will improve the initialization procedure by adding a readiness probe.
2.5 Let us delete the slow-boot pod and recreate it with readiness probe
# kubectl delete pod nginx-slowboot
2.6 Let us view the manifest of POD with a readiness probe, by executing the below command.
# cat -n ~/k8s-probes/slowboot.yaml
Output: Truncated output…
Output:
2.9 Let us list the end points, by executing the below command.
# kubectl get ep | grep nginx
Output:
Ctrl+C
All curl requests will be successful. The reason is, that the endpoint of the slowly booting POD is
not added to the list of the endpoints of the service before it successfully replies to the
readiness probe. This way, you never will create a black hole for the HTTP requests.
If you wait for more than 60 seconds, stop the while loop with <ctrl> -C and look for the list of
endpoints, the second endpoint will be available, though:
# kubectl get ep | grep nginx
Output: