Kubernetes stateful set with local-storage persistent volume
StatefulSets are similar to deployment contains identical container spec but ensures an order of the deployment. StatefulSets deploy pods in a sequential orders. Each pod as its own identity and is named with a unique ID. In the below post we are going to create a statefulsets and watch the behaviour during deletion of pod, scaling of pod and during update of container image.
The StatefulSets consist of a headless service, pods and a volume. We are going to use a local-storage volume for statefulsets. It is also common to dynamically allocate storage using volumeClaimTemplates, but due to some limitation in virtualbox i am using a manual allocation using PersistVolumeClaim.
Setup
I am using the Virtualbox(running in Ubuntu 18.04 physical machine) for this entire setup . The physical machine is Dell inspiron laptop with 12GB RAM , Intel® Core™ i7-6500U CPU @ 2.50GHz × 4 and 512GB SSD hardisk.
Step 1: Create a persistentVolume and persistemVolumeClaim
Step 2: Create a statefulSets
Create a statefulSets and map the PVC created in previous steps. The statefull set consist of the following. -
Headless service
StatefulSets with container details
Persistent Volumes
Step 3: Verify statefulSets
Now we can see the statefulsets are created and the respecive pod is created with a sequential unique id.
Step 4: Verify the DNS for statefulSets
Create a temporary busybox pod and test the dns resolution for each statefulSet pods based on the pod and stateful set name.
Now we can see both the statefulSets pods resolves for dns.
Step 5: Verify the order of pod creation on deletion
We can try deleting some pods and see how the new pods are creating
We can see the pod are creating in ascending order preserving the sequence status
Step 6: Verify the order of pod creation on scalling
Now lets scale up and scale down the statefulset and watch the order of pod creation and deletion
We can see from the above output the sequential order is preserved
Step 6: Verify the order of pod creation on update
Now lets update the image in the statefulset and watch for the pod creation and deletion
Current statefulsets has the updatestrategy of type “RollingUpdate”
Lets modify the container image to a different version
We can see from the above output the sequential order is preserved
Discussion and feedback