<p>Two pods <code>app</code> and <code>postgres</code> are successfully created and are able to communicate through each other's services in a node. In the current process, the two pods get created at the same time, but that can be changed to have them be created/started in a sequence.</p>

<p>Initially, the database container in the <code>postgres</code> pod is empty and needs to be seeded. The seed process goes through the <code>app</code> pod and so, it needs to be up and running too. Once <code>postgres</code> is seeded, <code>app</code> still is not aware of this new data, and needs to be restarted. This is a flaw in <code>app</code> itself, that I have low control over.</p>

<p>Right now, the process is:</p>

<pre><code>kubectl create -f pods.yaml       # creates `app` and `postgres` pods
kubectl exec app -- bash -c "&lt;seed command&gt;"
kubectl delete pod app
sleep 45                          # takes a while for `app` to terminate
kubectl create -f pods.yaml       # Ignore the "postgres pod and service already exist" error
</code></pre>

<p><strong>Is there a better way of automatically co-ordinating a restart of <code>app</code> once <code>postgres</code> reaches a seeded state?</strong></p>

<p>Perhaps there is some aspect/feature set of Kubernetes that I'm missing entirely which helps with such a circumstance....</p>

<p>You can use a "readiness probe" on the postgresql pod that will not report the container as ready before the data is imported (e.g. query the DB or Table you import). You app container can query the readiness status of the db pod in order to restart automatically once it reports ready. 
The readiness probe can be a script performing the import. Here is an example (you need to replace the "SHOW DATABASES" command with whatever applies in your case):</p>

<pre><code>spec:
  containers:
  - name: mysql
    image: mysql:latest
    ports:
    - containerPort: 3306
      name: mysql
    readinessProbe:
      exec:
        command:
        - /path-in-container/readiness-probe.sh
      initialDelaySeconds: 15
      timeoutSeconds: 5
</code></pre>

<p>readiness-probe.sh:</p>

<pre><code>#!/bin/bash

MYSQL_USER="readinessProbe"
MYSQL_PASS="readinessProbe"
MYSQL_HOST="127.0.0.1"

mysql -u${MYSQL_USER} -p${MYSQL_PASS} -h${MYSQL_HOST} -e"SHOW DATABASES;"

if [ $? -ne 0 ]; then
  exit 1
else
  exit 0
fi
</code></pre>

<p>To read more on the topic refer to k8s docs: </p>

<p><a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/" rel="nofollow noreferrer">readiness probes</a> </p>

<p><a href="https://kubernetes.io/docs/user-guide/walkthrough/k8s201/#health-checking" rel="nofollow noreferrer">health checking</a></p>
