Use Statefulsets Controller

KubernetesKubernetesBeginner
Practice Now

Introduction

In Kubernetes, StatefulSets are used to manage stateful applications. Unlike traditional stateless applications, stateful applications require stable, unique network identifiers and persistent storage. In this lab, we will learn how to model stability with StatefulSets in Kubernetes.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/exec("`Exec`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/BasicCommandsGroup -.-> kubernetes/delete("`Delete`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") subgraph Lab Skills kubernetes/exec -.-> lab-9205{{"`Use Statefulsets Controller`"}} kubernetes/get -.-> lab-9205{{"`Use Statefulsets Controller`"}} kubernetes/delete -.-> lab-9205{{"`Use Statefulsets Controller`"}} kubernetes/apply -.-> lab-9205{{"`Use Statefulsets Controller`"}} end

Create PV and PVC

The first step is to create a Pod with a Persistent Volume (PV) and a Persistent Volume Claim (PVC). PVs and PVCs are used to store and access data persistently across Pod restarts.

To do this, you will first create a PV.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"

Save the above code in a file named pv.yaml and execute the following command:

kubectl apply -f pv.yaml

This command will create a PV named my-pv with a capacity of 1Gi and a host path of /mnt/data.

Next, you will create a PVC that requests 1Gi of storage from the PV.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Save the above code in a file named pvc.yaml and execute the following command:

kubectl apply -f pvc.yaml

This command will create a PVC named my-pvc that requests 1Gi of storage.

Create a StatefulSet

Create a file named statefulset.yaml with the following contents:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "web"
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.19.7
          ports:
            - containerPort: 80
          volumeMounts:
            - name: www
              mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
    - metadata:
        name: www
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 1Gi

In this file, we define a StatefulSet named web that creates three replicas of an NGINX pod. We also define a service named web that selects the NGINX pods using the app: nginx label. Finally, we define a persistent volume claim template for the NGINX pod's data.

To create the StatefulSet, run the following command:

kubectl apply -f statefulset.yaml

You can check the status of the StatefulSet by running the following command:

kubectl get statefulsets

Once the StatefulSet is running, you can access the NGINX pods by running the following command:

kubectl get pods
kubectl exec -it web-0 -- /bin/bash

Replace web-0 with the name of any NGINX pod created by the StatefulSet.

Congratulations, you have successfully created a StatefulSet in Kubernetes!

Update a StatefulSet

In Kubernetes, you can update a StatefulSet's pods by updating its template. Let's update the statefulset.yaml file to use NGINX version 1.20.0.

Update the statefulset.yaml file to the following:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "web"
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.20.0
          ports:
            - containerPort: 80
          volumeMounts:
            - name: www
              mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
    - metadata:
        name: www
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 1Gi

To update the StatefulSet, run the following command:

kubectl apply -f statefulset.yaml

You can check the status of the StatefulSet by running the following command:

kubectl get statefulsets

Congratulations, you have successfully updated a StatefulSet in Kubernetes!

Scale a StatefulSet

In Kubernetes, you can scale a StatefulSet up or down by changing its replicas field. Let's scale our web StatefulSet to five replicas.

Update the statefulset.yaml file to the following:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "web"
  replicas: 5
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.20.0
          ports:
            - containerPort: 80
          volumeMounts:
            - name: www
              mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
    - metadata:
        name: www
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 1Gi

To update the StatefulSet, run the following command:

kubectl apply -f statefulset.yaml

You can check the status of the StatefulSet by running the following command:

kubectl get statefulsets

Congratulations, you have successfully scaled a StatefulSet in Kubernetes!

Delete a StatefulSet

In Kubernetes, you can delete a StatefulSet by running the following command:

kubectl delete statefulset web

This will delete the web StatefulSet and all of its pods.

Congratulations, you have completed the lab on modelling stability with StatefulSets in Kubernetes!

Summary

In this lab, we learned how to model stability with StatefulSets in Kubernetes. We created a StatefulSet, updated a StatefulSet, scaled a StatefulSet, and deleted a StatefulSet. These are the basic operations you will need to perform when working with StatefulSets in Kubernetes.

Other Kubernetes Tutorials you may like