DevOps.dev

Devops.dev is a community of DevOps enthusiasts sharing insight, stories, and the latest development in the field.

Follow publication

Deployments, DaemonSets, and StatefulSets in Kubernetes: Choosing the Right Resource for Your Application

Introduction

Kubernetes is one of the most popular container orchestration platforms used by developers worldwide. It provides a variety of features to manage, deploy, and scale containerized applications efficiently. Deployments, DaemonSets, and StatefulSets are three of the most widely used Kubernetes resources for application deployment. In this blog, we will explore these resources and determine which one is suitable for your application.

Deployments

Deployments are used to manage replica sets and ensure that the desired number of replicas is always running. They provide a way to perform rolling updates and rollbacks of your application without downtime. Deployments are suitable for stateless applications where data is not stored on the local disk of a pod.

Let’s look at an example of a deployment YAML file.

apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
labels:
app: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:latest
ports:
- containerPort: 80

In this example, we have defined a deployment named “myapp” with three replicas. The deployment uses a selector to match labels defined in the pod template. The pod template specifies the container image and port to be used for the application.

To create the deployment, run the following command:

$ kubectl apply -f deployment.yaml

To update the deployment, modify the YAML file and run the same command again.

DaemonSets

DaemonSets are used to ensure that all nodes in a Kubernetes cluster run a copy of a particular pod. They are useful for running system daemons or log collectors that need to run on every node. DaemonSets are also useful for running applications that require low latency, such as monitoring agents or load balancers.

Let’s look at an example of a DaemonSet YAML file.

apiVersion: apps/v1
kind: DaemonSet
metadata:
name: myapp
labels:
app: myapp
spec:
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:latest
ports:
- containerPort: 80

In this example, we have defined a DaemonSet named “myapp”. The DaemonSet uses a selector to match labels defined in the pod template. The pod template specifies the container image and port to be used for the application.

To create the DaemonSet, run the following command:

$ kubectl apply -f daemonset.yaml

To update the DaemonSet, modify the YAML file and run the same command again.

StatefulSets

StatefulSets are used to manage stateful applications that require stable network identities and persistent storage. They provide guarantees about the ordering and uniqueness of pod startup and shutdown. StatefulSets are useful for applications such as databases, message queues, and storage systems.

Let’s look at an example of a StatefulSet YAML file.

apiVersion: apps/v1
kind: StatefulSet
metadata:
name: myapp
labels:
app: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
serviceName: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:latest
ports:
- containerPort: 80
volumeMounts:
- name: data
mountPath: /data
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi

In this example, we have defined a StatefulSet named “myapp” with three replicas. The StatefulSet uses a selector to match labels defined in the pod template. The pod template specifies the container image, port to be used for the application, and volume mount to store data. Additionally, we have defined a volumeClaimTemplate to allocate storage for the pod.

To create the StatefulSet, run the following command:

kubectl apply -f statefulset.yaml

To update the StatefulSet, modify the YAML file and run the same command again.

Which One to Use for Your Application?

Deployments are suitable for stateless applications where data is not stored on the local disk of a pod. They provide a way to perform rolling updates and rollbacks of your application without downtime. DaemonSets are useful for running system daemons or log collectors that need to run on every node. They are also useful for running applications that require low latency, such as monitoring agents or load balancers. StatefulSets are suitable for stateful applications that require stable network identities and persistent storage. They provide guarantees about the ordering and uniqueness of pod startup and shutdown.

Conclusion

In this blog, we explored Deployments, DaemonSets, and StatefulSets and their use cases. Deployments are suitable for stateless applications, DaemonSets are useful for system daemons or log collectors, and StatefulSets are suitable for stateful applications. By understanding the differences between these resources, you can choose the right one for your application and ensure that it runs efficiently on Kubernetes.

If you enjoyed this post, please share it with your friends and colleagues who might find it useful. And don’t forget to follow us for more Kubernetes tips, tricks, and tutorials!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in DevOps.dev

Devops.dev is a community of DevOps enthusiasts sharing insight, stories, and the latest development in the field.

Written by Praveen Dandu

🚀 DevOps Engineer | Automating Infrastructure, Streamlining Deployments | Continuous Integration & Delivery Specialist https://www.linkedin.com/in/pravin24/

No responses yet

Write a response