<p>I havw a question, as part of the kubectl deployment process we currently run, we sometimes need to run migrations on the database as part of that process.</p>
<p>We do what we call a rollout restart after we have re-built a tagged Docker image.</p>
<p>However, we usually have anywhere between 2 and 4 pods, with randomly assigned names, e.g.:</p>
<pre><code>web-7f54669b5f-c6z8m             1/1     Running   0          55s
web-7f54669b5f-fp2kw             1/1     Running   0          67s
</code></pre>
<p>To run, say, a database migration command, we could do:</p>
<pre><code>kubectl exec --stdin --tty web-7f54669b5f-fp2kw -- python manage.py migrate --plan
</code></pre>
<p>That's fine. But we were wondering if there a way to just target any of the &quot;web&quot; pods ...?</p>
<p>I'm assuming it's not possible, but not 100% sure why...</p>

Micheal J. Roberts
<p>So, what you can do is add unique labels to all pods and fetch each one of them using labels.</p>
<p>Example:</p>
<pre><code>kubectl exec --stdin --tty $(kubectl get po -l &quot;&lt;label_name&gt;=&lt;label_value&gt;&quot; \
-o jsonpath='{.items[0].metadata.name}') -- python manage.py migrate --plan
</code></pre>
