A Hands-On Guide to Kubernetes: Deployments, StatefulSets, and DaemonSets 🛠️

Ranjan YadavRanjan Yadav
5 min read

⇢ Understanding Kubernetes: Deployments, StatefulSets, and DaemonSets with Practical Examples

·

Kubernetes Deployments, DaemonSets, and StatefulSets

Kubernetes has revolutionized the way we deploy, manage, and scale containerized applications. As the go-to platform for container orchestration, it offers a variety of controllers to handle different application requirements. In this blog, we will delve into three fundamental Kubernetes controllers: Deployments, StatefulSets, and DaemonSets. We’ll explain what they are, when to use them, and provide practical examples to help you get started.

Animated Kubernetes Deployments, DaemonSets, and StatefulSets

1. Kubernetes Deployments

What is a Deployment?

A Deployment in Kubernetes is used to manage a set of identical pods. It ensures that the specified number of pod replicas are running at any time. If a pod fails, the Deployment automatically replaces it. Deployments are perfect for stateless applications.

When to Use Deployments

Use Deployments when you need to manage stateless applications where the state is not stored within the pod, but in an external storage or database. They are ideal for scenarios where you need high availability and scalability.

Use Cases for Deployments

  • Web Servers: Deployments are great for deploying web applications like Nginx or Apache HTTP server.

  • API Servers: Use Deployments for stateless RESTful APIs.

  • Microservices: Deployments work well for microservices architectures where each service can scale independently.

Practical Example: Deploying a Web Application

Let’s create a simple Nginx web server deployment.

  1. Create a Deployment YAML File
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

2. Apply the Deployment

kubectl apply -f nginx-deployment.yaml

3. Verify the Deployment

kubectl get deployments
kubectl get pods
kubectl describe deployment nginx-deployment

Output:

$ kubectl get po   
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-7c79c4bf97-bzrdg   1/1     Running   0          13s
nginx-deployment-7c79c4bf97-rr4zb   1/1     Running   0          13s
nginx-deployment-7c79c4bf97-zjm25   1/1     Running   0          13s

This Deployment will ensure that there are always 3 replicas of the Nginx web server running. If any pod fails, it will be automatically replaced by a new pod.

2. Kubernetes StatefulSets

What is a StatefulSet?

StatefulSets are used for applications that require stable, unique network identifiers and persistent storage. They are ideal for databases and other stateful applications that need a consistent identity and storage across restarts.

When to Use StatefulSets

Use StatefulSets when you need to manage stateful applications where each instance of the application requires a unique identifier and stable storage. StatefulSets are particularly useful for applications that require ordered deployment and scaling.

Use Cases for StatefulSets

  • Databases: StatefulSets are ideal for deploying databases like MySQL, PostgreSQL, and MongoDB.

  • Distributed Systems: Use StatefulSets for distributed systems like Apache Kafka and Zookeeper.

  • Caching Systems: Deploy Redis or Memcached clusters using StatefulSets.

Practical Example: Deploying a Stateful Application

Let’s deploy a StatefulSet for a simple MySQL database.

  1. Create a StatefulSet YAML File
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  serviceName: "mysql"
  replicas: 3
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "password"
        ports:
        - containerPort: 3306

2. Apply the StatefulSet

kubectl apply -f mysql-statefulset.yaml

3. Verify the StatefulSet

kubectl get statefulsets
kubectl get pods
kubectl describe statefulset mysql

Output:

$ kubectl get po
NAME      READY   STATUS    RESTARTS   AGE
mysql-0   1/1     Running   0          7s
mysql-1   1/1     Running   0          5s
mysql-2   1/1     Running   0          3s

The StatefulSet ensures each MySQL pod has a unique identifier and a persistent volume. The pods will be named mysql-0, mysql-1, and mysql-2, and each will retain its data across restarts.

3. Kubernetes DaemonSets

What is a DaemonSet?

A DaemonSet ensures that a copy of a pod runs on all (or some) nodes in the cluster. DaemonSets are perfect for running background tasks such as log collection, monitoring, and other node-specific services.

When to Use DaemonSets

Use DaemonSets when you need to run a service on all or selected nodes in the cluster. This is particularly useful for node-level services that need to be present on every node.

Use Cases for DaemonSets

  • Log Collection: Deploy log collectors like Fluentd or Logstash on all nodes to collect logs.

  • Monitoring: Use DaemonSets to deploy monitoring agents like Prometheus Node Exporter or Datadog Agent.

  • Security: Deploy security agents or intrusion detection systems on all nodes using DaemonSets.

Practical Example: Deploying a Monitoring Agent

Let’s deploy a DaemonSet for a Fluentd logging agent.

  1. Create a DaemonSet YAML File
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd
spec:
  selector:
    matchLabels:
      name: fluentd
  template:
    metadata:
      labels:
        name: fluentd
    spec:
      containers:
      - name: fluentd
        image: fluentd:latest
        env:
        - name: FLUENTD_ARGS
          value: "--no-supervisor -q"
        volumeMounts:
        - name: varlog
          mountPath: /var/log
        - name: varlibdockercontainers
          mountPath: /var/lib/docker/containers
          readOnly: true
      volumes:
      - name: varlog
        hostPath:
          path: /var/log
      - name: varlibdockercontainers
        hostPath:
          path: /var/lib/docker/containers

2. Apply the DaemonSet

kubectl apply -f fluentd-daemonset.yaml

3. Verify the DaemonSet

kubectl get daemonsets
kubectl get pods
kubectl describe daemonset fluentd

Output:

$ kubectl get po   
NAME            READY   STATUS    RESTARTS   AGE
fluentd-kw7hn   1/1     Running   0          18s

The DaemonSet will ensure that the Fluentd logging agent runs on all nodes, collecting logs from each node and forwarding them to a central location.

Conclusion

Understanding Kubernetes Deployments, StatefulSets, and DaemonSets is essential for effectively managing applications in a Kubernetes cluster. Deployments are ideal for stateless applications, StatefulSets for stateful applications requiring persistent storage, and DaemonSets for running background tasks on every node. By mastering these concepts, you can ensure your applications are resilient, scalable, and maintainable.

0
Subscribe to my newsletter

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

Written by

Ranjan Yadav
Ranjan Yadav