StatefulSets and Deployments in Kubernetes

Tuanh.netTuanh.net
4 min read

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

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

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

Step 2: Verify the StatefulSet

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

kubectl get statefulsets

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

NAME    READY   AGE
mysql 3/3 1m

Step 3: Inspect the Pods

List the pods created by the StatefulSet:

kubectl get pods -l app=mysql

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

Read more at : StatefulSets and Deployments in Kubernetes

0
Subscribe to my newsletter

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

Written by

Tuanh.net
Tuanh.net