K8s: Rolling out all deployments in a namespace - Mon, May 2, 2022
K8s: Rolling out all deployments in a namespace
When updating a config maps that is part of a used by a pod, the Pod is not restarted automatically. In order to facilitate the restart a set of specific pods from the command line you can combine kubectl's selector with xargs :
kubectl get deployments.apps -n my-app -o=jsonpath='{.items[?(@.spec.template.metadata.labels.app\.kubernetes\.io/part-of=="frontend")].metadata.name}' | xargs kubectl rollout restart deployment -n my-app
In the first part of the example above all the deployments which are annotated with
app.kubernetes.io/part-of: frontend
are selected.
Using |
each value is passed to xargs
as an argument which uses it as a parameter for the kubectl rollout restart deployment
command. As a result all the pods are restarted and pick up the changed config map in the process.
Note that this only works for pods having been created from deployments. For stateful sets or other types the first selection needs to be altered accordingly.
Here is another example that does the name for all deployments in a namespace:
kubectl get deployments.apps -n my-app -o=jsonpath='{.items[?(@.spec.template.metadata.namespace=="my-app")].metadata.name}' | xargs kubectl rollout restart deployment -n my-app