Our Cron Job example builds on top of the Batch Job example above.
This example assumes a Kubernetes installation is available. In this case, it’s best played with Minikube, as we need some support for PVs. Check the INSTALL documentation for installing Minikube.
To access the PersistentVolume used in this demo, let’s mount a local directory into the Minikube VM that we later then use for a PersistentVolume that is mounted into the Pod:
minikube start --mount --mount-string="$(pwd)/logs:/tmp/example"Then create the PersistentVolume and PersistentVolumeClaim with
kubectl apply -f https://k8spatterns.com/PeriodicJob/pv-and-pvc.ymlbut you don’t have to do it, if you already have your Minikube still running from the Batch Job example [1]
Now create the CronJob which fires every three minutes:
kubectl create -f https://k8spatterns.com/PeriodicJob/cron-job.ymlIn this example we only use only one completion per Job (i.e. the default for a Job).
You can check the logs/random.log for the numbers generated, and also of course the Pods that are run on behalf of the job.
random.log is generated in the logs/ directory over there.