NH

Running Statefulset on Kubernetes

Cover Image for Running Statefulset on Kubernetes
Nasrul Hasan
Nasrul Hasan

What is a StatefulSet?

A StatefulSet is a higher-level abstraction in Kubernetes, designed to manage stateful applications. It provides unique identities and stable network names for each pod, which is vital for applications that rely on stable hostnames, data, and storage. StatefulSets are used for applications like databases (MySQL, PostgreSQL), distributed systems (Cassandra, Kafka), and more.

Creating a StatefulSet

Let's go through the steps to create a StatefulSet in Kubernetes:

  1. Create a ConfigMap or Secret (Optional)

    If your stateful application relies on configuration data or secrets, you can create a ConfigMap or Secret resource first. For example, to store database configuration parameters or access credentials.

 apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
data:
  MYSQL_ROOT_PASSWORD: my-secret-password
  MYSQL_DATABASE: mydb

2. Create a MySQL Headless Service (mysql-service.yaml):

A headless service provides stable network identities for your MySQL pods.

 apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  clusterIP: None
  selector:
    app: mysql
  ports:
    - protocol: TCP
      port: 3306

3. Create a MySQL StatefulSet (mysql-statefulset.yaml):

Here's an example of a MySQL StatefulSet with three replicas. The StatefulSet will use the ConfigMap created earlier for configuration.

 apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  serviceName: mysql-service
  replicas: 3
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql
        envFrom:
          - configMapRef:
              name: mysql-config
        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, we're using the MySQL 5.7 Docker image. You can choose a different image based on your requirements.

4. Apply the Configuration

Apply the configuration to your Kubernetes cluster using the kubectl apply -f command for each of the resources created (ConfigMap, Service, StatefulSet).

 kubectl apply -f configmap.yaml
kubectl apply -f mysql-service.yaml
kubectl apply -f mysql-statefulset.yaml

5. Verify the StatefulSet:

You can check the status and logs of the MySQL pods using the following commands:

 kubectl get statefulsets
kubectl get pods
kubectl logs mysql-0
kubectl logs mysql-1
kubectl logs mysql-2

6. Scaling and Managing Pods

Once your StatefulSet is running, you can scale it up or down by updating the replicas field in the StatefulSet specification. Use the following command to scale your StatefulSet:

 kubectl scale statefulset mysql-0 --replicas=4

7. Accessing Pods

You can access the individual pods in a StatefulSet using their stable network identities. For example, to access the first pod:

 kubectl exec -it mysql-0 -- /bin/bash

Conclusion

StatefulSets are a powerful tool for managing stateful applications in Kubernetes. They provide stable network identities and persistent storage, making them ideal for databases, distributed systems, and other stateful workloads. By following the steps outlined in this blog post, you can create and manage StatefulSets in your Kubernetes cluster effectively.

#statefulset#kubernetes#k8s#persistent data#persistence volume