Cronwize
Guide · Kubernetes

Monitor a Kubernetes CronJob

Copy spec.schedule and spec.timeZone into the monitor, then ping from the container.

1. Create the monitor from the manifest

Paste spec.schedule as written, and pick the same zone as spec.timeZone. Without spec.timeZone, the schedule is read in the zone of the Kubernetes control plane, so set it to keep the two in step.

2. Ping from the container

apiVersion: batch/v1
kind: CronJob
metadata:
  name: nightly-backup
spec:
  schedule: "0 2 * * *"
  timeZone: "Europe/Belgrade"
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      backoffLimit: 0
      template:
        spec:
          restartPolicy: Never
          containers:
            - name: backup
              image: registry.example.com/backup:1.4.0
              env:
                - name: PING
                  valueFrom:
                    secretKeyRef:
                      name: cronwize
                      key: ping-url
              command:
                - /bin/sh
                - -c
                - |
                  curl -fsS -m 10 -o /dev/null "$PING/start"
                  /app/backup.sh
                  code=$?
                  curl -fsS -m 10 -o /dev/null "$PING/$code"
                  exit "$code"

The image needs curl and a shell. Keep the ping URL in a Secret, because the key is its only credential.

  • $PING/$code reports the exit status: 0 is success and anything else marks the monitor down at once.
  • With backoffLimit: 0 a failed run is not retried. If you allow retries, each attempt sends its own pings, and a later success recovers the monitor after the first failure has already alerted.

3. Why this catches more than job failures

A run that never starts sends no ping. That covers a suspended CronJob, a missed schedule, an image that cannot be pulled and a Pod that cannot be scheduled, none of which the job could report itself.

4. Check the first ping

Create a one-off run from the CronJob. The monitor turns from new to up.

kubectl create job --from=cronjob/nightly-backup nightly-backup-test