Mastering Kubernetes Controllers: DaemonSet, Job, and CronJob Hands‑On Guide
This tutorial explains why Kubernetes controllers are essential for pod reliability, details the concepts and specifications of DaemonSet, Job, and CronJob, provides step‑by‑step YAML examples and kubectl commands, and demonstrates rolling updates and scheduled tasks in a cloud‑native environment.
Why Controllers Matter
Kubernetes pods created directly are vulnerable: if a pod is deleted it does not self‑recover. Controllers continuously monitor pod health and enforce the desired state, automatically restarting failed pods, maintaining replica counts, and handling node changes.
DaemonSet Overview
A DaemonSet ensures that a copy of a pod runs on every node in the cluster. When a new node joins, the DaemonSet creates a pod on it; when a node leaves, the associated pod is removed. Unlike Deployments, each node runs at most one DaemonSet pod.
DaemonSet Specification
Key fields include apiVersion: apps/v1, kind: DaemonSet, metadata, and spec with selector and template. The template defines pod metadata, tolerations, containers, resource limits, volume mounts, and termination grace period.
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd-elasticsearch
namespace: kube-system
labels:
k8s-app: fluentd-logging
spec:
selector:
matchLabels:
name: fluentd-elasticsearch
template:
metadata:
labels:
name: fluentd-elasticsearch
spec:
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
containers:
- name: fluentd-elasticsearch
image: xianchao/fluentd:v2.5.1
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
volumeMounts:
- name: varlog
mountPath: /var/log
- name: varlibdockercontainers
mountPath: /var/lib/docker/containers
readOnly: true
terminationGracePeriodSeconds: 30
volumes:
- name: varlog
hostPath:
path: /var/log
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containersApply the manifest with kubectl apply -f daemonset.yaml and verify creation using kubectl get ds -n kube-system and kubectl get pods -n kube-system -o wide.
DaemonSet Rolling Update
The updateStrategy field controls how pods are updated. Supported types are RollingUpdate and OnDelete. For rolling updates, you can set maxUnavailable to control the number of pods removed before new ones are created. Example command to update the image:
kubectl set image daemonsets fluentd-elasticsearch=ikubernetes/filebeat:5.6.6-alpine -n kube-systemJob Controller
A Job runs a pod to completion for one‑time tasks such as database backups. Important spec fields include completions, parallelism, activeDeadlineSeconds, backoffLimit, and ttlSecondsAfterFinished. Restart policy must be Never or OnFailure.
apiVersion: batch/v1
kind: Job
metadata:
name: my-busybox-job
spec:
ttlSecondsAfterFinished: 20
activeDeadlineSeconds: 50
completions: 6
parallelism: 3
backoffLimit: 6
template:
spec:
restartPolicy: Never
containers:
- name: my-container-job
image: busybox
imagePullPolicy: IfNotPresent
command: ['sh', '-c']
args: ['echo "Welcome to xianchao"; sleep 60; echo "Next to Meet you"']Deploy with kubectl apply -f job.yaml, monitor with kubectl get job, and view logs using kubectl logs <pod-name>.
CronJob Controller
CronJob adds scheduling to Jobs, allowing periodic execution. The schedule field follows standard cron syntax. The jobTemplate contains the Job spec.
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailureApply with kubectl apply -f cronjob.yaml, then watch scheduling with kubectl get cronjob and kubectl get jobs --watch. Logs of each run can be retrieved via kubectl logs <job-pod>.
Key Takeaways
Controllers automate pod lifecycle management, improving reliability.
DaemonSet is ideal for node‑level services such as log collectors or storage agents.
Jobs handle batch or one‑off tasks; CronJobs add time‑based automation.
Understanding spec fields and update strategies enables safe rolling updates and resource cleanup.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Full-Stack DevOps & Kubernetes
Focused on sharing DevOps, Kubernetes, Linux, Docker, Istio, microservices, Spring Cloud, Python, Go, databases, Nginx, Tomcat, cloud computing, and related technologies.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
