Scale Down
######################################
## Save Deployment State (excludes kube,mongo,k8 pods)
######################################
kubectl get deploy -A --no-headers | grep -v -E 'kube|mongo|k8s-api-proxy' > deploy_state_before_scale.txt
######################################
## Copy Deployment State to GCS Bucket
######################################
gsutil cp deploy_state_before_scale.txt gs://app1
#######################################
## Scale Deployments to zero
#######################################
kubectl get deploy -A --no-headers | grep -v -E 'kube|mongo|k8s-api-proxy' | awk '{print \$1,\$2}' | while read NS DEPLOY; do kubectl scale --replicas=0 deployment/\$DEPLOY -n \$NS; done
#######################################
## Scale Daemons to zero
#######################################
kubectl -n <namespace> patch daemonset <name-of-daemon-set> -p '{"spec": {"template": {"spec": {"nodeSelector": {"non-existing": "true"}}}}}'
#######################################
## Turn off Autoscaler on GKE nodepools
#######################################
gcloud container clusters update <app1-cluster> --no-enable-autoscaling --region <region> --node-pool <app1nodepool1>
gcloud container clusters update <app1-cluster> --no-enable-autoscaling --region <region> --node-pool <app1nodepool2>
#######################################
## Resize Node Pools to zero
#######################################
gcloud container clusters update <app1-cluster> --num-nodes 0 --region <region> --node-pool <app1nodepool1>
gcloud container clusters update <app1-cluster> --num-nodes 0 --region <region> --node-pool <app1nodepool2>
Scale Up
#######################################
## Resize Node size to 1 for each node pool
#######################################
gcloud container clusters update <app1-cluster> --num-nodes 1 --region <region> --node-pool <app1nodepool1>
gcloud container clusters update <app1-cluster> --num-nodes 1 --region <region> --node-pool <app1nodepool2>
#######################################
## Turn Autoscaling Back on
#######################################
gcloud container clusters update <app1-cluster> --enable-autoscaling --region <region> --node-pool <app1nodepool1>
gcloud container clusters update <app1-cluster> --enable-autoscaling --region <region> --node-pool <app1nodepool2>
#####################################################
## Copy Saved Deployment State from GCS bucket
#####################################################
gsutil cp gs://<app1>/deploy_state_before_scale.txt .
#####################################################
## Scale deployments using the previously saved state file
#####################################################
awk '{print \$1,\$2,\$4}' deploy_state_before_scale.txt | while read NS DEPLOY SCALE; do kubectl scale --replicas=\$SCALE deployment/\$DEPLOY -n \$NS; done
#####################################################
## Scale Daemons back up
#####################################################
kubectl -n <namespace> patch daemonset <name-of-daemon-set> --type json -p='[{"op": "remove", "path": "/spec/template/spec/nodeSelector/non-existing"}]'