This tutorial explains how you can reduce the cost by scheduling a scale-down GKE cluster to 0 nodes and again schedule scaling up based on your requirements.
Here I am going to explain two ways to schedule the scale-up/down of cluster nodes.
Using Cluster Autoscaling
Cluster Autoscaler in GKE automatically resizes the number of nodes in a given node pool, based on the demands of your workloads. When demand is low, the cluster Autoscaler scales down to a minimum size (which you will set while configuring the node pool as in the below image). This can increase the availability of your nodes in node pools when you need it while controlling costs. You don’t need to manually add or remove nodes or over-provision your node pools. Instead, you specify a minimum and maximum size for the Node pool, and the rest is automatic.
Steps –
- Go to cluster
- Go inside the Node pool
- Click on Edit (On top)
- Click on Autoscaling (It will turn on this feature)
Here you can set the minimum and maximum number of nodes in a Node pool.
Limitations:
Cluster Autoscaler never automatically scales to 0 nodes. One or more nodes must always be available in the cluster to run the System pods, so if your requirement is to scale down the Node pool to 0 nodes you cannot do so by following the Autoscaling method.
To cover the above requirement, we have another method.
Using the setSize method by HTTP request
Here I am going to use some terms that you might not be familiar with, I will explain them first, and if you know then you are good to skip them.
Cloud Scheduler – Cloud Scheduler provides a reliable and scalable solution for job scheduling, eliminating the need for users to manage their own scheduling infrastructure. It integrates seamlessly with other GCP services, enabling users to trigger tasks on services like Cloud Pub/Sub, Cloud Functions, App Engine, and HTTP endpoints.
OAuth Token – OAuth (Open Authorization) tokens are used to authenticate and authorize access to GCP resources and APIs. OAuth is an open standard protocol that allows users to grant limited access to their resources on one website or application to another website or application, without sharing their credentials directly.
API Keys – In Google Cloud Platform (GCP) API & Services, an API key is a simple and widely used method for authenticating requests to GCP APIs. It is a unique identifier that allows an application or service to access GCP APIs on behalf of the project it belongs to. API keys are typically used for non-user-specific access and provide a way to track and manage API usage.
First Let’s generate the API Key –
- Open APIs and Services, Go to Credentials
- Click on Create Credentials, this will generate a new API Key which you will able to see in the API keys section –
This API key will not have any restrictions means you can use this key for calling any API, if you want to restrict it for certain APIs then
- Click on the three-dot sign and click Edit
- Go to the API restrictions section and select Restrict Key
- Select the API for which you want to restrict the current API Key.
- Save
Now you have the API key, Let’s create the scheduler job for Node pools.
- Open Cloud Scheduler
- Give the job Name, select the Region, and then give the job frequency in Cron format (based on that this job will trigger) finally give the time zone (according to this time zone job will be triggered) and continue. See the following image –
- In Configure the Execution section first select HTTP as the Target type and then in the URL section paste the URL using the below format. (Google Docs)
https://container.googleapis.com/v1/projects/{projectId}/zones/{zone}/clusters/{clusterId}/nodePools/{nodePoolId}/setSize?key={API-key}
- Select POST as the HTTP method.
- Add headers (take reference from the below image).
- In the Body Section give the number of nodes for that Node pool.
- In Auth header select Add OAuth token
- And then Select the Service account (This SA should have access to resize the Node pool)
- Continue
- You can also check Configure optional settings which are optional.
- Finally, click on CREATE.
Limitations –
- Node pool autoscaling should be turned off.
- You need to manually increase the number of nodes in the Nodes whenever necessary.
My suggestion is, it is not efficient to use the second method in the Production environment because every time you manually need to increase the nodes whenever traffic will increase. But it will be very useful in your testing or development environment, it will save lot of cost.
That is all for now.
Happy Reading.
Stay tuned for more articles on Cloud computing and DevOps. Don’t forget to follow me for regular updates and insights.
This works, thanks