I have to scale up and down in one go nearly 200 microservices in my environment is there a possible way to automate scale up and scale down through a script.
CodePudding user response:
While using xargs or parallel or something might work. I suggest using command substitution or similar means, as much as possible, to give Kubernetes right away a list of objects.
For example, you can try the below, which does a dry-run but shows you want would be scaled.
kubectl scale --replicas 2 $(kubectl get deploy -o name) --dry-run=client
The list of deployments, could come also from a text file or somewhere else.
CodePudding user response:
There is for sure a way to script that but we are missing a bit of context about the why you want to do that.
Generaly speaking i would prefer go to the approach of using Horizontal Pod AUtoscaler or even things like knative that allow scaling from zero for doing automated scaling.
But if the reason is unusual such as a maintenance window or something eelse you can go use the scripting approach.
Then 2 you have 2 weapons at your disposal using the raw k8s api through a postman script or something similar or draw your best shell scripting techniques. Going further this way i can imagine something like this will work:
#!/bin/bash
set -eu -o pipefail
cat targets.txt | parallel echo "kubectl scale --replicas 0 -n {}"
where targets.txt is formated this way:
- each line is one microservice with the following format "namespace obejctType/objectName"
Exemple:
default deploy/nginx
anotherns deploy/api
... more services
The advantage of using parallel over doing a simple bash loop is that this will be done in ... parallel :-) This will save you a lot of time.
Also you might also want to include a display/check at the end showing the number of replicas but this wasn't specified in your question.