<p>Why do pod names have 5 random alphanumeric characters appended to their name when created through a kubernetes deployment? Is it possible to get rid of them so that the pods names don't change? I am frequently deleting and creating deployments and would prefer that pod names don't change.</p>

<p>Update: I would like to have the same name because I am constantly deleting/recreating the same deployment and if the name doesn't change, then I can quickly reuse old commands to exec into/see the logs of the containers.</p>

<p><a href="https://i.stack.imgur.com/wvi32.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/wvi32.jpg" alt=""></a></p>

user674669
<p><strong>Reason for having random alphanumeric in pod names:</strong> </p>

<p>When we create a <code>deployment</code>, it will <strong>not directly create pods</strong>(to match the replica count). </p>

<ul>
<li>It will create a <strong>replicaset</strong> (with name = deployname_name + 10 digit aplhanumeric). But <strong>why</strong> extra alphanumeric ? When we do <strong>upgrade</strong> of deployment, <strong>new</strong> replicaset is create with new alphanumeric and old is kept as it is. This old replicaset is used for <strong>rollbacks</strong>.</li>
<li>The created replicaset will create <strong>pods</strong> (with name = replicaset_name + 5 digit alphanumeric). But <strong>why</strong> extra alphanumeric? We cannot have two pods with same name.</li>
</ul>

<p>If your usecase is to use the old commands frequently, then going for <code>Statefulset</code> is <strong>not the good solution</strong>. Statefulsets are <code>heavy weight</code>(ordered deployment, ordered termination, unique network names) and they are specially designed to preserve state across restart (in combination with persistent volume).</p>

<p>There are few <strong>tools</strong> which you can use:</p>

<ol>
<li><a href="https://github.com/wercker/stern" rel="noreferrer">stern</a></li>
<li><a href="https://github.com/arunvelsriram/kube-fzf/" rel="noreferrer">kube-fzf</a></li>
</ol>

<p><strong>Lightweight solution to your problem:</strong></p>

<ol>
<li><p>You can use <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/" rel="noreferrer">labels</a> to get the same pod across deployments:</p>

<pre><code>kubectl get pods -l app=my_app,app_type=server
NAME                             READY   STATUS    RESTARTS   AGE
my-app-5b7644f7f6-4hb8s   1/1     Running   0          22h
my-app-5b7644f7f6-72ssz   1/1     Running   0          22h
</code></pre></li>
</ol>

<p>after this we can use some bash magic get what we want like below</p>

<p><strong>Final command:</strong></p>

<pre><code>kubectl get pods -l app=my_app,app_type=server -o name | rg "pod/" -r "" | head -n 1 | awk '{print "kubectl logs " $0}' | bash
</code></pre>

<p><strong>Explanation:</strong></p>

<ol>
<li><p>get list of pod names</p>

<pre><code>kubectl get pods -l app=my_app,app_type=server -o namenames
pod/my-app-5b7644f7f6-4hb8s
pod/my-app-5b7644f7f6-72ssz
</code></pre></li>
<li><p>replace pod/ using <code>ripgrep</code> or <code>sed</code> (rg "pod/" -r "")</p></li>
<li>take only one pod using <code>head -n 1</code></li>
<li>use awk to print exec/see_log command</li>
<li>pipe it to bash to execute</li>
</ol>
