I have the following situation:
I have multiple deployments running the same image and are configured identically except for the environmental variables.
Now my question is, is there an easy way to update for example the image of all these deployments instead of doing it one by one?
I haven't found a solution, but I think this should be possible with the use of labels?
Or is there a better way to deploy the same deployments with only different environmental variables?
Yes, you can, this is what labels are for. They are good for grouping similar objects. Here is the minimal reproducible example.
Create 2 deployments with the same label app=nginx
:
$ kubectl run --image=nginx --overrides='{ "metadata": {"labels": {"app": "nginx"}}, "spec":{"template":{"spec": {"containers":[{"name":"nginx-container", "image": "nginx"}]}}}}' nginx-1
deployment.apps/nginx-1 created
$ kubectl run --image=nginx --overrides='{ "metadata": {"labels": {"app": "nginx"}}, "spec":{"template":{"spec": {"containers":[{"name":"nginx-container", "image": "nginx"}]}}}}' nginx-2
deployment.apps/nginx-2 created
Here are our deployments:
$ kubectl get deploy -o wide --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR LABELS
nginx-1 1/1 1 1 20s nginx-container nginx run=nginx-1 app=nginx,run=nginx-1
nginx-2 1/1 1 1 16s nginx-container nginx run=nginx-2 app=nginx,run=nginx-2
Then we can use set
command and filter desired deployments using label app=nginx
:
$ kubectl set image deployment -l app=nginx nginx-container=nginx:alpine
deployment.extensions/nginx-1 image updated
deployment.extensions/nginx-2 image updated
And see the results:
$ kubectl get deploy -o wide --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR LABELS
nginx-1 1/1 1 1 6m49s nginx-container nginx:alpine run=nginx-1 app=nginx,run=nginx-1
nginx-2 1/1 1 1 6m45s nginx-container nginx:alpine run=nginx-2 app=nginx,run=nginx-2