Kubernetes Deployments, Stateful Sets, Daemon Sets, Jobs and Cronjobs. Deploy each type of workload in Kubernetes cluster
Introduction :
Kubernetes is a powerful container orchestration platform that provides a wide range of workload types for managing containerized applications at scale. In this blog post, we will cover the key aspects of Kubernetes workloads, including Deployments, StatefulSets, DaemonSets, Jobs, and CronJobs, and provide code examples to illustrate how to deploy each type of workload in a Kubernetes cluster.
Deployments :
Deployments are the most commonly used workload type in Kubernetes. Deployments provide a declarative way to manage a set of identical pods, ensuring that the desired number of replicas is always available, and handling rolling updates and rollbacks when necessary.
Code Example for deploying a Deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: my-image:latest
ports:
- containerPort: 8080
This code creates a Deployment called my-deployment
that deploys three replicas of a pod with the label app=my-app
. The pod contains a container running the my-image: latest
image, exposing port 8080
.
StatefulSets :
StatefulSets are used to manage stateful applications, such as databases, that require stable network identities and stable storage. StatefulSets ensure that each pod in the set is created and managed in a specific order, with each pod having a unique hostname and persistent storage.
Code Example for deploying a StatefulSet:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: my-statefulset
spec:
replicas: 3
selector:
matchLabels:
app: my-app
serviceName: my-service
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: my-image:latest
ports:
- containerPort: 8080
volumeMounts:
- name: my-volume
mountPath: /data
volumeClaimTemplates:
- metadata:
name: my-volume
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
This code creates a StatefulSet called my-stateful set
that deploys three replicas of a pod with the label app=my-app
. The pod contains a container running the my-image: latest
image, exposing port 8080
. The pod also has a persistent volume mounted at /data
, with a volume claim template requesting 1GB of storage.
DaemonSets :
DaemonSets are used to manage background tasks that need to run on every node in the cluster. DaemonSets ensure that a pod is running on each node in the cluster that matches a specific label selector, providing a way to manage tasks such as log collection or monitoring.
Code Example for deploying a DaemonSet:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: my-daemonset
spec:
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: my-image:latest
command: [ "my-command" ]
This code creates a DaemonSet called my-daemonset
that runs a pod with the label app=my-app
on every node in the cluster.
Kubernetes Jobs :
Kubernetes Jobs is a workload type that creates one or more pods to perform a specific task and then terminates them when the task is completed. Jobs are useful for running batch jobs, such as data processing or backups, and ensuring that the task completes successfully.
Code Example for creating a Job:
apiVersion: batch/v1
kind: Job
metadata:
name: my-job
spec:
template:
spec:
containers:
- name: my-container
image: my-image
command: ["my-command"]
restartPolicy: Never
backoffLimit: 5
This code creates a Job called my-job
that runs a container with the image my-image
and the command my-command
. The restart policy
is set to Never
, which means the pod will not be restarted if it fails. The backoffLimit
is set to 5
, which means the Job will be retried up to 5 times if it fails.
Kubernetes CronJobs :
Kubernetes CronJobs is a workload type that creates Jobs on a scheduled basis. CronJobs are useful for running periodic tasks, such as backups or data cleanup, and ensuring that they are run at a specific time or interval.
Code Example for creating a CronJob:
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: my-cronjob
spec:
schedule: "*/5 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: my-container
image: my-image
command: ["my-command"]
restartPolicy: Never
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1
This code creates a CronJob called my-cronjob
that runs a Job every 5 minutes with a container running the image my-image
and the command my-command
. The restartPolicy
is set to Never
, and the successfulJobsHistoryLimit
is set to 3
, which means up to 3 successful Jobs will be retained in history. The failedJobsHistoryLimit
is set to 1
, which means only the most recent failed Job will be retained in history.
Conclusion :
Kubernetes Jobs and CronJobs are powerful workload types for managing batch and periodic tasks in a Kubernetes cluster. Understanding how to deploy and manage Jobs and CronJobs is essential for deploying and maintaining containerized applications at scale. By utilizing the code examples provided in this blog post, developers and IT teams can leverage the features of Kubernetes to build a highly scalable and resilient infrastructure for their containerized applications.