If you’ve ever needed to schedule a single or recurring task in your Kubernetes clusters, it’s just a cronjob away. Jack Wallen shows you how.
Kubernetes is one of the most flexible and powerful container managers on the market. The deeper you dive into the technology, the more you realize it can do. Take for instance, Kubernetes’ ability to use the Linux cron tool for the scheduling of tasks.
Using the combination of Kubernetes and cron make it possible to not just create recurring tasks, but scheduled tasks. With the scheduling of tasks, you are able to have them run at a time when usage is low, as to not bog down your cluster with tasks that are necessary but may not be directly related to your production workflow.
So how do you integrate cron into your Kubernetes cluster? You do it on a manifest-by-manifest basis. I’m going to show you how to do just that. I’ll demonstrate with a very basic task, so you can easily understand the concept.
What you’ll need
In order to add cron jobs, you’ll need a running Kubernetes cluster. If you’ve yet to deploy one, check out my article: How to deploy a Kubernetes cluster on Ubuntu server.
SEE: Implementing DevOps: A guide for IT pros (free PDF) (TechRepublic)
How to create the YAML file
The first thing we must do is create a YAML file for the task. Remember, this is a very basic task, so our YAML file won’t contain too much information. Let’s break it down.
The first section defines the API version and the kind of job we’re creating. That section looks like:
apiVersion: batch/v1beta1 kind: CronJob
The next section details the metadata for the job. That section looks like:
metadata: name: cron-hello
Next, we have the specs for the job. This section contains our parameters for cron and is defined by way of the schedule option, which will run the task every 15 minutes. This section looks like:
spec: schedule: "0,15,30,45 * * * *"
Now we have the job template section, some of which will remain blank (for simplicity). This section defines the image used for the container to be deployed–in this case, busybox. It also includes a few arguments, one of which is the output to be printed for the job (remember, this is a simple task). The output will be the date and a text message printed every 15 minutes, as defined by the schedule option.
This section looks like:
jobTemplate: spec: template: spec: containers: - name: cron-hello image: busybox args: - /bin/sh - -c - date; echo Hello, TechRepublic! restartPolicy: OnFailure
Create the new file with the command:
In that file, paste the entire contents of the manifest shown below:
apiVersion: batch/v1beta1 kind: CronJob metadata: name: cron-hello spec: schedule: "0,15,30,45 * * * *" jobTemplate: spec: template: spec: containers: - name: cron-hello image: busybox args: - /bin/sh - -c - date; echo Hello, TechRepublic! restartPolicy: OnFailure
Save and close the file.
How to deploy the job
Now we’re going to deploy the job that includes the scheduled task. To do that, issue the command:
kubectl create -f cron.yaml
Once the job runs, it’ll report that cron-hello has been created (Figure A).
You can ensure the cronjob task is running with the command:
kubeclt get cronjobs
This will list out every cronjob task on your cluster (Figure B).
How to watch for the cronjob
Let’s say you’ve deployed a container with an associated cronjob and you want to watch for it. To do that, you’d issue the command:
kubectl get jobs --watch
The output of that command will include a running list of every deployed job (Figure C).
To end that watch, use the Ctrl+C combination.
How to delete the cronjob
Deleting a cron-scheduled job is quite simple. To do this, issue the command:
kubectl delete cronjob NAME
Where NAME is the name used in the metadata section of the YAML file.
And that’s the gist of using cronjob to schedule single and recurring tasks to your Kubernetes cluster. This simple tool can go a long way to make your Kubernetes admin job a bit easier. Give it a go and see if you don’t wind up making use of cronjob more and more.
View original article here Source