How to periodically delete pod in Kubernetes cluster on GCP

8/20/2019

I'd like to restart my service every day in the morning. When I do it manually I just remove the pod on GCP and it starts again. Alternatively I can use:

helm delete service_name-dev --purge
helm install service_name/ --namespace dev --name service_name-dev --values=service_name/google-dev-values.yaml

It would be easy to schedule it on my laptop, but is it possibile to schedule it on Google Cloud Platform so the service would be rebooted on the given time?

-- Bartłomiej Szałach
cron
google-cloud-platform
kubernetes

1 Answer

8/20/2019

You can create a CronJob object, set it to the schedule you need. Additionally, you need to create the necessary ServiceAccount, Role and RoleBinding resources for it, so it can delete the pods.

When you get those right, you can use the service account token that is mounted into your cronjob pod, to issue, for example, kubectl delete pod -l <your-app-label>. You could do it via kubectl, or through any Kubernetes client library in any language.

Please note that it is actually a bad practice to kill the application periodically, and most possibly an indicator to a problem in the application's design.

-- Utku Özdemir
Source: StackOverflow