Kubernetes - wait for other pod to be ready

6/28/2018

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?

-- ohwelppp
kubernetes

3 Answers

6/28/2018

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.

-- Michael Hausenblas
Source: StackOverflow

10/8/2018

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.

-- Vishrant
Source: StackOverflow

3/8/2020

Using wait-for-it.sh is actually quite easy:

  initContainers:
  - name: wait-for-app1
    image: image-docker-containing-sh
    args:
    - /bin/sh
    - -c
    - /usr/app/wait-for-it.sh app1:portapp1 -t 0

Of course retries and timeouts are the way to go, but this works great as a workaround.

-- Javier Aviles
Source: StackOverflow