Kubernetes increase resources for all deployments
Asked Answered
D

2

8

I am new to Kubernetes. I have a K8 cluster with multiple deployments (more than 150), each having more than 4 pods scaled. I have a requirement to increase resource limits for all deployments in the cluster; and I'm aware I can increase this directly via my deployment YAML. However, I'm thinking if there is any way I can increase the resources for all deployments at one go.

Thanks for your help in advance.

Doubleheader answered 26/7, 2021 at 13:55 Comment(1)
You can try Vertical Pod Autoscaler with "Auto" mode?Coady
T
12

There are few things to point out here:

  1. There is a kubectl patch command that allows you to:

Update field(s) of a resource using strategic merge patch, a JSON merge patch, or a JSON patch.

JSON and YAML formats are accepted.

See examples below:

kubectl patch deploy deploy1 deploy2 --type json -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/resources/limits/memory", "value":"120Mi"}]'

or:

kubectl patch deploy $(kubectl get deploy -o go-template --template '{{range .items}}{{.metadata.name}}{{" "}}{{end}}') --type json -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/resources/limits/memory", "value":"120Mi"}]'

For further reference see this doc.

  1. You can add proper labels into deployment via kubectl set command:

kubectl set resources deployment -l key=value --limits memory=120Mi
  1. Also, you can use some additional CLI like sed, awk or xargs. For example:

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"}]'

or:

kubectl get deployments -o name | awk '{print $1 }' | xargs kubectl patch deployment $0  -p "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"date\":\"`date +'%s'`\"}}}}}"
  1. It is also worth noting that configuration files should be stored in version control before being pushed to the cluster. See the Configuration Best Practices for more details.
Trouveur answered 27/7, 2021 at 9:12 Comment(1)
Does anyone know how to adapt this to also include the namespace? I.E. have kubectl patch all deployments in all (or multiple) namespaces? I tried it with the 2nd one, but how can I pass the relationship between deployment name and namespace out of the $() properly?Necromancy
B
0

You can use kustomize's "components" system if you want to set them all to the same thing. But that's unlikely. Better solution is probably write a little Python (or whatever lang you prefer) script to modify all the YAML files and push them back into source control.

Bogus answered 26/7, 2021 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.