Monitor Kubernetes CronJobs with a heartbeat
A CronJob that misses its start, can't pull its image or fails every night leaves a trail of Job objects and events that nobody reads. A Tissue Mast heartbeat expects a beat after every run and pages you when one is missing or reports a failure.
Put the heartbeat URL in a Secret, hand it to the container as an environment variable, and wrap the command so it sends /start before the job and its exit code after. Anything that stops the pod from getting that far shows up as a missing beat.
The Secret
kubectl create secret generic mast-heartbeat \ --from-literal='url=https://mast.tissue.dev/mk_8e2a…'
The URL is a write capability, so it belongs in a Secret and out of the manifest you commit.
The CronJob
apiVersion: batch/v1
kind: CronJob
metadata:
name: nightly-backup
spec:
schedule: "17 3 * * *"
timeZone: "America/Los_Angeles"
concurrencyPolicy: Forbid
startingDeadlineSeconds: 600
jobTemplate:
spec:
backoffLimit: 0
template:
spec:
restartPolicy: Never
containers:
- name: backup
image: ghcr.io/your-org/backup:1.4 # needs sh and curl
env:
- name: HEARTBEAT_URL
valueFrom:
secretKeyRef:
name: mast-heartbeat
key: url
command: ["/bin/sh", "-c"]
args:
- |
rid="$HOSTNAME"
curl -fsS -m 10 --retry 3 -X POST "$HEARTBEAT_URL/start?rid=$rid" >/dev/null
/usr/local/bin/backup.sh
status=$?
curl -fsS -m 10 --retry 3 -X POST "$HEARTBEAT_URL/$status?rid=$rid" >/dev/null
exit $status
timeZone- Stable since Kubernetes 1.27. Without it the schedule follows the time zone of the controller manager. Kubernetes rejects
CRON_TZorTZwritten inside the schedule itself. concurrencyPolicy: Forbid- Skips a run while the last one is still going, so two backups never fight over the same files. The default,
Allow, runs them side by side. backoffLimit: 0andrestartPolicy: Never- One attempt per run, so one exit code per run. If you'd rather let Kubernetes retry, send the beat with
&&on success only and let a run that never succeeds go down when its grace runs out. rid="$HOSTNAME"- The pod's name, used as a run id. Kubernetes says a CronJob can occasionally create two Jobs for one scheduled time, and the run id lets Mast pair each end with its own start.
exit $status- Hands the backup's own exit code back to Kubernetes, so the Job still shows as failed in
kubectl.
What a missing beat covers
The beat only goes out once the container is running its command. So the same missing beat stands for every failure before that point: an image that can't be pulled, a pod that can't be scheduled, a start missed by more than startingDeadlineSeconds, a CronJob someone suspended, or a cluster that's down. Mast waits for the grace window and then pages, and the card names the run it was waiting for.
Once the command runs, a non-zero exit code pages straight away with the code on the card, nightly-backup exited 1. Set a longest run on the heartbeat and a pod that hangs after /start pages too, with is still running.
The heartbeat to go with it
Give the heartbeat the CronJob's schedule and time zone:
curl -X PATCH https://api.tissue.systems/v1/mast/alert-sources/nightly-backup \
-H "Authorization: Bearer $TISSUE_TOKEN" -H "Content-Type: application/json" \
-d '{"schedule": "17 3 * * *", "schedule_tz": "America/Los_Angeles",
"grace_secs": 1800, "max_duration_secs": 5400}'
Or in the web app, under Heartbeats. Leave the zone empty on both sides to work in UTC. The grace should cover startingDeadlineSeconds, the time to pull the image and the backup's own run time.
A heartbeat can be kept as code next to the manifest, too: alert sources as code covers that.
Free is ten heartbeats with an email address and no credit card. Team is $29 a month for 250 heartbeats, with rotations and escalation for everyone on call.