Search code examples
kubernetesgoogle-cloud-platformgoogle-kubernetes-engineautoscalingstatefulset

How to get number of replicas of StatefulSet Kubernetes


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?


Solution

  • 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