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.
Start the Minikube Cluster
Before creating resources, you need a running Kubernetes cluster. Minikube is a lightweight Kubernetes environment that runs on your local machine.
Navigate to your working directory:
Open the terminal and navigate to the default project folder:
cd /home/labex/projectStart Minikube:
Start Minikube to initialize a Kubernetes cluster:
minikube start- This command sets up a single-node Kubernetes cluster on your local machine.
- Minikube may take a few minutes to start depending on your system's performance.
Verify Minikube is running:
Check the status of the Minikube cluster:
minikube status- Look for components like
kubeletandapiserverlisted asRunning. - If the cluster is not running, rerun
minikube start.
- Look for components like
If you encounter issues starting Minikube. Use minikube delete to reset the environment if needed.
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 /home/labex/project/pv.yaml and execute the following command:
cd /home/labex/project
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 /home/labex/project/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 /home/labex/project/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 /home/labex/project/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 /home/labex/project/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.


