51

I use gcloud to provision kubernetes clusters, and use container engine to do so.

Aside from the production cluster, I would like to create short-lived clusters, for testing etc'.

Instead of recreating clusters, I would like to have a test cluster and be able to stop/start on demand. Trying to hit "stop" on the vm instances listed at compute engine will make them restart eventually.

What is the proper way to stop & start a test cluster?

The motivation for short lived test clusters is from cost consideration.

Avihoo Mamka
  • 4,656
  • 3
  • 31
  • 44
pazams
  • 969
  • 1
  • 8
  • 10

4 Answers4

109

You can temporarily scale the number of nodes in your cluster down to zero by running:

gcloud container clusters resize $CLUSTER_NAME --num-nodes=0

Then scale it back up later by running that with a non-zero value for the size flag.

Gehock
  • 11
  • 2
Alex Robinson
  • 12,633
  • 2
  • 38
  • 55
  • Thanks! the docs are a bit tricky on this subject. This [doc](https://cloud.google.com/container-engine/docs/clusters/operations) has all the CRUD operation on a cluster except the U (resize), while the [resize doc](https://cloud.google.com/container-engine/docs/resize-cluster) is detached from the other how-to guides. – pazams Jul 12 '16 at 09:33
  • When I do this on a cluster that was created with `gcloud container clusters create $CLUSTER_NAME --num-nodes 1 --machine-type g1-small`, `gcloud container clusters describe $CLUSTER_NAME` still reports `currentNodeCount: 1`. What am I getting wrong? – Drux Jul 18 '16 at 03:45
  • 1
    Oh, so it apparently takes a few seconds (after `gcloud container clusters resize $CLUSTER_NAME --size=0` returns) for the change to become effective. When taking this into account, now works for me also. – Drux Jul 18 '16 at 03:49
  • 1
    Note that this will not work with all machine types. For example a cluster of f1-micro instances needs to be at least of size 3 (this is enforced both in the GUI console and the CLI). – Oliver Jan 08 '18 at 06:28
27

--zone also shoud be specified to be able to resize cluster nodes to zero in gcloud SDK v 2.0.27

gcloud container clusters resize $CLUSTER --size=0 --zone=$ZONE
Stranger B.
  • 9,004
  • 21
  • 71
  • 108
14

This problem obviously needs an improved solution, as I still had to use the GCP console under Kubernetes Engine -> Clusters.

I changed the number of nodes running in my cluster to 0, as well as changing the minimum number of nodes to 0, because autoscaling was enabled and then it worked.

The gcloud command above provides useful insight, but it fails due to the autoscaling feature that is enabled. A better solution would be to scale down the minimum number of nodes to 0 before resizing the cluster to zero as seen below:

gcloud container clusters update [CLUSTER_NAME] --enable-autoscaling \
    --min-nodes 0 --max-nodes 10 --node-pool [NODE_POOL_NAME]

Or you can disable autoscaling completely:

gcloud container clusters update [CLUSTER_NAME] --no-enable-autoscaling \
--node-pool [NODE_POOL_NAME] --project [PROJECT_ID]]

Then you can now resize the cluster nodes to zero:

gcloud container clusters resize [CLUSTER_NAME] --num-nodes=0
user1338062
  • 11,939
  • 3
  • 73
  • 67
Dalton Whyte
  • 657
  • 1
  • 8
  • 15
7

The same effect can be achieved in the web GCP console by clicking the Edit button on the cluster and setting the size of every node pool to 0.

Reference: https://cloud.google.com/kubernetes-engine/docs/how-to/resizing-a-cluster

sean
  • 877
  • 10
  • 16