<p>I have a kubernetes cluster with a node pool. I enabled autoscaling. I defined min &amp; max values of nodes in node pool options.</p>
<p>I observed that I need those nodes during working hours only. During non working hours nodes are mostly idle. I want to save cost by shutting down nodes if they are idle for say - 30 mins.</p>
<p>Is there any option on node pool to configure node based on time period.</p>
<p>I know I can set minimum node to 0 but in that case I have to wait for new node to spin.</p>
<p>Atul Sureka</p>

Atul Sureka
<p>There is no default option however you can write the <strong>cloud function</strong> and trigger it with the <strong>scheduler</strong> to scale down the <strong>GKE nodes</strong> on a <strong>time</strong> basis. i had the same requirement so written <strong>cloud function</strong> script.</p>
<p>i tried two methods</p>
<p><strong>Method 1</strong> : Cloud Scheduler publish a message to <strong>Pub/sub</strong> --&gt; Which processed by <strong>Cloud Function</strong> --&gt; Cloud Function scale the <strong>GKE nodes</strong></p>
<p><strong>Method 2</strong> : Cloud Scheduler send HTTP request to <strong>Cloud function</strong> --&gt; Cloud Function scale the <strong>GKE nodes based on paylob</strong></p>
<pre><code>/* HTTP function get data from event and resize the GKE cluster pools based on data */

const container = require(&quot;@google-cloud/container&quot;);
const client = new container.v1.ClusterManagerClient();

exports.helloHTTP = async (req, res) =&gt; {
  console.log(`Request Body`, req.body);

  const request = {
    projectId:  req.body.projectId,
    zone:       req.body.zone,
    clusterId:  req.body.cluster_id,
    nodePoolId: req.body.node_pool_id,
    nodeCount:  req.body.node_count,
  };

  const result = await client.setNodePoolSize(request);
  const operation = result[0];

  console.log(operation);
  res.status(200);
};
</code></pre>
<p><a href="https://github.com/harsh4870/cloud-function-scale-gke" rel="nofollow noreferrer">GitHub repo</a> &amp; follow my article for more details <a href="https://medium.com/@harsh.manvar111/scale-gke-with-cloud-function-d76f20f9416e" rel="nofollow noreferrer">Medium article</a></p>
