<p>A new Kubernetes Deployment with 2+ replicas for high availability.</p>

<p>I want to be able to execute a command on the first pod only, let's say create a DB, and let the other replicas wait for the first one to complete.<br>
To implement this, I just want to know in the pod if this is replica #1 or not.</p>

<p>So in the pod's entry point I can test:</p>

<pre><code>if [ $REPLICA_ID -eq 1 ]; then 
    CreateDB
else
    WaitForDB
fi
</code></pre>

<p>Can this be done in Kubernetes?</p>

<p>in Kubernetes a <code>Deployment</code> is considered stateless and therefore doesn't provide the feature you're looking for. You should rather look into <code>StatefulSet</code> and their features.</p>

<p>A <code>StatefulSet</code>e.g. supports <a href="https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/#ordered-pod-creation" rel="noreferrer">ordered creation</a> and when combined with the generally available <code>readinessProbe</code> for you pods you could create the desired behaviour. Also the pod name is stable within a <code>StatefulSet</code> so your test could then be done with the <code>hostname</code> of the <code>Pod</code>.</p>
