Start/stop a Kubernetes cluster in Google Cloud Platform using Google Cloud Scheduler

4/6/2021

I want to start and stop a Kubernetes on GCP using Scheduler. How can we reduce the number of nodes to zero using a corn scheduler at a particular time and again change it back to the required number when using a cron expression?

enter image description here

-- VVR
cron
google-cloud-platform
google-cloud-scheduler
kubernetes
python

1 Answer

4/7/2021

It seems GKE Auto Scale Down for Fun and Profit has everything you need except vise-versa part to restore back 3 nodes. But Im sure you'll manage it. Tested for you, this is work solution.

gcloud beta scheduler jobs list
ID                           LOCATION              SCHEDULE (TZ)                      TARGET_TYPE  STATE
gke-cluster-auto-scale-down  australia-southeast1  0 */1 * * * (Australia/Melbourne)  HTTP         ENABLED

1) enable Google Cloud Scheduler (GKE already enabled):

gcloud services enable cloudscheduler.googleapis.com

2)spin test cluster:

gcloud beta container clusters create vvr \
    --zone="australia-southeast1-a" \
    --machine-type="n1-standard-1" \
    --num-nodes="3" \
    --preemptible \
    --no-user-output-enabled \
  1. create custom role
gcloud iam roles create gke.scheduler \
  --project ${PROJECT_ID} \
  --title "Role GKE Scheduler" \
  --description "Managing the scaling of GKE nodes" \
  --permissions container.clusters.update \
  --stage GA
  1. create ServiceAccount
gcloud beta iam service-accounts create gke-scheduler \
    --description "managing scheduling of worker nodes on gke" \
    --display-name "gke-scheduler"
  1. create IAM role
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
    --member serviceAccount:gke-scheduler@${PROJECT_ID}.iam.gserviceaccount.com \
    --role projects/${PROJECT_ID}/roles/gke.scheduler
  1. Create Cloud Scheduler Job that will decrease nodes num to "nodeCount":0
cloud beta scheduler jobs create http gke-cluster-auto-scale-down \
  --schedule "0 */1 * * *" \
  --uri=https://container.googleapis.com/v1beta1/projects/${PROJECT_ID}/zones/australia-southeast1-a/clusters/vvr/nodePools/default-pool/setSize \
  --message-body '{"nodeCount":0}' \
  --time-zone=Australia/Melbourne \
  --oauth-service-account-email gke-scheduler@${PROJECT_ID}.iam.gserviceaccount.com

Good luck

-- Vit
Source: StackOverflow