For this example we need a Kubernetes installation as described in INSTALLATION.adoc.
Here we are reusing our random-generator which also includes support for health checks.
To apply a Deployment with liveness and readiness check enabled, use
kubectl create -f https://k8spatterns.io/HealthProbe/deployment.yml
This deployment introduces an artificial pause of 20 seconds before the application becomes ready. To monitor the readiness and liveness states you best open an extra terminal with running
kubectl get pods -w NAME READY STATUS RESTARTS AGE random-generator-5856b5f774-54h6b 0/1 Running 0 5s random-generator-5856b5f774-54h6b 1/1 Running 0 38s
The example apps expose to endpoints with which you can switch the state of the readiness and liveness checks. For simplicity reasons, we haven’t installed a Service or Ingress (but of course, you are free to do so!)
Instead we are using a simple port-forwarding directly to the pod to trigger the toggles:
# Port forward to Pod. "pod" is an alias to pick the full name of the pod (see INSTALL.adoc) kubectl port-forward $(pod random-generator) 8080:8080 &
Now you can switch on/off the readiness/liveness checks and see how the cluster manages your pods:
# Check liveness probe by querying the actuator curl -s http://localhost:8080/actuator/health | jq . # Toggle liveness check to off curl -s http://localhost:8080/toggle-live # Check liveness probe again curl -s http://localhost:8080/actuator/health | jq . # Watch the pods and wait a bit. What happens after 2-3 mins ? kubectl get pods -w # Now switch readiness off curl -s http://localhost:8080/toggle-ready # Watch the pods for 1-2 mins kubectl get pods -w # Toggle readiness back on curl -s http://localhost:8080/toggle-ready # Watch the pods again ... kubectl get pods -w