Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes - wait for other pod to be ready

Tags:

kubernetes

I have two applications - app1 and app2, where app1 is a config server that holds configs for app2. I have defined /readiness endpoint in app1 and need to wait till it returns OK status to start up pods of app2.

It's crucial that deployment of app2 wait till kubernetes receives Http Status OK from /readiness endpoint in app1 as it's a configuration server and holds crucial configs for app2.

Is it possible to do this kind of deployment dependency?

like image 957
ohwelppp Avatar asked Jun 28 '18 09:06

ohwelppp


People also ask

How do you know when pods are ready?

If the output from a specific pod is desired, run the command kubectl describe pod pod_name --namespace kube-system . The Status field should be "Running" - any other status will indicate issues with the environment. In the Conditions section, the Ready field should indicate "True".

How do you use kubectl wait?

Use Wait command We utilize the 'wait' command to recess until the pods meet the requirements. Use kubectl apply to relate the variations to the cluster and wait a randomly set amount of time (60 seconds) to check the status of the pod. At this point, we expect the fresh deployment to be active and the old one removed.

What is restartPolicy in Kubernetes?

The restartPolicy applies to all containers in the Pod. restartPolicy only refers to restarts of the containers by the kubelet on the same node. After containers in a Pod exit, the kubelet restarts them with an exponential back-off delay (10s, 20s, 40s, …), that is capped at five minutes.

What is Initcontainers?

This page provides an overview of init containers: specialized containers that run before app containers in a Pod. Init containers can contain utilities or setup scripts not present in an app image. You can specify init containers in the Pod specification alongside the containers array (which describes app containers).


2 Answers

You need to use initContainers. Following is an example of how you can do in your YAML file

initContainers: - name: wait-for-other-pod   image: docker.some.image   args:   - /bin/sh   - -c   - >     set -x;     while [ $(curl -sw '%{http_code}' "http://www.<your_pod_health_check_end_point>.com" -o /dev/null) -ne 200 ]; do       sleep 15;     done 

I have used curl to hit the health check endpoint, you can use any other UNIX command to check if the other pod is ready.

like image 79
Vishrant Avatar answered Oct 08 '22 20:10

Vishrant


Yes, it's possible using Init Containers (also, see this blog post for some background re timing) but a better, more Kubernetes-native pattern is to use retries and timeouts rather than hard-coding dependencies in this way.

like image 29
Michael Hausenblas Avatar answered Oct 08 '22 19:10

Michael Hausenblas