As a business scales, we constantly try to balance profitability against the cost of running our systems, which makes cloud cost management critical.
If you run your workloads on a Kubernetes cluster (GKE), here are a few tips that can work to keep your compute costs under control.
While the examples use GKE and gcloud commands, the principles and steps can still apply to your own cloud provider, just use the equivalent CLI tools available in your environment.
Simple tips:
- Separate Spot nodes (AWS)/ Preemptible nodes (GCP) node pools
A Spot Instance is an instance that uses spare EC2 capacity that is available for less than the On-Demand price.
Preemptible VM instances are available at much lower price—a up to 91% discount—compared to the price of standard VMs
Create dedicated node pools for stateless and non-critical workloads to take advantage of lower-cost compute.
1 | gcloud container node-pools create stateless-preemptible-pool \ |
Use Kubernetes scheduling rules
Apply node selectors, node affinity, and tolerations to ensure workloads run only on the intended node pools.
1 | apiVersion: apps/v1 |
- Scale down non-essential workloads after weekdays
Scale deployments to zero for applications not required outside business hours , such as self-hosted runners, internal tools and dashboards, and sandbox environments and scale the Spot/Preemptible node pools hosting these applications to zero.
1 | import os |
1 | # Use a slim, supported Python base image |
- Automate the whole Process
Use a scheduled job K8s Cronjob to automate this
Create the SA account
1 | export PROJECT_ID="my-gcp-project" |
Create the Cron job
1 | apiVersion: batch/v1 |
Using the yaml format above, you can set two cron tasks:
- One that scales down deployment replicaCount and Nodepool count to zero after work hours on Friday evening.
schedule: "0 19 * * 5" - One that scales up deployment replicaCount and Nodepool count on Monday morning.
schedule: "0 8 * * 1"- just make sure to increase the key-value"replicaCount": 0and"count": 0to values greater than zero.