CronJobs help you to perform a task inside the Kubernetes cluster at a given schedule. Let’s create a CronJob for an application that is run on Docker and Kubernetes.
CronJobs help you to perform a task inside the Kubernetes cluster at a given schedule.
Jobs in Kubernetes are really important when your application runs in a Kubernetes environment. We can identify two types of Jobs which are Jobs & CronJobs in Kubernetes, and both of these Jobs are doing the same kind of tasks. In my previous article, I talked about Jobs in Kubernetes and I explained some important features in Jobs. In this article, I talk about CronJobs in Kubernetes and some important features in it.
CronJob helps you to run your task at a given schedule so that you can define at what time it should run to complete your task. It could be hourly, weekly, monthly, or you can set any other time you prefer. When you add a CronJob for your application there’s a separate area in the configuration file for you to set up your schedule. Also, you’ll get a separate jobTemplate section in CronJobs which you don’t find in Jobs.
Let’s create a CronJob for an application that is run on Docker and Kubernetes.
To start with, I will stick to a basic configuration file, and later I will explain each section in more detail.
apiVersion: batch/v1beta1 kind: CronJob metadata: name: node-app-cronjob namespace: node-app spec: schedule: "* * * * *" jobTemplate: spec: template: spec: containers: - name: node-app-cronjob image: alpine command: ["echo", "This is CronJob in Kubernetes"] restartPolicy: Never
The above code snippet displays a default CronJob configuration file and when you set the
“* * * * *” it will create a new job in each minute and also a new pod at the same time.
You can deploy this CronJob into the cluster using the command below.
$ kubectl apply -f <file-name>eg: kubectl apply -f sample-cronjob.yaml
This will create a new CronJob and a relevant pod. Use the below command to see the events of pod creating and you will see once the time passes over 60 seconds (1 minute), it will create another CronJob and as well as a new pod.
$ kubectl get pod --watch
You can use the
kubectl get cronjob command to see the available CronJobs inside the cluster.
Use the below command to delete your CronJob.
$ kubectl delete -f sample-cronjob.yaml
This command usually deletes all the CronJobs and Pods created from the above process, but if you found any cronjob or a pod is left inside your cluster, then you will have to delete them manually using
kubectl delete cronjob <cronjob-name> & kubectl delete pod <pod-name> commands.
Our original Kubernetes tool list was so popular that we've curated another great list of tools to help you improve your functionality with the platform.
In this article, we break down everything you need to know about DevOps, so that you can get started building your own CI/CD pipeline. DevOps Tutorial - Docker, Kubernetes, and Azure DevOps
Get Hands-on experience on Kubernetes and the best comparison of Kubernetes over the DevOps at your place at Kubernetes training
At some point we've all said the words, "But it works on my machine." It usually happens during testing or when you're trying to get a new project set up. Sometimes it happens when you pull down changes from an updated branch.
Kubernetes and Docker Tutorial will help you understand the basic concepts of a Kubernetes and Docker along with a practical demo.