38
votes

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?

5

5 Answers

23
votes

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.

22
votes

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.

6
votes
initContainers:
    - name: wait-for-dependent-service
      image: stefanevinance/wait-for-200
      env:
        - name: URL
          value: http://dependent-service.{{.Release.Namespace}}.svc.cluster.local:3000

Using https://hub.docker.com/r/stefanevinance/wait-for-200/

5
votes

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.

0
votes

same as @Vishrant Answer... just other way to write bash commands in single line in YAML file

  initContainers:
  - name: wait-for-some-pod
    image: yourDockerImage
    command: ["/bin/sh","-c"]
    args: ['while [ $(curl -ksw "%{http_code}" "https://<pod_health_check_end_point>" -o /dev/null) -ne 200 ]; do sleep 5; echo "health check failed . Waiting for the service..."; done']