Kubernetes CronJob
Create a heartbeat check with the same schedule and time zone as the CronJob, then end the job container's own command with the ping. Only a container that started, did the work and succeeded can send it.
The manifest
A nightly backup at 03:00 in Paris, with the ping chained after the work. Change
YOUR-PING-UUID to the UUID on the check's page, and the image and script to your
own:
apiVersion: batch/v1
kind: CronJob
metadata:
name: nightly-backup
spec:
schedule: "0 3 * * *"
timeZone: "Europe/Paris"
concurrencyPolicy: Forbid
jobTemplate:
spec:
backoffLimit: 2
template:
spec:
restartPolicy: OnFailure
containers:
- name: backup
image: registry.example.com/backup:1.4.2
command:
- /bin/sh
- -c
- |
/app/backup.sh &&
{ curl -fsS -m 10 --retry 3 -o /dev/null https://staging.marsolen.com/ping/YOUR-PING-UUID || true; }
Apply it with kubectl apply -f backup-cronjob.yaml. The check turns green after the
first run that succeeds. A run that fails, and a run that never starts, send nothing, so the
check goes red when its ping is late.
Why it is written this way
The ping is in the command, after &&
&& runs the ping only when /app/backup.sh exits 0. Anything that
stops the work stops the ping too: a script that fails, a container killed for running out of
memory, an image that cannot be pulled, a pod that is never scheduled. That last pair is the
one that hurts. An image tag that no longer exists leaves the pod in
ImagePullBackOff every night, and with the ping inside the container the check goes
red the first morning.
No sidecar, no hook, no second CronJob
A sidecar container that pings, a postStart hook, or a separate CronJob whose only
job is to ping all report that something was scheduled, not that the work ran. A sidecar pings
while the main container is still crash-looping; a postStart hook runs as soon as the
container starts, before the work; a ping-only CronJob runs whether the backup did or not. Each
of them keeps the check green through the failure it exists to catch.
A failed ping does not run the job again
The ping is wrapped in { … || true; }, so the container's exit status is the work's
own. Without it, a ping that could not reach Marsolen would make the container fail, and
restartPolicy: OnFailure would run the whole backup a second time. A ping that fails
is still noticed: its error is in the pod's log, and the check goes red when the ping is late.
When the work fails, && stops before the group and the container exits with
the work's own code.
restartPolicy and backoffLimit
With restartPolicy: OnFailure, a container whose work fails is started again in the
same pod, up to backoffLimit times (2 here; Kubernetes' default is 6), waiting 10s,
20s, 40s and so on in between. A retry that finally succeeds pings once, late, which is what you
want: the run happened. So give the check a grace time longer than the job's run time plus those
retries, or a run that succeeded on its second try is reported as missed. When every retry
fails, nothing pings and the check goes red.
timeZone and the check's time zone
Without timeZone, the schedule is read in the time zone of the cluster's controller
manager, which is usually UTC and often not what you meant. Write it out (Kubernetes 1.27 and
later), and give the check the same time zone, or it expects the run hours before or after it
starts.
concurrencyPolicy: Forbid
A run that is still going when the next one is due is not doubled up: the next one is skipped. A skipped run sends no ping, so a backup that has started taking longer than a day shows up as a late check rather than as two backups fighting over the same files.
An image without curl
Most job images are built on a small base, and many have no curl. Alpine and other
BusyBox images have wget instead; replace the ping line with this one:
{ wget -q -T 10 -O /dev/null https://staging.marsolen.com/ping/YOUR-PING-UUID || true; }
BusyBox's wget fails on a 4xx or 5xx like curl -f does, and
-T 10 caps the wait at 10 seconds, but it has no retry option, so a network error
costs that run its ping. Debian -slim images have neither tool; add
curl to the image.
A distroless or scratch image has no shell at all, so there is nothing to chain
&& in: the command is the program itself. Either build the job image on a base
that has a shell, or have the program ping when it finishes. On Spring Boot,
the starter does that from the @Scheduled
method.
Keeping the URL out of the manifest
The ping URL needs no other credential, so anyone who reads the manifest can ping the check. If the manifest is in a repository others can read, put the URL in a Secret:
kubectl create secret generic marsolen-ping \
--from-literal=url=https://staging.marsolen.com/ping/YOUR-PING-UUID
and read it into the container, keeping the ping in the same place in the command:
- name: backup
image: registry.example.com/backup:1.4.2
env:
- name: MARSOLEN_PING_URL
valueFrom:
secretKeyRef:
name: marsolen-ping
key: url
command:
- /bin/sh
- -c
- |
/app/backup.sh &&
{ curl -fsS -m 10 --retry 3 -o /dev/null "$MARSOLEN_PING_URL" || true; }
Being told about a failure at once
With the manifest above, a failed run is noticed when its ping is late: after the check's grace
time. To be alerted on the next sweep instead, send the exit code: the ping URL ending in
/0 is a success and any other number is a failure. The container still exits with the
work's own code, so OnFailure retries it as before.
- |
/app/backup.sh; code=$?
curl -fsS -m 10 --retry 3 -o /dev/null https://staging.marsolen.com/ping/YOUR-PING-UUID/$code || true
exit $code
Each failed attempt reports its code, so a run that fails once and then succeeds on a retry raises an alert and then its recovery. A pod that never starts still sends nothing, and is still caught by the schedule.
Other ways in
For a job on a server, see cron. For a scheduled workflow, see GitHub Actions. To create checks from a script or CI, see the API.