问题描述
我有一个 statefulset,我需要知道 pod 内部的当前副本数是多少。为此,我尝试了:
apiVersion: apps/v1
kind: StatefulSet
Metadata:
name: sample-mariadb
namespace: demo
spec:
replicas: 3
template:
spec:
containers:
- env:
- name: REPLICAS
valueFrom:
fieldRef:
apiVersion: v1
fieldpath: spec.replicas
并收到此错误:
警告 FailedCreate 4m15s (x17 over 9m43s) statefulset-controller create Pod sample-mariadb-0 in StatefulSet sample-mariadb Failed error: Pod "sample-mariadb-0" is invalid: spec.containers[1].env[3 ].valueFrom.fieldRef.fieldpath:无效值:“spec.replicas”:错误转换fieldpath:不支持字段标签:spec.replicas
如何从 Pod 内部获取当前副本数?
解决方法
您只能发送属于 Pod 规范一部分的字段。 spec.replicas
字段是 StatefulSet 规范的一部分,而不是底层 pod 的一部分。 StatefulSet 的 template
部分是 pod 规范。因此,您会收到此错误。