<p>I have this pod</p>
<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
  labels:
    app: myapp
    myapp: db
    subdomain: name
  annotations:
    app.myapp.com/uuid: some-uuid
    app.myapp.com/domain: domain
</code></pre>
<p>And I want to use <a href="https://kubernetes.io/docs/tasks/configure-pod-container/assign-pods-nodes-using-node-affinity/" rel="nofollow noreferrer">this</a> to make another pod wait for mysql, is it possible?</p>

Rodrigo
<p>Pod affinities are just a way to tell the scheduler where to place pods, in relation to other containers. However the scheduler will not look at Pod statuses when doing so. Thus there is no way to &quot;wait for mysql&quot;, using pod affinities.</p>
<p>Usually, an easy way to implement some &quot;wait for something&quot; during your pods startup would be to add some initContainer.</p>
<p>In StatefulSets, Deployments, ReplicationControllers, Daemonsets, ... in addition to the <code>containers</code> array describing containers that should always be running, there is an optional <code>initContainers</code> array. This one is meant to list containers that will start, then exit, BEFORE containers from the <code>containers</code> array would start.</p>
<p>See: <a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/workloads/pods/init-containers/</a></p>
<p>Docs gives the following sample:</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! &amp;&amp; sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', &quot;until nslookup myservice.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for myservice; sleep 2; done&quot;]
[...]
</code></pre>
<p>I'm not sure I would use some nslookup... Usually, since your main container image has proper runtime to query your database, you should be able to re-use it with your init container.</p>
<p>In your case, it could be listing table from your mysql database, and retrying until you get an answer.</p>
