DEV Community

Anh Trần Tuấn
Anh Trần Tuấn

Posted on • Originally published at tuanh.net on

StatefulSets and Deployments in Kubernetes

1. Understanding Deployments

Deployments are the most commonly used Kubernetes resource for managing stateless applications. A Deployment allows you to describe an application’s life cycle, including scaling, updating, and rolling back. It ensures that the desired number of pod replicas are running and can automatically replace failed or unhealthy pods.

Deployments , on the other hand, are typically used for stateless applications. These are applications where any instance (or pod) can be replaced or scaled without requiring any unique identity or storage persistence. Web servers, microservices, and batch processing applications are classic examples of when to use Deployments.

Key Features of Deployments:

  • Statelessness : Pods in a Deployment are interchangeable and do not retain any state. This means if a pod fails, a new one can be spun up without affecting the application.
  • Rolling Updates : Deployments can update applications with zero downtime by gradually replacing old pods with new ones.
  • ReplicaSet Management : Deployments manage ReplicaSets, ensuring the correct number of pods are running at all times.

Example YAML for a Deployment:

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:1.21.0
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

2. Understanding StatefulSets

StatefulSets are designed for stateful applications that require persistent storage, unique network identifiers, and stable, persistent identities. Unlike Deployments, StatefulSets manage the deployment and scaling of a set of pods where each pod is unique and maintains a consistent identity across restarts.

Key Features of StatefulSets:

  • Stable Pod Names : Each pod in a StatefulSet gets a stable, unique identifier based on its order (e.g., pod-name-0 , pod-name-1 ).
  • Ordered, Graceful Deployment and Scaling : Pods are created, updated, or deleted in order, ensuring the application’s state is preserved.
  • Persistent Volumes : StatefulSets work in conjunction with PersistentVolume Claims (PVCs) to retain data even if a pod is deleted or rescheduled.

Example YAML for a StatefulSet:

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:8.0
        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
Enter fullscreen mode Exit fullscreen mode

3. Key Differences Between StatefulSets and Deployments

| Feature | StatefulSets | Deployments |
| Pod Identity | Each pod has a stable, unique identity. | Pods are interchangeable and stateless. |
| Storage | Works with Persistent Volumes to retain data. | Typically used for ephemeral storage. |
| Network Identity | Each pod has a stable hostname and network identity. | Pods share a common service name. |
| Scaling | Pods are scaled up/down sequentially. | Pods are scaled up/down in parallel. |
| Use Case | Databases, distributed systems (e.g., Kafka, Zookeeper) | Stateless applications (e.g., web servers, APIs) |

4. When to Use StatefulSets vs. Deployments

  • Use StatefulSets when you need to maintain a unique identity for each pod, retain persistent storage, and ensure ordered deployment and scaling. Examples include databases (e.g., MySQL, Cassandra), distributed systems (e.g., Kafka, Zookeeper), and stateful applications that cannot tolerate random pod restarts.
  • Use Deployments for stateless applications where pods can be replaced without affecting the application’s overall state. Examples include web servers, APIs, and microservices where state is not retained in the pods themselves.

5. Demo: Deploying a Stateful Application with StatefulSets

Let’s walk through a demo of deploying a MySQL database using a StatefulSet in Kubernetes.

Step 1: Create the StatefulSet

We’ll use the example YAML provided earlier to deploy a MySQL database. Save the YAML file as mysql-statefulset.yaml and apply it:

kubectl apply -f mysql-statefulset.yaml
Enter fullscreen mode Exit fullscreen mode

Step 2: Verify the StatefulSet

Check the status of the StatefulSet to ensure the pods are being created in order:

kubectl get statefulsets
Enter fullscreen mode Exit fullscreen mode

You should see output indicating that the StatefulSet is managing three replicas:

NAME READY AGE
mysql 3/3 1m
Enter fullscreen mode Exit fullscreen mode

Step 3: Inspect the Pods

List the pods created by the StatefulSet:

kubectl get pods -l app=mysql
Enter fullscreen mode Exit fullscreen mode

You should see the pods with stable, unique names like mysql-0 , mysql-1 , and mysql-2.

NAME READY STATUS RESTARTS AGE
mysql-0 1/1 Running 0 2m
mysql-1 1/1 Running 0 1m
mysql-2 1/1 Running 0 30s
Enter fullscreen mode Exit fullscreen mode

Step 4: Check Persistent Volumes

Verify that Persistent Volumes are created for each pod:

kubectl get pvc
Enter fullscreen mode Exit fullscreen mode

Each pod should have its own PVC, ensuring data persistence:

NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mysql-persistent-storage-0 Bound pvc-12345678-1234-1234-1234-123456789abc 10Gi RWO standard 2m
mysql-persistent-storage-1 Bound pvc-23456789-2345-2345-2345-234567890bcd 10Gi RWO standard 1m
mysql-persistent-storage-2 Bound pvc-34567890-3456-3456-3456-345678901cde 10Gi RWO standard 30s
Enter fullscreen mode Exit fullscreen mode

6. Demo: Deploying a Stateless Application with Deployments

Now, let’s deploy an Nginx web server using a Deployment.

Step 1: Create the Deployment

Use the example YAML provided earlier to deploy an Nginx server. Save the YAML file as nginx-deployment.yaml and apply it:

kubectl apply -f nginx-deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Step 2: Verify the Deployment

Check the status of the Deployment to ensure it’s running the desired number of replicas:

kubectl get deployments
Enter fullscreen mode Exit fullscreen mode

You should see output indicating the deployment status:

NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 1m
Enter fullscreen mode Exit fullscreen mode

Step 3: Inspect the Pods

List the pods created by the Deployment:

kubectl get pods -l app=nginx
Enter fullscreen mode Exit fullscreen mode

Since these pods are stateless, they don’t have unique identifiers:

NAME READY STATUS RESTARTS AGE
nginx-deployment-xxxxxxxxxx-abcde 1/1 Running 0 2m
nginx-deployment-xxxxxxxxxx-fghij 1/1 Running 0 2m
nginx-deployment-xxxxxxxxxx-klmno 1/1 Running 0 2m
Enter fullscreen mode Exit fullscreen mode

Step 4: Test Rolling Update

To demonstrate the power of Deployments, let’s perform a rolling update. Edit the nginx image version in the nginx-deployment.yaml file to 1.21.1:

image: nginx:1.21.1
Enter fullscreen mode Exit fullscreen mode

Apply the update:

kubectl apply -f nginx-deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Monitor the rollout status:

kubectl rollout status deployment/nginx-deployment
Enter fullscreen mode Exit fullscreen mode

You should see the Deployment gradually replacing old pods with new ones.

7. Conclusion

Both StatefulSets and Deployments are powerful tools in Kubernetes, each serving different types of workloads. Deployments are perfect for stateless applications that require rapid scaling and frequent updates, while StatefulSets are essential for stateful applications where the order and identity of pods matter. By understanding these differences and applying the right Kubernetes object for your workload, you can ensure that your applications are both robust and scalable.

Whether you’re deploying a simple web server or a complex database, Kubernetes provides the tools you need to manage your applications effectively. Experiment with StatefulSets and Deployments in your own Kubernetes environment to see how they can best serve your needs.

Happy Kubernetes-ing!

Read posts more at : StatefulSets and Deployments in Kubernetes

Top comments (0)