Why do pod names have 5 random alphanumeric characters appended to their name when created through a kubernetes deployment?

2/2/2019

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.

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.

-- user674669
kubernetes

3 Answers

2/2/2019

This is how deployments works, every time pod dies, ReplcaSet create pod with different name to match desired state and random number attached to pod name to give them unique names.

Whatever you are trying to achieve is not possible with deployment object as they are intended for stateless applications. As you want to preserve state( name) of application this is certainly possible with StatefulSet.

So if you use StatefulSet object to manage replicas, every pod will be created with certain name convention, e. g. POD_NAME-1, POD_NAME-2 etc i. e. Index will be appeneded to pod name. Also when pod dies, new pod will created with same name.

Ao you want to achieve is ideal use case of StatefulSet. Go for it.

-- Rajesh Deshpande
Source: StackOverflow

2/5/2019

Reason for having random alphanumeric in pod names:

When we create a deployment, it will not directly create pods(to match the replica count).

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

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

There are few tools which you can use:

  1. stern
  2. kube-fzf

Lightweight solution to your problem:

  1. You can use labels to get the same pod across deployments:

    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

after this we can use some bash magic get what we want like below

Final command:

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

Explanation:

  1. get list of pod names

    kubectl get pods -l app=my_app,app_type=server -o namenames
    pod/my-app-5b7644f7f6-4hb8s
    pod/my-app-5b7644f7f6-72ssz
  2. replace pod/ using ripgrep or sed (rg "pod/" -r "")

  3. take only one pod using head -n 1

  4. use awk to print exec/see_log command
  5. pipe it to bash to execute
-- Dinesh Balasubramanian
Source: StackOverflow

2/2/2019

If you deploy a pod from deployment object, kind:Deployment, then the deployment controller appends a unique name to pod that is part of specific deployment. This is how the deployment controller looks up all the relevant pods of respective deployment. This is needed for rolling upgrade, rollback functions

-- P Ekambaram
Source: StackOverflow