Helm, Jobs, CronJobs & DaemonSets
Deployments are for apps that run forever. Real clusters also run one-off tasks, tasks on a timetable, and agents that must sit on every node, and most third-party software (monitoring, ingress controllers, databases) is installed with Helm rather than hand-written YAML. This chapter covers all four.
Jobs: run to completion
A Job runs a pod until it finishes successfully, retrying if it fails. Database migrations, a one-time data import or a report are typical Jobs.
apiVersion: batch/v1
kind: Job
metadata:
name: migrate-db
spec:
backoffLimit: 3 # retry a failed pod at most 3 times
activeDeadlineSeconds: 600 # give up after 10 minutes in total
ttlSecondsAfterFinished: 3600 # delete the finished Job an hour later
template:
spec:
restartPolicy: Never # Jobs need Never or OnFailure
containers:
- name: migrate
image: ghcr.io/you/myapp:1.4.0
command: ["python", "manage.py", "migrate"]
$ kubectl apply -f job.yaml
$ kubectl get jobs
NAME STATUS COMPLETIONS DURATION AGE
migrate-db Complete 1/1 12s 40s
$ kubectl logs job/migrate-db
CronJobs: Jobs on a timetable
A CronJob creates a Job on a schedule, written in the same five-field format as Linux cron (Linux chapter 9).
apiVersion: batch/v1
kind: CronJob
metadata:
name: nightly-backup
spec:
schedule: "0 2 * * *" # 02:00 every day
timeZone: "Europe/London" # without it, the schedule uses the cluster's time zone (usually UTC)
concurrencyPolicy: Forbid # don't start a new run while the last one is still going
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 3
jobTemplate:
spec:
backoffLimit: 2
template:
spec:
restartPolicy: OnFailure
containers:
- name: backup
image: ghcr.io/you/backup:1.0
args: ["--target", "s3://my-backups"]
Waiting until 2 a.m. to see whether it works is no fun, so trigger a run by hand:
$ kubectl create job backup-test --from=cronjob/nightly-backup
$ kubectl get cronjob nightly-backup # LAST SCHEDULE shows when it last fired
Set suspend: true in the spec to pause a CronJob without deleting it.
DaemonSets: one pod on every node
A DaemonSet runs exactly one copy of a pod on every node (or every node matching a selector), and adds one automatically when a node joins. It's how clusters run node-level agents: log collectors like Fluent Bit, the Prometheus node exporter, and networking plugins.
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: node-exporter
namespace: monitoring
spec:
selector:
matchLabels: {app: node-exporter}
template:
metadata:
labels: {app: node-exporter}
spec:
tolerations: # also run on control-plane nodes
- key: node-role.kubernetes.io/control-plane
effect: NoSchedule
containers:
- name: node-exporter
image: prom/node-exporter:v1.8.2
ports:
- containerPort: 9100
Which workload for which job
- Deployment: stateless apps that run forever and can be copied freely (web servers, APIs).
- StatefulSet: apps where each copy needs its own identity and storage (databases, queues).
- DaemonSet: one per node (agents and collectors).
- Job: run once until done.
- CronJob: run on a schedule.
Helm: a package manager for Kubernetes
Installing something like a full monitoring stack by hand means dozens of YAML files. Helm packages them as a chart: templates plus a values.yaml file of settings you can override. Installing a chart creates a release, which Helm can upgrade, list and roll back as one unit.
# install Helm (on a Mac: brew install helm)
$ curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
$ helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
$ helm repo update
$ helm search repo prometheus-community
$ helm show values prometheus-community/kube-prometheus-stack > defaults.yaml # every setting you can change
$ helm install monitoring prometheus-community/kube-prometheus-stack \
--namespace monitoring --create-namespace -f my-values.yaml
$ helm list -A # releases in every namespace
$ helm upgrade monitoring prometheus-community/kube-prometheus-stack -n monitoring -f my-values.yaml
$ helm history monitoring -n monitoring
$ helm rollback monitoring 1 -n monitoring
$ helm uninstall monitoring -n monitoring
Values: changing a chart's settings
You never edit a chart's templates; you override its values, either in a file (best, because it goes in Git) or with --set for a one-off:
# my-values.yaml
grafana:
adminPassword: change-me
persistence:
enabled: true
alertmanager:
enabled: false
$ helm template monitoring prometheus-community/kube-prometheus-stack -f my-values.yaml | less # see the YAML without installing
$ helm upgrade --install myapp ./chart -n prod --set image.tag=1.4.0 # install or upgrade: safe to rerun
helm upgrade --install is what CI pipelines use: it works whether or not the release already exists.
Your own chart
$ helm create myapp # makes Chart.yaml, values.yaml and templates/
$ helm lint myapp
$ helm install myapp ./myapp -n shop
Inside templates/, values are filled in with Go templating, for example image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}". Start by changing values.yaml and reading what each template does with it.
Common problems
cannot re-use a name that is still in use: a release with that name exists. Usehelm upgrade --install, or pick another name.- A Job retries forever, or never: check
backoffLimitandrestartPolicy;kubectl describe jobshows each attempt. - A CronJob never fires: check
suspend, the time zone, and the schedule (crontab.guru explains any expression). If a run is missed by more thanstartingDeadlineSeconds, it's skipped. - A DaemonSet has fewer pods than nodes: some nodes have taints the pods don't tolerate, or a
nodeSelectorexcludes them.