Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How can I setup a cronjob on only one instance in an autoscale group?

Hi

 
I'm moving my server from on prem to GCP. In the GCP i set up the autoscale to cater my website needs accordingly. I need to run a script for every 15 minutes for the schedule task. However i want the script to run on only one server. And not on multiple server if it goes to autoscale.
 
Have anybody encountered this before?
1 1 342
1 REPLY 1

Hi @lompat31,

Welcome to Google Cloud Community!

It is not possible to directly schedule a cronjob on only one instance in an autoscaling group as all instances in an instance group have to be identical using the same template. However, if you need to run the cronjob from within a single instance, you can use a separate instance that is not part of the managed instance group (MIG). This will also avoid the problem where if an instance autoscales, and/or gets re-deployed by the autohealing property of the MIG, the cronjob might not be able to run completely. 

In addition, you might also want to consider the following GCP Services to set up your Cronjob: 

  • Google Kubernetes - has a scheduling capability that can be configured to run a task on a specific pod.
  • Cloud Scheduler - allows setting up of scheduled work units (cronjobs) to be executed at defined times or regular intervals.

I hope the above information is helpful.