Rename deployment in Kubernetes
Solution 1
Object names are immutable in Kubernetes. If you want to change a name, you can export/edit/recreate with a different name
Solution 2
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
Related videos on Youtube
Comments
-
Erik Rothoff over 1 year
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 googlingkubectl 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 dokubectl run analytics --image=rethinkdb --command -- rethinkdb etc etc
but I feel like it should be possible to simply rename it, no? -
Erik Rothoff over 7 yearsAs a total newbie to kubernetes: why is this? What is in the name that needs to be so immutable? Thanks!
-
Tiger93 over 7 yearsThe name is the identity of the object. An object with a different name is a different object. One goal of the API is idempotent operations. If a create operation encounters a network error, it should be able to be retried and either get an AlreadyExists error (if the previous try actually succeeded server side) or the create would work as intended. If an object could have been renamed in the meantime, it's not clear how the subsequent create request should behave.