Introduction #
In Kubernetes, workload controllers orchestrate and automate the life cycle of containerized applications. They handle scaling, redundancy and resource management for applications, enabling a scalable, fault-tolerant architecture.
Deployment #
Controller for stateless applications.
Overview #
A Deployment deploys and manages stateless applications. It maintains the desired number of running Pods, handles updates and rollbacks, and offers horizontal scaling options.Use cases #
Used for web applications, APIs or any stateless service that can easily be replicated without depending on persistent storage.
Example #
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-deployment
spec:
replicas: 3
selector:
matchLabels:
app: example
template:
metadata:
labels:
app: example
spec:
containers:
- name: example
image: nginxReplicaSet #
Maintains the number of Pod replicas.
Overview #
A ReplicaSet maintains a constant number of identical Pods in the cluster. Deployments often rely on ReplicaSets to create and update Pods.Use cases #
Used internally by Deployments to keep a stable number of Pods running.
StatefulSet #
For applications that need persistent storage.
Overview #
A StatefulSet is used for applications that need data persistence. Each Pod has a unique identity, preserved when the Pod is recreated, and is associated with a dedicated storage volume.Use cases #
Databases, distributed file systems, or any application that needs a unique identifier or persistent state.
Example #
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: example-statefulset
spec:
serviceName: "example"
replicas: 3
selector:
matchLabels:
app: example
template:
metadata:
labels:
app: example
spec:
containers:
- name: example
image: mysqlDaemonSet #
Ensures a Pod runs on every node.
Overview #
A DaemonSet ensures that a Pod runs on every node in the cluster. It is used for monitoring, log analysis or network maintenance tasks.Use cases #
Monitoring agents, log collection, and any service that needs to be present on every node.
Example #
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: example-daemonset
spec:
selector:
matchLabels:
name: example
template:
metadata:
labels:
name: example
spec:
containers:
- name: example
image: fluentdJob #
Running one-off tasks.
Overview #
A Job runs Pods to complete a one-off task and stops when the task is finished. It is especially suited to data processing or maintenance scripts.Use cases #
One-off tasks such as data analysis or batch processing.
Example #
apiVersion: batch/v1
kind: Job
metadata:
name: example-job
spec:
template:
spec:
containers:
- name: example
image: busybox
command: ["echo", "Hello, Kubernetes!"]
restartPolicy: NeverCronJob #
Scheduling recurring tasks.
Overview #
A CronJob schedules recurring tasks using crontab syntax. It is useful for backups, report generation or any periodic task.Use cases #
Recurring tasks such as backups or data cleanup.
Example #
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: example-cronjob
spec:
schedule: "*/5 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: example
image: busybox
command: ["echo", "Hello from CronJob"]
restartPolicy: OnFailureChoosing a workload controller #
- For stateless applications: use a Deployment.
- For stateful applications that need persistent identifiers: prefer a StatefulSet.
- For tasks that must run on every node: choose a DaemonSet.
- For one-off tasks: a Job is recommended.
- For recurring tasks: a CronJob is ideal.