如何获取 StatefulSet Kubernetes 的副本数

How to get number of replicas of StatefulSet Kubernetes

我正在尝试在 Kubernetes 上自动缩放我的 StatefulSet。为此,我需要获取 pods.

的当前数量

处理部署时:

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

这会输出一个数字,即当前部署的数量。

但是,当您 运行

kubectl describe statefulsets

我们没有得到那么多的信息。知道如何获取有状态集的当前副本数吗?

你应该运行以下命令之一

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
 kubectl get sts web -n default -o=jsonpath='{.status.replicas}'

这也适用于 .status.readyReplicas 和 .status.currentReplicas

来自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