Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass statefulset's replica count to it's pod

I have a statefulset and I need to know what is the current replica count from inside the pod. To do so, I tried:

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

and got this error:

Warning 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: Invalid value: "spec.replicas": error converting fieldPath: field label not supported: spec.replicas

How can I get current replica count from inside the pod?

like image 967
Alif Biswas Avatar asked Oct 30 '25 15:10

Alif Biswas


1 Answers

You can only send the fields that are part of Pod specification. The spec.replicas field is part of StatefulSet specification not the underlying pod's. The template part of a StatefulSet is the pod specification. Hence, you are getting this error.

like image 51
Emruz Hossain Avatar answered Nov 03 '25 10:11

Emruz Hossain