Kubernetes pod and service dependencies - using init containers

Kubernetes pod and service dependencies - using init containers

When you have multiple pods and services and you want to make sure the pods start in correct order, there is a neat way how to achieve that. It's called initContainers. This is container which starts before the actual container and its purpose is just to check whether the dependent services are available. If not, it just loops until they are.

Example:

Some pod needs to function with JHipster Registry. This kind of snippet has to be added to the actual pod deployment yaml descriptor:

      containers:
      ...
      ...
      initContainers:
        - name: init-myservice
          image: busybox:1.28
          command: ['sh', '-c', "until nslookup jhipster-registry.default.svc.cluster.local; do echo waiting; sleep 2; done"] 

Easy as that. This allows to "heal" the pods or container state and making sure pods start in correct order, without explicitly running deployment of each pod individually.