Scheduling a restart of a Kubernetes Deployment using CronJobs
Most of my home network runs on my Raspberry Pi Kubernetes cluster, and for the most part it’s rock solid. However, applications being applications, sometimes they become less responsive than they should (or for example, when my Synology updates itself and reboots, any mounted NFS volumes can cause the running pods to degrade in performance). This isn’t an issue with service liveliness, which can be mitigated with a liveness probe that restarts the pod if a service isn’t running.
If my PiHole or Plex deployments become slow to respond I can generally restart the
Deployment and everything springs back into life. Typically this is just a
kubectl rollout restart deployment pihole command to bounce the pods. This is generally pretty safe as it creates a new
ReplicaSet and ensures the
Pods are running before terminating the old ones.
Rather than waiting for performance to degrade, then manually restarting the deployment, I wanted to bake in an automated restart once a week while the family sleep. The added benefit here is also that I can keep my Plex server up to date by specifying
imagePullPolicy: Always. Fortunately, we can make use of the CronJob functionality to schedule the command. To do this I need to create a few objects:
ServiceAccount - an account I can delegate the rights to restart the deployment, as the default service account does not have rights
Role - a role with minimal permissions to perform the actions on the deployment
RoleBinding - to bind the role to the service account
Lastly, I need a CronJob - the Cron-like task definition to run my kubectl command. I’m using the raspbernetes/kubectl image to provide kubectl compatible with my Raspberry Pi’s architecture - if you’re doing it on a different architecture I’d recommend the bitnami/kubectl image.
Once these configurations have been applied, I can view my new setup:
One final tip…if I don’t want to wait until next week to see if the cronjob works, I can create a new job based on the cronjob configuration using the
From the command above I can see that the job is created, then has completed (
COMPLETIONS: 1/1) and the pihole pod was created 65s ago. I can also see the restart-pihole-now job pod has a
STATUS: Completed, and if I check the pod logs I can see the response to the kubectl command.
Hopefully that’s a useful little starter for
CronJobs in Kubernetes - as with cron jobs in Linux or scheduled tasks in Windows, it’s a useful tool in the administrator’s toolbelt!