All guides
Cron jobs·3 min read

RECIPE: KUBERNETES CRONJOB

Monitor a Kubernetes CronJob.

1

Store the push URL as a secret

kubectl create secret generic upguru --from-literal=push-url="https://upguru.io/api/push/YOUR-TOKEN"
2

Call it from the CronJob

The container runs the job and then reports the result. Its image needs curl.

apiVersion: batch/v1
kind: CronJob
metadata:
  name: nightly-backup
spec:
  schedule: "0 2 * * *"
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      backoffLimit: 0
      template:
        spec:
          restartPolicy: Never
          containers:
            - name: backup
              image: registry.example.com/backup:1.4   # must include curl
              env:
                - name: UPGURU_PUSH_URL
                  valueFrom:
                    secretKeyRef:
                      name: upguru
                      key: push-url
              command: ["/bin/sh", "-c"]
              args:
                - >-
                  ./backup.sh
                  && curl -fsS -X POST "$UPGURU_PUSH_URL" -d "Backup finished"
                  || curl -fsS -X POST "$UPGURU_PUSH_URL?level=error" -d "Backup failed"
3

Apply it and test

kubectl apply -f cronjob.yaml
kubectl create job --from=cronjob/nightly-backup backup-test
Also set Expect a Push At Least Every: it catches a CronJob that was suspended or can't be scheduled, which never reports a failure itself.