May 2, 2021

How to trigger a Cron Job manually on Kubernetes

So you've defined a cronjob.yaml manifest, but it doesn't run until midnight and you want to test it now? Simply replace the variables, and run the following command to create a job from the cronjob.

kubectl create job --from=cronjob/{{ cron_job_name }} {{ the-name-of-this-one-off-job }}

This will create a one-off job in your cluster based on your cronjob.yaml manifest, which might look something like this.

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: {{ cron_job_name }}
spec:
  schedule: "0 1 * * *"
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: curl
            image: buildpack-deps:curl
            args:
            - /bin/sh
            - -ec
            - curl https://example.com
          restartPolicy: Never

Checking The Status

To check the status, use the following commands to investigate.

kubectl get jobs --selector=job-name={{ the-name-of-this-one-off-job }}
kubectl get po --selector=job-name={{ the-name-of-this-one-off-job }}
Let's Work Together

I solve problems with DevOps, Developer Relations, IoT, and Artificial Intelligence.