WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Kubernetes · Intermediate · question 32 of 100

What is a StatefulSet in Kubernetes and when should you use it?

📕 Buy this interview preparation book: 100 Kubernetes questions & answers — PDF + EPUB for $5

In Kubernetes, a StatefulSet is a controller that manages the deployment and scaling of a set of stateful Pods. Unlike a Deployment, which manages stateless Pods that can be scaled up or down without any impact on application state, a StatefulSet is designed to manage stateful applications, such as databases or other stateful services, that require stable network identities and persistent storage.

Here are some of the key features and use cases of a StatefulSet in Kubernetes:

Stable Network Identity: Each Pod in a StatefulSet is given a unique and stable hostname that can be used to access the Pod within the cluster. This is critical for stateful applications that require stable network identities, such as databases or other services that rely on peer-to-peer communication.

Ordered Deployment: When a StatefulSet is created, the Pods are deployed in a specific order, based on their ordinal index. This ensures that the Pods are deployed and initialized in the correct order, which is important for stateful applications that have dependencies on other Pods in the set.

Persistent Storage: StatefulSets support the use of Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) to provide persistent storage for stateful applications. This allows stateful applications to maintain their state across Pod restarts or scaling events.

Scaling: StatefulSets support scaling operations, such as scaling up or down the number of replicas. However, scaling down a StatefulSet is more complex than scaling down a Deployment, as it requires careful consideration of the application’s state and dependencies.

Here is an example of a StatefulSet specification for a MySQL database:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  serviceName: mysql
  replicas: 3
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        env:
        - name: MYSQL\_ROOT\_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secrets
              key: mysql-root-password
        ports:
        - containerPort: 3306
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
  volumeClaimTemplates:
  - metadata:
      name: mysql-persistent-storage
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi

In this example, the StatefulSet specifies a MySQL database with three replicas, each with a unique and stable hostname. The StatefulSet also specifies the use of Persistent Volumes for persistent storage, and the use of a Secret for storing the MySQL root password.

In conclusion, a StatefulSet in Kubernetes is a controller that manages the deployment and scaling of a set of stateful Pods. StatefulSets are designed for stateful applications, such as databases or other stateful services, that require stable network identities and persistent storage. By providing features such as stable network identities, ordered deployment, and persistent storage, StatefulSets enable the deployment of stateful applications in a Kubernetes cluster with ease.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Kubernetes interview — then scores it.
📞 Practice Kubernetes — free 15 min
📕 Buy this interview preparation book: 100 Kubernetes questions & answers — PDF + EPUB for $5

All 100 Kubernetes questions · All topics