Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename deployment in Kubernetes

Tags:

kubernetes

If I do kubectl get deployments, I get:

$ kubectl get deployments
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
analytics-rethinkdb   1         1         1            1           18h
frontend              1         1         1            1           6h
queue                 1         1         1            1           6h

Is it possible to rename the deployment to rethinkdb? I have tried googling kubectl edit analytics-rethinkdb and changing the name in the yaml, but that results in an error:

$ kubectl edit deployments/analytics-rethinkdb
error: metadata.name should not be changed

I realize I can just kubectl delete deployments/analytics-rethinkdb and then do kubectl run analytics --image=rethinkdb --command -- rethinkdb etc etc but I feel like it should be possible to simply rename it, no?

like image 945
Erik Rothoff Avatar asked Sep 10 '16 16:09

Erik Rothoff


People also ask

How do I rename a Deployment name in Kubernetes?

As others mentioned, kubernetes objects names are immutable, so technically rename is not possible.

How do I change the Deployment on Kubernetes?

Updating a Kubernetes Deployment You can edit a Deployment by changing the container image from one version to the other, decreasing or increasing the number of instances by changing the ReplicaSet value. etc. For example, the container image nginx we have been using in our exercises has many versions.

How do I change the Deployment file in kubectl?

Edit PODs and DeploymentsRun the kubectl edit pod <pod name> command. This will open the pod specification in an editor (vi editor). Then edit the required properties.

How do I rename a container in Kubernetes?

I'm sorry, but object names in kubernetes cannot be changed. They're immutable. You'd have to delete and deploy the Deployment with a different name to get what you want.


2 Answers

Object names are immutable in Kubernetes. If you want to change a name, you can export/edit/recreate with a different name

like image 167
Jordan Liggitt Avatar answered Sep 20 '22 14:09

Jordan Liggitt


As others mentioned, kubernetes objects names are immutable, so technically rename is not possible.

A hacking approach to emulate some similar behavior would be to delete an object and create it with a different name. That is a bit dangerous as some conflicts can happen depending on your object. A command line approach could look like this:

    kubectl get deployment analytics-rethinkdb -o json \
        | jq '.metadata.name = "rethinkdb"' \
        | kubectl apply -f - && \
    kubectl delete deployment analytics-rethinkdb
like image 35
Pedreiro Avatar answered Sep 17 '22 14:09

Pedreiro