Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a running pod name?

Tags:

kubernetes

  1. eg. I have 2 pods, they were called nginx-111-xxx, nginx-111-yyy, but there is something wrong in nginx-111-xxx, I need to create a new pod to replace it because the demands acquire you to delete nginx-111-xxx, then a NEW pods nginx-111-zzz, Can I rename nginx-111-zzz with nginx-111-xxx? I try to use kubectl edit command to achieve it,
  2. But Kubernetes said copy of your changes has been stored to "/tmp/kubectl-edit-oh43e.yaml" error: At least one of apiVersion, kind and name was changed, so If there is a way to change the name of the running pod ?enter image description here
like image 403
Layne Liu Avatar asked Sep 12 '25 22:09

Layne Liu


2 Answers

It's not possible to change the name of a running pod, as the API will not accept this.

The random pod names come in fact you are using Kubernetes deployments, which are the default or most commonly used ways to run pods on Kubernetes. But there are different ways to schedule pods, beside of that.

However, it looks like you need more predictable pod names for some reason. Here are some ways to go:

  1. use StatefulSets (https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/). The names of pods created by a StatefulSet will go like nginx-01, nginx-02 ..
  2. use pods directly. With that, you won't have those rolling update features from replicasets, DaemonSets etc, but absolute control about naming
like image 134
David Steiman Avatar answered Sep 14 '25 11:09

David Steiman


I strongly recommend using statefulSet as advised. However, Keep in mind that editing specifications of a running pod should be approached with caution to avoid disruptions. The following fields can be edited for an existing pod:


 - spec.containers[*].image
 - spec.initContainers[*].image
 - spec.activeDeadlineSeconds
 - spec.tolerations

To make changes, you have two options:

Using kubectl edit:

Run kubectl edit pod <pod name> to open the pod specification in the vi editor. Attempting to save changes directly will be denied, but a copy of the modified file is saved at a temporary location. Delete the existing pod with kubectl delete pod , then create a new pod with the changes using kubectl create -f /tmp/kubectl-edit-<random-string>.yaml. in your case kubectl create -f /tmp/kubectl-edit-oh43e.yaml

Using exported YAML:

Extract the pod definition to a YAML file with kubectl get pod <pod name> -o yaml > my-new-pod.yaml. Edit the exported file using an editor like vi (vi my-new-pod.yaml). Save the changes, delete the existing pod with kubectl delete pod , and create a new pod using kubectl create -f my-new-pod.yaml

like image 31
linkonabe Avatar answered Sep 14 '25 12:09

linkonabe