How to edit all the deployment of kubernetes at a time

8/29/2018

We have hundreds of deployment and in the config we have imagePullPolicy set as “ifnotpresent” for most of them and for few it is set to “always” now I want to modify all deployment which has ifnotpresent to always.

How can we achieve this with at a stroke?

Ex:

kubectl get deployment -n test -o json | jq.spec.template.spec.contianer[0].imagePullPolicy=“ifnotpresent”| kubectl -n test replace -f - 

The above command helps to reset it for one particular deployment.

-- Nishanth
docker
kubernetes
kubernetes-deployment

1 Answer

8/29/2018

Kubernetes doesn't natively offer mass update capabilities. For that you'd have to use other CLI tools. That being said, for modifying existing resources, you can also use the kubectl patch function.

The script below isn't pretty, but will update all deployments in the namespace.

kubectl get deployments -o name | sed -e 's/.*\///g' | xargs -I {} kubectl patch deployment {} --type=json -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/imagePullPolicy", "value": "Always"}]'

Note: I used sed to strip the resource type from the name as kubectl doesn't recognize operations performed on resources of type deployment.extensions (and probably others).

-- Grant David Bachman
Source: StackOverflow