How to edit all the deployment of kubernetes at a time
Asked Answered
W

1

6

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.

Woodsia answered 29/8, 2018 at 14:59 Comment(1)
What sort of help are you looking for? It sounds like you have the core of a script to do it already; the sort of mass update you describe isn’t a typical Kubernetes operation and the CLI and API tools don’t support it directly.Innuendo
J
18

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).

Jacobina answered 29/8, 2018 at 15:41 Comment(1)
Thank you so much for this answer the "kubectl patch deployment" helped to do what i want.Woodsia

© 2022 - 2024 — McMap. All rights reserved.