Kubectl generate pod with same name all time

8/20/2018

I am new to kubernetes. Is there anyway we can fix the name of pod ? If we are creating only one replica, then I want to be generated with same name all time. It's generating different name all time. If I want to see the logs of a container, each time I need to change the command with the newly generated pod name.

Following is sample of YAML file.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nagendra-app-deploy1
spec:
  replicas: 1
  template:
    metadata:
      name: nagendra-app-deploy1
      labels:
        app: nagendra-app-deploy1
    spec:
      containers:
      - name: nagendra-spring-app1
        image: springbootapp:v1
        ports:
        - containerPort: 8080
      - name: nagendra-myImage
        image: myImage:v2
-- nagendra547
containers
docker
kubectl
kubernetes

3 Answers

11/15/2019

If you don need a deployment (no need for rolling updates, no replica set that watches if the pod is up) you can simple deploy a pod.

A simple pod will keep it's name through the updates.

-- Shridhar Hegde
Source: StackOverflow

8/20/2018

There is no way to generate the same name for a deployment produced pod. as far as the command is concerned, you can use kubectl get po -l app=nagendra-app-deploy1 -o jsonpath={.items[0].metadata.name} to get the pod's name.

-- Kun Li
Source: StackOverflow

8/20/2018

Here is the trick, I use to get the logs of the deployment

kubectl logs -f $(kubectl get deployment -o name | grep DEPLOYMENT_NAME | head -n 1)

for example, in your situation, I just need to change deployment_name which is quite static.

kubectl logs -f $(kubectl get deployment -o name | grep nagendra-app-deploy1 | head -n 1)

-- Suresh Vishnoi
Source: StackOverflow