You know the scenario, you’re wanting to run jobs either at certain points in a day or throughout the data every N timespans (i.e. every 5 mins).
Kubernetes has you covered, there’s a specific “kind” of job for this, as you guessed from the title, the CronJob.
An example app.
Let’s assume you created yourself a job – I’m going to create a simple job that just outputs the date/time at the scheduled time. I’ve written this in Rust but to be honest it’s simple enough that this could be any language. Here’s the Cargo.toml
The application is just a standard console application named crj (for cronjob or cron rust job, I really didn’t think about it :)).
[package]
name = "crj"
version = "0.1.0"
edition = "2024"
[dependencies]
chrono = "0.4"
Here’s the code
use chrono::Local;
fn main() {
let now = Local::now();
println!("Current date and time: {}", now);
}
See I told you it was simple.
Docker
For completeness, here’s the Dockerfile and the steps to get things built, tagged and pushed
FROM rust:1.89.0-slim AS builder
WORKDIR /app
COPY . .
RUN cargo build --release
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y ca-certificates && \
rm -rf /var/lib/apt/lists/*
COPY --from=builder /app/target/release /usr/local/bin/crj
RUN chmod +x /usr/local/bin/crj
ENTRYPOINT ["/usr/local/bin/crj/crj"]
Next up we need to build the image using (remember to use the image you created as well as the correct name for your container registry)
docker build -t putridparrot/crj:1.0.0 .
then tag it using
docker tag putridparrot/crj:1.0.0 putridparrotreg/putridparrot/crj:1.0.0
Finally we’ll push it to our container registry using
docker push putridparrotreg/putridparrot/crj:1.0.0
Kubernetes CronJob
All pretty standard stuff and to be honest the next bit is simple enough. We need to create a kubernetes yaml file (or helm charts). Here’s my cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: scheduled-job
namespace: dev
spec:
schedule: "*/5 * * * *" # every 5 minutes
jobTemplate:
spec:
template:
spec:
containers:
- name: scheduled-job
image: putridparrotreg/putridparrot/crj:1.0.0
restartPolicy: Never
My cronjob has the name scheduled-job (I know, not very imaginative). We apply this file to Kubernetes as usual i.e.
kubectl apply -f .\cronjob.yaml
Did it work?
We’ll ofcourse want to take a look at what happened after this CronJob was set up in Kubernetes. We can simply use the following. You can set the namespace used, such as dev in my case.
kubectl get cronjobs --all-namespaces -w
you’ll see something like this
NAMESPACE NAME SCHEDULE TIMEZONE SUSPEND ACTIVE LAST SCHEDULE AGE
dev scheduled-job */5 * * * * <none> False 0 <none> 9s
dev scheduled-job */5 * * * * <none> False 1 0s 16s
dev scheduled-job */5 * * * * <none> False 0 13s 29s
dev scheduled-job */5 * * * * <none> False 1 0s 5m16s
In my case the job starts (ACTIVE) and then completes and shuts down. Then 5 minutes later it starts again as expected with this cron schedule.
On the pods side you can run
kubectl get pods -n dev -w
Now what you’ll see is something like this
NAME READY STATUS RESTARTS AGE
scheduled-job-29257380-5w4rg 0/1 Completed 0 51s
scheduled-job-29257385-qgml2 0/1 Pending 0 0s
scheduled-job-29257385-qgml2 0/1 Pending 0 0s
scheduled-job-29257385-qgml2 0/1 ContainerCreating 0 0s
scheduled-job-29257385-qgml2 1/1 Running 0 2s
scheduled-job-29257385-qgml2 0/1 Completed 0 3s
scheduled-job-29257385-qgml2 0/1 Completed 0 5s
scheduled-job-29257385-qgml2 0/1 Completed 0 5s
scheduled-job-29257390-2x98r 0/1 Pending 0 0s
scheduled-job-29257390-2x98r 0/1 Pending 0 0s
scheduled-job-29257390-2x98r 0/1 ContainerCreating 0 0s
scheduled-job-29257390-2x98r 1/1 Running 0 2s
Notice that the pod is created and goes into a “Pending” state. Then “ContainerCreating” before “Running” and finally “Completed”, but the next run of the cronjob creates a new pod name. Therefore, if you’re trying to log the pods i.e. kubectl logs scheduled-job-29257380-5w4rg -n dev – then you’ll get something like the below, but you cannot -f (follow) the logs as the next time the job runs it creates a new pod.
Current date and time: 2025-08-17 15:00:09.294317303 +00:00