I am running a sample webapp python in Kubernetes.
I am not able to figure out how I make use of probes here.
-
I want app to recover from broken states by automatically restarting pods
-
Route the traffic to the healthy pods only.
-
Make sure the database is up and running before the application (which in my case is Redis).
I have understanding of probes but not sure how/what values exactly to look for
My definition file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
labels:
env: production
app: frontend
spec:
selector:
matchLabels:
env: production
app: frontend
replicas: 1
template:
metadata:
name: myapp-pod
labels:
env: production
app: frontend
spec:
containers:
- name: myapp-container
image: myimage:latest
imagePullPolicy: Always
ports:
- containerPort: 5000
Now I am doing something like this
readinessProbe:
httpGet:
path: /
port: 5000
initialDelaySeconds: 20
periodSeconds: 5
livenessProbe:
httpGet:
path: /
port: 5000
intialDelaySeconds: 10
periodSeconds: 5
3
Answers
In Kubernetes you have three kind of probes:
So, in your case your application should provide one probe that checks if it still running and another if it can server traffic (also checking Redis). Be aware the liveness probes may be dangerous – maye start only with a readiness probe.
If your app provides the health check (incl. Redis) under
/healthz
, this is how you would define your readiness probe.You need to define both
readinessProbe
: allowing to tell whether yourDeployment
is ready to serve traffic at any point of time during its lifecycle. This configuration item can support different command but in your case, this would be anhttpGet
matching and endpoint that you would implement in your web-app (most modern stack define the endpoints by default so check check the documentation of whatever framework you are using). Note that the endpoint handler would need to check the readiness of any needed dependency, in your case you would need to check ifhttp[s]://redis-host:redis-port
responds with successlivenessProbe
: allowing for the control plane to check continuously your pods health and to decide on actions needed to get the cluster to the desired state rolling out any pods failing to report being alive. This probe support different definitions as well and as for thereadinessProbe
most modern framework offer endpoints responding to such request by defaultHere down you can see a sample of both probes, for which you would have two respective HTTP endpoints within your web application:
As we know, there are Liveness, Readiness and Startup Probes in Kubernetes. Check the official Kubernetes Documentation page on this topic.
The Kubelet Node agent can perform these probes on running Pods using 3 different methods:
/health
), and succeeds if the response status is between 200 and 399For this you should use both the readiness and liveness probes. They can both use the same probe method and perform the same check, but the inclusion of a readiness probe will ensure that the Pod doesn’t receive traffic until the probe begins succeeding.
For example you can add minimal health endpoint for a flask python app like this:
And specify your k8s liveness and readiness probe like this:
If you wish you can define a liveness probe command (replace the extra section):
To perform a probe, the kubelet executes the command
cat /tmp/healthy
in the target container. If the command succeeds, it returns 0, and the kubelet considers the container to be alive and healthy. If the command returns a non-zero value, the kubelet kills the container and restarts it.Another option is to define a TCP liveness/readiness probe:
This will attempt to connect to the your container on port 5000. If the probe succeeds, the Pod will be marked as ready.
And for checking health of Redis pods you can put a bash script file in your Redis pod to /health/ping_liveness_local.sh:
and use this section for your redis pods:
If this seems too complicated, then I suggest installing Redis packaged by Bitnami. It is very easy to install it on a Kubernetes cluster using the Helm package manager.
Bitnami guys configured all needed liveness and readiness probes for Redis.
Regarding how to ensure that DB is up and running before your application, check this answer, please. This solution uses K8s Init Containers.