Table of Contents
- What Are DaemonSets?
- Key Components of DaemonSets
- DaemonSets: Use Cases
- How DaemonSets Work
- DaemonSet Configuration Example
- Best Practices for DaemonSets
- What Are Jobs?
- Types of Jobs
- Key Components of Jobs
- Jobs: Use Cases
- How Jobs Work
- Job Configuration Examples
- Best Practices for Jobs
- DaemonSets vs. Jobs: Key Differences
- Conclusion
- References
What Are DaemonSets?
A DaemonSet is a Kubernetes workload resource that ensures a copy of a pod runs on all nodes in the cluster (or a subset of nodes, if filtered). Unlike Deployments or StatefulSets— which scale based on replicas—DaemonSets scale with the cluster: when a new node joins, a DaemonSet pod is automatically scheduled on it; when a node is removed, the pod is terminated.
DaemonSets are designed for node-centric tasks—services that need to run locally on each node to function.
Key Components of DaemonSets
A DaemonSet manifest (daemonset.yaml) includes the following core fields:
| Component | Purpose |
|---|---|
apiVersion | Specifies the Kubernetes API version (e.g., apps/v1). |
kind | Identifies the resource type as DaemonSet. |
metadata | Metadata like name and namespace for the DaemonSet. |
spec.selector | Labels to match pods controlled by the DaemonSet (must match template.metadata.labels). |
spec.template | The pod template used to create DaemonSet pods (includes containers, volumes, etc.). |
spec.updateStrategy | Defines how pods are updated (e.g., RollingUpdate or OnDelete). |
spec.minReadySeconds | Minimum seconds a pod must be ready before the next update starts. |
Node Selection
By default, DaemonSets run on all nodes. To target specific nodes, use:
nodeSelector: Matches nodes with specific labels (e.g.,disk: ssd).affinity/anti-affinity: More advanced rules (e.g., “avoid nodes with labelenv: prod”).tolerations: Allows pods to run on nodes with taints (e.g., control plane nodes).
DaemonSets: Use Cases
DaemonSets excel at scenarios where a service must run locally on nodes. Common use cases include:
- Logging/Monitoring: Tools like Fluentd (log collection) or Prometheus Node Exporter (metrics) run on every node to aggregate data.
- Network Plugins: CNI plugins (e.g., Calico, Flannel) use DaemonSets to manage node networking.
- Storage Daemons: Local storage drivers (e.g.,
local-volume-provisioner) run on nodes to manage persistent storage. - Security Agents: Antivirus or compliance tools (e.g., Falco) monitor node-level activity.
How DaemonSets Work
The DaemonSet controller (a built-in Kubernetes controller) manages DaemonSet pods. Its workflow:
- Desired State: The DaemonSet manifest defines the desired pod template and node selection rules.
- Current State: The controller watches nodes, pods, and the DaemonSet itself to track the current state.
- Reconciliation: If a node lacks a DaemonSet pod (or has an outdated one), the controller creates/updates the pod to match the desired state.
Key behaviors:
- Node Additions: When a new node joins the cluster, the controller immediately schedules a DaemonSet pod on it.
- Node Removals: When a node is deleted, the controller terminates the associated DaemonSet pod.
- Updates: With
RollingUpdate, old pods are replaced with new ones one node at a time (default). WithOnDelete, pods are only updated when manually deleted.
DaemonSet Configuration Example
Here’s a DaemonSet for Prometheus Node Exporter (a metrics collector):
# daemonset-node-exporter.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: node-exporter
namespace: monitoring
spec:
selector:
matchLabels:
app: node-exporter
template:
metadata:
labels:
app: node-exporter
spec:
containers:
- name: node-exporter
image: prom/node-exporter:v1.5.0
ports:
- containerPort: 9100
name: metrics
volumeMounts:
- name: proc
mountPath: /host/proc
readOnly: true
- name: sys
mountPath: /host/sys
readOnly: true
volumes:
- name: proc
hostPath:
path: /proc
- name: sys
hostPath:
path: /sys
tolerations: # Allow running on control plane nodes
- key: node-role.kubernetes.io/control-plane
effect: NoSchedule
Explanation:
selectorandtemplate.metadata.labelsensure the DaemonSet manages pods with labelapp: node-exporter.volumesmount host directories (/proc,/sys) into the pod, enabling Node Exporter to collect node metrics.tolerationsallow the pod to run on control plane nodes (which are typically tainted to prevent non-essential workloads).
Best Practices for DaemonSets
-
Limit Resources: DaemonSets run on every node—set
resources.requestsandresources.limitsto prevent resource starvation.resources: requests: cpu: 100m memory: 128Mi limits: cpu: 250m memory: 256Mi -
Use Node Affinity Sparingly: Prefer
nodeSelectorfor simple node targeting; useaffinityonly for complex rules (e.g., “run on nodes with GPU”). -
Tolerate Taints Judiciously: Only add tolerations for nodes that require the DaemonSet (e.g., control plane nodes for network plugins).
-
Monitor DaemonSets: Use tools like Prometheus to track pod status, restart counts, and resource usage across nodes.
-
Test Updates: Use
RollingUpdatewithmaxUnavailable: 1(default) to minimize downtime during upgrades.
What Are Jobs?
A Job is a Kubernetes workload resource that manages pods to run a task to completion. Unlike DaemonSets (which run indefinitely), Jobs ensure pods execute a task (e.g., a script, batch job) and exit successfully. Once all pods in a Job complete, the Job itself is marked as Completed.
Jobs are ideal for one-off or recurring batch tasks that don’t need to run continuously.
Types of Jobs
Kubernetes Jobs have three primary flavors:
1. One-Off Jobs
Runs a single pod to completion. If the pod fails, the Job restarts it until it succeeds (or hits a retry limit).
2. Parallel Jobs
Runs multiple pods in parallel to complete a task. Two sub-types:
- Indexed Jobs: Each pod processes a distinct subset of data (e.g., “pod 1 processes IDs 1-100, pod 2 processes 101-200”).
- Non-Indexed Jobs: All pods process the same task (e.g., rendering a video in parallel).
3. CronJobs
A time-based Job that runs on a schedule (e.g., “daily at 2 AM”). CronJobs create Jobs at specified intervals using cron syntax (e.g., 0 2 * * *).
Key Components of Jobs
A Job manifest (job.yaml) includes these core fields:
| Component | Purpose |
|---|---|
apiVersion | Kubernetes API version (e.g., batch/v1). |
kind | Resource type (Job or CronJob). |
metadata | Name, namespace, etc. |
spec.template | Pod template for the Job (includes containers, restartPolicy, etc.). |
spec.completions | Number of successful pod completions required (default: 1). |
spec.parallelism | Number of pods to run in parallel (default: 1). |
spec.backoffLimit | Max retries if pods fail (default: 6; retries delay exponentially). |
spec.activeDeadlineSeconds | Max time the Job can run (fails if exceeded). |
spec.ttlSecondsAfterFinished | Time to retain the Job after completion (auto-deletes afterward). |
For CronJobs, additional fields include:
spec.schedule: Cron syntax for recurrence (e.g.,0 3 * * *for 3 AM daily).spec.concurrencyPolicy: How to handle overlapping Jobs (Allow,Forbid, orReplace).
Jobs: Use Cases
Jobs shine for tasks that need to finish and exit. Common use cases:
- Batch Processing: Analyzing logs, generating reports, or transforming data (e.g., CSV to Parquet).
- Backups: Running daily database backups (via CronJob).
- Database Migrations: Executing schema updates during deployments.
- One-Off Scripts: Cleaning up old files, syncing data between services, or testing APIs.
How Jobs Work
The Job controller manages Job lifecycle:
- Pod Creation: The controller creates pods based on
spec.templateuntilspec.completionsare met. - Failure Handling: If a pod fails (e.g., exits with non-zero code), the controller restarts it (up to
backoffLimitretries). - Completion Check: Once
completionssuccessful pods exist, the Job is markedCompleted. - Cleanup: If
ttlSecondsAfterFinishedis set, the Job is auto-deleted after the TTL expires.
For CronJobs:
- The CronJob controller checks the schedule at fixed intervals (every 10 seconds by default).
- When the schedule triggers, it creates a Job, which in turn creates pods.
Job Configuration Examples
Example 1: One-Off Job (Batch Processing)
A Job to process a CSV file and exit:
# job-batch-processing.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: csv-processor
spec:
template:
spec:
containers:
- name: processor
image: my-csv-processor:1.0
command: ["/app/process.sh", "/data/input.csv"]
volumeMounts:
- name: data-volume
mountPath: /data
volumes:
- name: data-volume
persistentVolumeClaim:
claimName: input-data-pvc
restartPolicy: Never # Jobs typically use Never or OnFailure
backoffLimit: 3 # Retry up to 3 times on failure
activeDeadlineSeconds: 3600 # Fail if not done in 1 hour
Example 2: Parallel Job
A Job with 4 completions and 2 parallel pods (e.g., processing 4 data chunks):
# job-parallel.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: parallel-data-processor
spec:
completions: 4 # 4 total successful pods
parallelism: 2 # Run 2 pods at a time
template:
spec:
containers:
- name: processor
image: my-parallel-processor:1.0
env:
- name: CHUNK_ID
valueFrom:
fieldRef:
fieldPath: metadata.annotations['batch.kubernetes.io/job-completion-index'] # Unique chunk ID
restartPolicy: Never
backoffLimit: 2
Example 3: CronJob (Daily Backup)
A CronJob to run a daily database backup at 2 AM:
# cronjob-backup.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: db-backup
spec:
schedule: "0 2 * * *" # Daily at 2 AM
concurrencyPolicy: Forbid # Don't run if previous backup is still running
jobTemplate:
spec:
template:
spec:
containers:
- name: backup
image: my-db-backup:1.0
command: ["/backup.sh", "--db=prod"]
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-secrets
key: password
restartPolicy: OnFailure
backoffLimit: 1
ttlSecondsAfterFinished: 86400 # Keep Job for 1 day
Best Practices for Jobs
-
Set
restartPolicy: NeverorOnFailure: Jobs are not meant to restart indefinitely. UseNeverfor one-off tasks andOnFailureto retry failed pods. -
Limit Runtime with
activeDeadlineSeconds: Prevent stuck Jobs from running forever (e.g.,3600seconds = 1 hour). -
Clean Up Old Jobs: Use
ttlSecondsAfterFinishedto auto-delete completed Jobs (e.g.,604800= 7 days). -
Avoid Over-Parallelism: Too many parallel pods can strain the cluster—test with
parallelism: 1first, then scale up. -
Secure Sensitive Data: Use
secrets(not plain text) for credentials in CronJobs/Jobs.
DaemonSets vs. Jobs: Key Differences
| Feature | DaemonSets | Jobs |
|---|---|---|
| Purpose | Run a pod on all/specific nodes (continuous). | Run pods to completion (batch/one-off tasks). |
| Lifecycle | Indefinite (runs as long as the node exists). | Finite (stops when completions are met). |
| Scheduling | Tied to nodes (new nodes get pods automatically). | Scheduled on any available node. |
| Scaling | Scales with cluster size (1 pod per node). | Scales via completions and parallelism. |
| Use Cases | Logging, monitoring, network plugins. | Backups, batch processing, migrations. |
Conclusion
DaemonSets and Jobs are indispensable Kubernetes workload resources, each solving unique orchestration challenges.
- DaemonSets ensure node-level services (e.g., logging, networking) run consistently across the cluster, scaling with node additions/removals.
- Jobs (and CronJobs) manage finite tasks, from one-off scripts to recurring backups, ensuring tasks complete reliably.
By mastering these resources, you can build resilient, efficient Kubernetes clusters that handle both continuous services and batch workloads with ease.