简体   繁体   中英

How to wait until another pod starts running using init containers?

  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', "until nslookup myservice; do echo waiting for myservice; sleep 2; done"]

I am using nslookup to wait until another service comes up. However, that does not solve my problem. As service will comes up but the corresponding app - pod maybe not ready. In this case, service dependency wont be handle.

How to ensure that one app1 is running before start app2?

I had similar problems with docker-compose in the past. There are two approaches I used to resolve them. Both of them assume that services will expose a port to listen to.

  1. Simple and easy : wait for the specific port of the application to be up an listening with a timeout command. This is very similar to what you are already doing. So your command will be something like:
     initContainers: - name: init-myservice image: busybox:1.28 command: ['sh', '-c', "until timeout 2 bash -c \"</dev/tcp/myservice/${MY_SERVICE_TCP_PORT}\"; do echo waiting for myservice; sleep 2; done"]
  2. Versatile but with extra dependency : Use the wonderful dockerize tool. A lightweight binary that will control the dependencies between containers. It also does many other things, like control the log from a log file, or perform configuration templating. The only downside is that you are introducing a new dependency in your container, but in my opinion is totally worth it. In this case you can perform tcp and http checks of liveness for the services, and even introduces headers in the http ones (very useful for authenticated endpoints).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM