Use context: kubectl config use-context k8s-c1-H
There are two Pods named o3db-*
in Namespace project-c13
. C13 management asked you to scale the Pods down to one replica to save resources.
Scaling Down a StatefulSet in Kubernetes
In Kubernetes, Pods are often managed by higher-level controllers like Deployments, DaemonSets, or StatefulSets. In this guide, we’ll walk through the process of identifying and scaling down a StatefulSet to satisfy a specific task.
Step 1: Identify the Pods and Their Managing Controller
First, let’s take a look at the running Pods. Here we see two replicas of the o3db
Pod:
1 2 3 4 5 6 7 |
➜ kubectl -n project-c13 get pod | grep o3db o3db-0 1/1 Running 0 52s o3db-1 1/1 Running 0 42s |
From the Pod names, it looks like these Pods are managed by a StatefulSet. However, to confirm this, we can check the common Kubernetes resources that manage Pods:
1 2 3 4 5 6 |
➜ kubectl -n project-c13 get deploy,ds,sts | grep o3db statefulset.apps/o3db 2/2 2m56s |
This confirms that the Pods are indeed managed by a StatefulSet. Alternatively, we could also look at the labels of the Pods to gather this information:
1 2 3 4 5 6 7 |
➜ kubectl -n project-c13 get pod --show-labels | grep o3db o3db-0 1/1 Running 0 3m29s app=nginx,controller-revision-hash=o3db-5fbd4bb9cc,statefulset.kubernetes.io/pod-name=o3db-0 o3db-1 1/1 Running 0 3m19s app=nginx,controller-revision-hash=o3db-5fbd4bb9cc,statefulset.kubernetes.io/pod-name=o3db-1 |
Step 2: Scale Down the StatefulSet
To meet the task’s requirement, we need to scale down the number of replicas in the StatefulSet o3db
to one. This can be done using the following command:
1 2 3 4 5 6 |
➜ kubectl -n project-c13 scale sts o3db --replicas 1 statefulset.apps/o3db scaled |
Step 3: Verify the Scaling Operation
After scaling down, you can verify the current state of the StatefulSet with this command:
1 2 3 4 5 6 7 |
➜ kubectl -n project-c13 get sts o3db NAME READY AGE o3db 1/1 4m39s |
As shown above, the StatefulSet o3db
now has one replica, and everything is functioning as expected. The management is happy again!
In this guide, we walked through how to identify the managing controller of a set of Pods, confirm it’s a StatefulSet, and scale it down. This is a common task in managing Kubernetes clusters, ensuring resources are used efficiently.