Kubernetes - wait for other pod to be ready

后端 未结 4 959
难免孤独
难免孤独 2020-12-03 03:07

I have two applications - app1 and app2, where app1 is a config server that holds configs for app2

相关标签:
4条回答
  • 2020-12-03 03:24

    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.

    0 讨论(0)
  • 2020-12-03 03:26

    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.

    0 讨论(0)
  • 2020-12-03 03:33

    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 讨论(0)
  • 2020-12-03 03:34
    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/

    0 讨论(0)
提交回复
热议问题