Rename deployment in Kubernetes

9/10/2016

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?

-- Erik Rothoff
kubernetes

2 Answers

9/10/2016

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

-- Jordan Liggitt
Source: StackOverflow

6/7/2019

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
-- Pedreiro
Source: StackOverflow