StatefulSet vs Deployment vs DaemonSet

Saurabh AdhauSaurabh Adhau
4 min read

Introduction

Kubernetes, with its powerful orchestration capabilities, offers several types of workload controllers to manage containerized applications. Among the most commonly used are StatefulSet, Deployment, and DaemonSet. Each serves distinct purposes tailored to different application requirements and operational needs within a Kubernetes cluster. Understanding the differences between these workload types is essential for effectively deploying and managing applications in a Kubernetes environment.

Deployment

Purpose and Use Case:

  • Deployment is the most commonly used workload controller in Kubernetes. It is designed for stateless applications, where instances of the application can be easily replicated and scaled horizontally.

  • Scaling: Deployments facilitate easy scaling by managing replica sets. They ensure a specified number of pod replicas are running at any given time, handling updates and rollbacks seamlessly.

  • Updates and Rollbacks: Deployments support rolling updates, enabling new versions of applications to be gradually deployed without downtime. Rollbacks to previous versions are also supported, ensuring stability and resilience.

Key Features:

  • Declarative Updates: You can define desired state configurations (YAML or JSON manifests) for deployments, and Kubernetes handles the rest, ensuring the cluster matches the desired state.

  • Pod Management: Deployments manage pods via replica sets, ensuring availability and desired pod count.

  • Automatic Rollback: In case of deployment failures, automatic rollback to the previous stable version is supported, maintaining application availability.

Example Use Case:

Deploying web servers, APIs, microservices that can scale horizontally and don’t require persistent state.

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

StatefulSet

Purpose and Use Case:

  • StatefulSet is designed for stateful applications that require unique identities, stable storage, and ordered deployment and scaling.

  • Stateful Nature: Each pod in a StatefulSet maintains a unique identifier and persistent storage. This is crucial for applications like databases, key-value stores, and distributed systems requiring stable network identities.

  • Ordered Deployment: StatefulSets guarantee ordered deployment and scaling, ensuring each pod is initialized sequentially based on its ordinal index.

Key Features:

  • Persistent Storage: StatefulSets manages persistent volumes for pods, ensuring data persistence and stability across pod restarts or rescheduling.

  • Stable Network Identity: Each pod in a StatefulSet has a stable DNS name and network identity, simplifying communication and configuration for distributed applications.

  • Ordered Operations: StatefulSets sequentially manages pod deployment and scaling, maintaining application-specific ordering requirements.

Example Use Case:

Deploying databases (like MySQL, and PostgreSQL), messaging queues (like Kafka), or distributed storage systems requiring stable identities and persistent storage.

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  replicas: 3
  serviceName: mysql
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:5.7
          ports:
            - containerPort: 3306
          volumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
  volumeClaimTemplates:
    - metadata:
        name: mysql-persistent-storage
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: 10Gi

DaemonSet

Purpose and Use Case:

  • DaemonSet ensures that a copy of a pod runs on each node in a Kubernetes cluster. It is suitable for deploying system daemons, log collectors, monitoring agents, or any utility that needs to run on every node.

  • Node Affinity: DaemonSets allow you to specify node selectors or node affinities to control which nodes the daemon pods run on.

  • Cluster-wide Deployment: Unlike Deployments or StatefulSets, DaemonSets ensure that exactly one pod runs on each node, making them ideal for tasks requiring node-specific configuration or control.

Key Features:

  • Node-level Operations: DaemonSets manage pods on a per-node basis, ensuring that specific tasks or utilities are running on every node in the cluster.

  • Node Selector: You can define node selectors or node affinities to control on which nodes the DaemonSet pods should run.

  • Automatic Deployment: DaemonSets automatically deploy and manage pods on new nodes that are added to the Kubernetes cluster, ensuring consistent operation across the entire cluster.

Example Use Case:

Running logging agents (like Fluentd, and Elasticsearch), monitoring agents (like Prometheus Node Exporter), or networking proxies (like Cilium) on every node for cluster-wide observability and management.

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd
spec:
  selector:
    matchLabels:
      app: fluentd
  template:
    metadata:
      labels:
        app: fluentd
    spec:
      containers:
        - name: fluentd
          image: fluent/fluentd:v1.11.5-debian-1
          volumeMounts:
            - name: varlog
              mountPath: /var/log
      volumes:
        - name: varlog
          hostPath:
            path: /var/log

Choosing the Right Workload Controller

When deciding which workload controller to use in Kubernetes, consider the following guidelines:

  • Stateless vs Stateful: Choose Deployments for stateless applications that can scale horizontally and don’t require persistent storage. Opt for StatefulSets for stateful applications needing stable identities and persistent storage.

  • Node-specific vs Cluster-wide: Use DaemonSets for tasks that require execution on every node in the cluster, such as logging, monitoring, or networking agents.

By understanding the distinct roles and capabilities of StatefulSet, Deployment, and DaemonSet in Kubernetes, you can effectively choose and deploy the right workload controller to meet your application’s requirements for scalability, resilience, and operational efficiency within a Kubernetes environment.

10
Subscribe to my newsletter

Read articles from Saurabh Adhau directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Saurabh Adhau
Saurabh Adhau

As a DevOps Engineer, I thrive in the cloud and command a vast arsenal of tools and technologies: ☁️ AWS and Azure Cloud: Where the sky is the limit, I ensure applications soar. 🔨 DevOps Toolbelt: Git, GitHub, GitLab – I master them all for smooth development workflows. 🧱 Infrastructure as Code: Terraform and Ansible sculpt infrastructure like a masterpiece. 🐳 Containerization: With Docker, I package applications for effortless deployment. 🚀 Orchestration: Kubernetes conducts my application symphonies. 🌐 Web Servers: Nginx and Apache, my trusted gatekeepers of the web.