...

/

Scaling StatefulSets

Scaling StatefulSets

Let's see how to scale StatefulSets up and down.

We'll cover the following...

Each time Kubernetes scales up a StatefulSet, it creates new Pods and PVCs. However, when scaling down, Kubernetes only terminates Pods. This means future scale-up operations only need to create new Pods and connect them back to the original PVCs. Kubernetes and the StatefulSet controller handle all of this without our help.

Modifying the Replica Count

We currently have three StatefulSet Pods and three PVCs. Edit the sts.yml file, change the replica count from 3 to 2, and save the changes.

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: tkb-sts
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  serviceName: "dullahan"
  template:
    metadata:
      labels:
        app: web
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: ctr-web
        image: nginx:latest
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: webroot
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: webroot
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "flash"
      resources:
        requests:
          storage: 1Gi
Playground

When you’ve done that, run the following command to re-post the updated configuration to the cluster. You’ll have to type exit if you’re still logged on to the jump Pod.

$ kubectl apply -f sts.yml
statefulset.apps/tkb-sts configured
Create the StatefulSet
...
Access this course and 1400+ top-rated courses and projects.