CronJob
Last updated
Last updated
Previously we already create a Job to backup the postgres database service that we have. But its inconvenient to run this job manually every time we want to backup. We can use CronJob for this.
CronJob create a job on a repeating schedule. It runs a Job periodically on a given schedule, written in Cron format.
I usually use this website () to check if my cron format is correct and will behave as expected or not.
Lets create a CronJob that will run a job we previously created to dump PostgreSQL database. The basic CronJob definition/configuration is simple.
We just need to define our CronJob above, specify the schedule
(in here I use 0 0 * * *
so it will run every midnight), and then put the job definition we previously created into the jobTemplate
section.
Your full file should looks like this.
Lets name the file as postgres-backup-cronjob.yaml
then apply the file.
Validate that your CronJob is properly created.
As you can see above we now have a CronJob that will create a Job every midnight to run pg_dump
. We also can explicitly specify a timezone or if we left it empty it will be defaulted to local timezone.
For CronJobs with no time zone specified, the kube-controller-manager interprets schedules relative to its local time zone.
Waiting the job to run at midnight is not ideal when we want to test if our CronJob will run properly or not. In this case we can manually trigger the scheduled job using this command below.
Lets get list of the job and you should see the manually triggered job. Like before, we can get the job logs to make sure that it run successfully.