Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ryderdamen/73ff9f93cd61d5dd45a0c50032e3ae03 to your computer and use it in GitHub Desktop.
Save ryderdamen/73ff9f93cd61d5dd45a0c50032e3ae03 to your computer and use it in GitHub Desktop.
How to trigger a Cron Job manually on Kubernetes

Triggering 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 }}
@tonyma
Copy link

tonyma commented May 19, 2021

is there a way to pass parameters to the job when manually creating a Kubernetes job?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment