Kubernetes Init Container fail fast

5/19/2019

I have an Init Container that does a database version check. If the database version check in the Init Container fails, the whole Deployment should fail immediately. But I still want the other Containers in the Pod to restartPolicy=Always.

However, Init Containers are

retried according to the Pod restartPolicy

It would be good if I could specify a different restartPolicy for the Init Container than the Deployment but that's not possible. Lifecycle Hooks wouldn't really work either as what you need for this use case is a PreStart hook.

Is there a good way to get Init Containers to fail fast?

-- Everett Toews
kubernetes

1 Answer

5/20/2019

Would you like to spin a separate container, which will be checking db version and if succeeds, creates some artifact, like file /tmp/db_version on the host's drive and then you just add livenessProbe/readinessProbe to your main Deployment, which will be checking existence of the /tmp/db_version, e.g.

livenessProbe:
      exec:
        command:
        - ls
        - /tmp/db_version
      initialDelaySeconds: 5
      periodSeconds: 5

Thus you'll be able to sideline restartPolicy of the db_version_check container and your main container.

-- A_Suh
Source: StackOverflow