How to scale and create a StatefulSet

Saiyam Pathak avatar
By Saiyam Pathak
Director of Technical Evangelism


Begin to learn about creating and scaling up a StatefulSet with Kubernetes by using the persistent volume claim.



In this video, we'll be doing a demo on StatefulSet. So, let's see what file I have already. So, this is a headless service. When we say a headless service, we give a cluster IP as none, creating a headless service. And in this particular service, the metadata selector is the app: nginx.

Now, let's see what we have in StatefulSet. In StatefulSet, we have defined a service named nginx, which is correct. The service name metadata is nginx. So, make sure these are correct. Now here, this StatefulSet will match the pods with the label nginx. In the template section, we have the labels as nginx, and in the spec section, we now define the image we will use and the port.

Here comes this section where we are telling what will be the persistent volume claim that will be created. This is the volume claim template. Each pod will automatically create a PVC, which now depends on the provisioner that you are using. You can also have a manual and pre-create the manual PVCs. There has to be some mechanism for creating them. And that will be the nginx HTML.

Creating and scaling up a StatefulSet

Now, since I'm just creating it again, I already have the PVs created, and I already have the PVCs. They will be attaching directly, and they have HTML5 already in there. Now, let's first see some of the resources we already have. We will use the kubectl get storageclass command, and we can see that we are using a rancher local path provisioner, which is very simple to install with the YAML file. We'll have that put in the video's description, where we will see how to deploy the local path provisioner.

You can see that the local path name over here matches the local path name. Now, when we use the kubectl create -f statefulset.yaml, it creates the service and StatefulSet. Now, let's verify the pods through kubectl get pods. First, the web-0 pod started running, and then the web-1 pod started creating. We can get the StatefulSet through the kubectl get statefulset command. We can see that the name is the web. Hence, we define the name as web, and the pod names are very simple, web-0 and web-1. Now, another interesting thing that we were talking about was the service. It's a headless service, so it has its own characteristics and it has its own properties. Now, we'll try to scale the replica set with the >kubectl scale --replicas=3 statefulset/web command, which has been scaled. Now through kubectl get pods, we can see that it has started creating. Through kubectl get pvc, we can see that a new persistent volume claim just got created, and with kubectl get pv, we can see the persistent volumes. Since it's a dynamic provisioner that I'm using, the PV was automatically created. You should have some mechanism for this. With kubectl get pods, we can see that the name of the new pod is web-2, a predictable name, so as we were told.

Now first, let's go inside one of the pods. We can do that using the kubectl exec -it web-0 --bash command. Now, we have over here that whenever we create a headless service, it will also have created a network entity, so it has its own DNS name that can be referred to. How we can check that is we can use the curl command. Use the curl web-0.nginx.default.svc.cluster.local command, and you will get the Hello world! response which was the HTML we had. You can see that we can curl this. We can also curl web-0.ngnix using the curl web-0.nginx command and, similarly, the other ones, and we will get the same response. Web-2.nginx should not have anything because I just created the PVC, and I do not have an index.html file, and that's why this particular thing is coming.

Scaling down a StatefulSet

If I exec into web-2 and see it in the HTML folder, there won't be anything. Now, let's exit this and clear the screen. What if we scale it down to one? So, it will delete in the reverse order. So first, your web-2 will delete, then web-1 will delete, and web-0 will remain. Use the kubectl scale --replicas=1 statefulset/web command to scale down. Now when we use the kubectl get pods command, we can see that this one is getting deleted, and we only have one pod remaining, which is web-0. This is how we can have persistence. We know the name of the pod that will spin up, and even if we scale up, the new pods that will be coming will be web-1 and web-2, and they will be getting attached to the same persistent volume claims that were there. The sticky session remains over there. So, StatefulSet is responsible for attaching the pods to the same PVC whenever we, you know, whenever it is rescheduled on another node and things like that.


This is the sort of flexibility you get with StatefulSet. Though you have to take care of the persistent volume creation, you have to think of a backup recovery strategy when you talk about databases because they are mainly used for databases. You will need to create the replication mechanism, the master and slave configurations, and how the data will be replicated from the master to slave and then to another slave. The copies will be maintained. The user must take care of all these things and their standard storage. That was all about StatefulSet. Thank you for watching, see you in the next lecture.

Don't stop now, check out your next lesson