How to get number of replicas of StatefulSet Kubernetes

6/3/2019

I am trying to autoscale my StatefulSet on Kubernetes. In order to do so, I need to get the current number of pods.

When dealing with deployments:

kubectl describe deployments [deployment-name] | grep desired | awk '{print $2}' | head -n1

This outputs a number, which is the amount of current deployments.

However, when you run

kubectl describe statefulsets

We don't get back as much information. Any idea how I can get the current number of replicas of a stateful set?

-- DuDoff
autoscaling
google-cloud-platform
google-kubernetes-engine
kubernetes
statefulset

2 Answers

6/3/2019
 kubectl get sts web -n default -o=jsonpath='{.status.replicas}'

This also works with .status.readyReplicas and .status.currentReplicas

From github.com/kubernetes:

// replicas is the number of Pods created by the StatefulSet controller.
Replicas int32

// readyReplicas is the number of Pods created by the StatefulSet controller that have a Ready Condition.
ReadyReplicas int32

// currentReplicas is the number of Pods created by the StatefulSet controller from the StatefulSet version
// indicated by currentRevision.
CurrentReplicas int32

-- frankd
Source: StackOverflow

6/3/2019

you should be running one of the below command

master $ kubectl get statefulsets
NAME      DESIRED   CURRENT   AGE
web       4         4         2m
master $
master $ kubectl get sts
NAME      DESIRED   CURRENT   AGE
web       4         4         2m


number of running pods
---------------------
master $ kubectl describe sts web|grep Status
Pods Status:        4 Running / 0 Waiting / 0 Succeeded / 0 Failed

another way
------------
master $ kubectl get sts --show-labels
NAME      DESIRED   CURRENT   AGE       LABELS
web       4         4         33s       app=nginx

master $ kubectl get po -l app=nginx
NAME      READY     STATUS    RESTARTS   AGE
web-0     1/1       Running   0          56s
web-1     1/1       Running   0          55s
web-2     1/1       Running   0          30s
web-3     1/1       Running   0          29s

master $ kubectl get po -l app=nginx --no-headers
web-0     1/1       Running   0         2m
web-1     1/1       Running   0         2m
web-2     1/1       Running   0         1m
web-3     1/1       Running   0         1m
master $
master $
master $ kubectl get po -l app=nginx --no-headers | wc -l
4
-- P Ekambaram
Source: StackOverflow