Skip to content

Create StatefulSets in K8s

What are StatefulSets?

StatefulSets are a Kubernetes resource that provides a way to manage stateful applications. They allow for the deployment and scaling of stateful applications in a distributed environment. StatefulSets ensure that each pod in the set has a unique identity, persistent storage, and a stable network identity.

  • Predictable and persistent Pod names
  • Predictable and persistent DNS hostnames
  • Predictable and persistent volume bindings

StatefulSets use unique identity to manage stateful applications by creating a unique identity for each pod in the set. Each pod is assigned a unique name that is based on the name of the StatefulSet and a unique index. This unique identity allows StatefulSets to manage the deployment and scaling of stateful applications in a distributed environment.

How do StatefulSets Work?

StatefulSets work by creating a unique identity for each pod in the set. This unique identity allows StatefulSets to manage the deployment and scaling of stateful applications in a distributed environment. Each pod in the set has a unique name that is based on the name of the StatefulSet and a unique index. The index is used to ensure that each pod has a unique identity and can be scaled independently of the others.

StatefulSets manage stateful applications by providing a way to manage stateful applications in a distributed environment. They ensure that each pod in the set has a unique identity, persistent storage, and a stable network identity. This ensures that stateful applications can be deployed and scaled in a reliable and highly available manner.

Databases

StatefulSets are well-suited for managing stateful applications like databases. A database requires persistent storage and a stable network identity. StatefulSets can provide both of these requirements, allowing for the deployment and scaling of databases in a distributed environment.

Databases like MySQL, PostgreSQL, and MongoDB are common examples of stateful applications that can be managed using StatefulSets. These databases require persistent storage and a stable network identity to ensure reliability and availability. StatefulSets can provide both of these requirements, allowing for the deployment and scaling of databases in a distributed environment.

Message Brokers

Message brokers like Apache Kafka are another example of stateful applications that can be managed using StatefulSets. Message brokers require persistent storage and a stable network identity to ensure reliability and availability. StatefulSets can provide both of these requirements, allowing for the deployment and scaling of message brokers in a distributed environment.

Usage Example

Prepare sts-test.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
          name: web
kubectl create -f sts-test.yaml
kubectl get sts
kubectl get svc
kubectl get po -l app=nginx

The output looks like:

NAME    READY   STATUS    RESTARTS   AGE
web-0   1/1     Running   0          53s
web-1   1/1     Running   0          45s

Scale up sts: use scale

kubectl scale sts web --replicas=5

kubectl get pods -w -l app=nginx

Output:

NAME    READY   STATUS    RESTARTS   AGE
web-0   1/1     Running   0          3m6s
web-1   1/1     Running   0          2m58s
web-2   1/1     Running   0          24s
web-3   1/1     Running   0          15s
web-4   1/1     Running   0          12s

Scale down sts: use patch

kubectl patch sts web -p '{"spec":{"replicas":3}}'
kubectl get po -l app=nginx

Output:

NAME    READY   STATUS    RESTARTS   AGE
web-0   1/1     Running   0          5m56s
web-1   1/1     Running   0          5m48s
web-2   1/1     Running   0          3m14s

Delete sts

kubectl delete statefulset web

If you want to keep the pod, add --cascade=false

Conclusion

StatefulSets provide a powerful tool for managing stateful applications in Kubernetes. They allow for the deployment and scaling of stateful applications in a distributed environment while ensuring that each pod in the set has a unique identity, persistent storage, and a stable network identity. By using StatefulSets, you can ensure that your stateful applications are reliable, scalable, and highly available.# StatefulSets: Managing Stateful Applications in Kubernetes.

Feedback