<p>I am writing a shell script for executing a pod for which the syntax is:</p>

<pre><code>winpty  kubectl --kubeconfig="C:\kubeconfig" -n namespace exec -it podname bash
</code></pre>

<p>This works fine but since podname is not stable and changes for every deployment so is there any alternative for this?</p>

<p>Thanks.</p>

Ash Raf
<p>You can use normally <a href="https://kubernetes.io/docs/tasks/debug-application-cluster/get-shell-running-container/" rel="nofollow noreferrer"><code>$ kubectl exec</code></a> command but define value for changing pod name. </p>

<p>Assuming that you have deployment and labeled pods: app=example, simply execute:</p>

<pre><code>$ kubectl exec -it $(kubectl get pods -l app=example -o custom-columns=:metadata.name) -- bash
</code></pre>

<p><strong>EDIT:</strong></p>

<p>You can also execute:</p>

<pre><code>POD_NAME = $(kubectl get pods -l app=example -o custom-columns=":metadata.name")
</code></pre>

<p>or</p>

<pre><code>POD_NAME = $(kubectl get pods -l app=example -o jsonpath = "{. Items [0] .metadata.name}")
</code></pre>

<p>finally </p>

<pre><code>$ winpty kubectl exec -ti $POD_NAME --bash
</code></pre>

<p>Make sure that you execute command in proper namespace - you can also add <code>-n</code> flag and define it.</p>
