I have two applications - app1 and app2, where app1 is a config server
that holds configs for app2
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.
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.
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.
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/