<p>Let's say that I defined a pod that simply runs a few pieces of code and exits afterwards. I need to make sure that this pod exits before allowing other pods to run. What is the best way to implement this?</p>

<p>I used to check whether a pod is ready by performing network requests, e.g. once ready, some webapps pods will block and listen to pre-defined ports, hence I can have the waiting pods performing netcat requests to them. But in this particular case the pod does not need to open any port, hence this approach does not work. Can anyone suggest an alternative?</p>

<p>Thanks</p>

João Matos
<p>If the "pieces of code" needs to be run in every pod start/restart, you maybe are looking for an <a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/" rel="nofollow noreferrer">Init Container</a> implementation:</p>

<blockquote>
  <p>Init Containers are specialized Containers that run before app
  Containers and can contain utilities or setup scripts not present in
  an app image.</p>
</blockquote>

<p>If your code is a dependency for multiple pods and needs to be run once (e.g., on every new deploy), you may consider using a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/" rel="nofollow noreferrer">Job Controller</a>, and implement a logic to check if it's completed before deploying new pods/containers.
(You can use commands like <code>kubectl wait --for=condition=complete job/myjob</code> on your deploy script).  </p>

<p>If you are using helm to deploy, the best option is to use k8s job combined with <code>pre-install</code> and <code>pre-upgrade</code> <a href="https://github.com/helm/helm/blob/master/docs/charts_hooks.md" rel="nofollow noreferrer">helm hooks</a>.</p>
