Shutdown multiple instances in one command or in script

11/7/2016

I am playing with Kubernetes on google compute engines, and I would like to do something that will stop multiple instances at the same time.

I reason of this is, Kubernetes will create a cluster which contains partial common names, for example,

kubernetes-minion-group-1
kubernetes-minion-group-2
......

Kubernetes has the option to delete all the instances, but I couldn't found the option to shutdown them. I don't want to delete all the instance and recreate the next time.

I understand that the following command will delete one instance,

gcloud compute instances stop kubernetes-minion-group-1

but how to stop all of them at the same time in one line or maybe in a bash script? like,

gcloud compute instances stop (instances contains 'kubernetes')

Thank you in advance.

-- Haipeng Su
google-compute-engine
kubernetes

1 Answer

11/21/2016

I'm not sure if there's a cleaner way, but something based on the below using instances list might work:

gcloud compute instances list --filter 'name:kubernetes' --format="csvno-heading"

...which you'd have to parse/grep to get the list. I told it to give a CSV of just the name attributes that match, with no title row (you might be able to play with different topic formats to get one that works exactly like a spaced list. Then:

gcloud computes instances stop [list of names]

It seems 'stop' won't take a filter or wildcard. :/

-- Paul R. Nash
Source: StackOverflow