Introduction
Kubernetes, a powerful platform for coordinating containers, simplifies the management of containerized applications. One of its notable features is CronJobs. This functionality enables you to automate tasks according to a preset schedule. Whether you need to perform backups, generate reports, or conduct regular maintenance, CronJobs have got you covered.
Kubernetes CronJobs enable users to schedule and execute tasks at specific intervals, similar to the Unix cron utility. These CronJobs play a vital role in automating repetitive operations such as backups, report generation, and system maintenance.
Prerequisites
Before you begin, ensure you have the following prerequisites in place:
Download and install kubectl Command-line Tool according to your operating system, by following the official guide for kubernetes.
Set up Kubernetes Cluster.
Configure
kubectl
to connect to your Kubernetes cluster.
Create a CronJob
In this section, set up to start the CronJob. You must create a manifest file outlining these crucial aspects:
The
metadata
section, which encompasses the CronJob's name and labels.The
spec
section, where the schedule, concurrency policy, starting deadline, and job template of the CronJob are defined.The
jobTemplate
section, housing the specifications for the Job generated by the CronJob. This includes details such as pod templates, restart policies, active deadlines, backoff limits, and additional Job parameters.
The schedule field is mandatory, adhering to the cron format with five space-separated fields:
minute (0 - 59) hour (0 - 23) day of the month (1 - 31) month (1 - 12) day of the week (0 - 6) (Sunday to Saturday; 7 represents Sunday on certain systems)
Special characters including asterisks (*), commas (,), hyphens (-), slashes (/), and question marks (?) can be utilized to denote ranges, lists, steps, or values for a field. For example, \5denotes every five minutes, 0 0 1-15/2specifies midnight on alternate days from the 1st to the 15th of each month, and 0 12 * 1-5 signifies noon on weekdays.
The concurrencyPolicy field dictates how the CronJob manages concurrent executions of a Job:
Allow (default): The CronJob permits multiple Jobs to run concurrently.
Forbid: The CronJob disallows concurrent Jobs; if a new Job's scheduled time arrives before the previous Job finishes, the new Job is skipped.
Replace: The CronJob terminates the currently running Job and replaces it with a new instance.
The startingDeadlineSeconds field sets a deadline in seconds for initiating the Job if it misses its scheduled time. If not specified or set to zero, no deadline is imposed.
The jobTemplate field encompasses the specifications for the Job created by the CronJob. It accepts any valid Job object parameters, such as pod templates, restart policies, active deadline seconds, backoff limits, parallelism, completions, and more. Run and test the commands as described in the following steps.
- Open a text editor on your local machine.
Copy and paste the following content into the text editor:
apiVersion: batch/v1 kind: CronJob metadata: name: hi spec: schedule: "* " jobTemplate: spec: template: spec: containers: - name: hi image: busybox:1.28 imagePullPolicy: IfNotPresent command: - /bin/sh - -c - date; echo hi from the Kubernetes cluster restartPolicy: OnFailure
Save the file with the name greetings_cronjob.yaml
.
- Use the kubectl create command to instantiate a CronJob from this file.
Open your terminal. Navigate to the directory where you saved greetings_cronjob.yaml.
Run the following command to create the CronJob:
$ kubectl create -f hello-cronjob.yaml
- Verify CronJob Creation.
Confirm the CronJob's creation using the kubectl get
command.
$ kubectl get cronjobs
Output:
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE hi * False 0 10s
- Get Detailed Information.
Leverage the kubectl describe command, for more detailed information about the CronJob.
$ kubectl describe cronjobs hi
Output:
Name: hi Namespace: default Labels: Annotations: Schedule: * Concurrency Policy: Allow Suspend: False Starting Deadline Seconds: Successful Job History Limit: 3 Failed Job History Limit: 1 Pod Template: Labels: Containers: hi: Image: busybox:1.28 Port: Host Port: Command: /bin/sh -c date; echo Hello from the Kubernetes cluster Environment: Mounts: Volumes: Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulCreate 12s cronjob-controller Created job hi-27218925 Normal SawCompletedJob 11s cronjob-controller Saw completed job: hi-27218925, status: Complete
Manually Run CronJobs
Kubernetes CronJobs are designed to automate tasks based on a specified schedule. However, there are instances where you might want to execute a scheduled task immediately, without waiting for the next scheduled run. In such cases, you can manually trigger a CronJob to run as a Kubernetes Job. Here is how do it:
- Identify the name of the CronJob you want to run manually.
To begin, list all the existing CronJobs.
$ kubectl get cronjobs
- Create a one-time Kubernetes Job.
Run the command to create a manual CronJob.
$ kubectl create job --from=cronjob/manual_cronjob manual-job
This command creates a new Kubernetes Job based on the CronJob's template, effectively running the same tasks as specified in the CronJob's configuration. manual_cronjob
is the name of the CronJob you created manually, above.
- Monitor the progress of the manually triggered CronJob.
Just like any other Kubernetes Job, use the command bellow.
$ kubectl get jobs
Manually triggering a Kubernetes CronJob provides flexibility and control over the execution of tasks. It allows you to perform immediate runs without altering the regular schedule.
Manage a CronJob
After successfully creating a CronJob, proficient management is vital. To execute various tasks through the kubectl command-line tool or the Kubernetes API, follow the steps described bellow.
- List CronJobs
Use kubectl get to list CronJobs in a specific namespace.
$ kubectl get cronjobs -n my-namespace
List CronJobs based on labels.
$ kubectl get cronjobs -l app=my-app
- Describe a CronJob.
Run kubectl describe to get complete details of a CronJob, including its schedule, status, history, and events.
$ kubectl describe cronjobs hi
- Analyze Pod Logs.
Use kubectl logs to inspect the logs of Pods generated by a CronJob. Specify the Pod name to filter Pods.
$ kubectl logs hi-27218925-z9kx8
- Remove a CronJob.
Use kubectl delete, along with its associated Jobs and Pods.
$ kubectl delete cronjobs hi
- Pause and Resume a CronJob.
Use kubectl patch to pause a CronJob by setting spec.suspend to true.
$ kubectl patch cronjobs hi -p '{"spec":{"suspend":true}}'
Resume it by setting spec.suspend to false.
$ kubectl patch cronjobs hi -p '{"spec":{"suspend":false}}'
- Edit or Update a CronJob.
Edit a CronJob interactively through kubectl edit.
$ kubectl edit cronjobs hi
Update it from the manifest file using kubectl apply.
$ kubectl apply -f hi-cronjob.yaml
Cron jobs are suitable for tasks that don't require real-time precision. Make sure the system's clock and time zone are properly configured.
By leveraging Kubernetes CronJobs, organizations can streamline operations and focus on higher-value activities while maintaining a well-managed and optimized Kubernetes cluster.
Conclusion
You have gained insight into defining CronJob manifests, generating CronJobs, listing, viewing, deleting, pausing, resuming, and updating CronJobs, and accessing Pod logs. Furthermore, you have acquired knowledge about the cron format, concurrency policy, and starting deadline of a CronJob. While ideally suited for activities such as backups, report generation, and maintenance. Testing before deploying CronJobs to production and continuous monitoring of their performance and status are crucial. Read Architecting Kubernetes to further advance your knowledge.
Next Steps
To implement additional solutions and leverage the power of your Kubernetes CronJobs, visit the following resources.