I have a statefulset with an init container which creates openshift objects upon scaling. I need to pass it the replicas number from the same yaml definition. However the replicas filed doesn't seem to be working with fieldRef. Is there another way to do it?


If you launch the statefulset with the following key set


you can run

curl \
https://kubernetes/apis/apps/v1/namespaces/<NAMESPACE>/statefulsets/<STATEFULSET> \
-k -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \
| jq '.spec.replicas'

to extract the replica count. This, of course, relies on the service account (myserviceaccount in this example) to have appropriate permissions to actually query the k8s API for this information, as well as having curl and jq available in the container.

You could then setup some kind of background job in the container to regularly make this request and update an env var or file or whatever.

  • 1
    P.S. You can get the namespace with the following command: ns=$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace) – Hakob Jul 27 '20 at 12:23
  • @Hakob good point, feel free to edit that in. – LLlAMnYP Jul 27 '20 at 13:20

Define the number of replicas in the values.yaml and pass it anywhere in resources (statefulset)

Update 1: You can neither have a dynamic environment variable nor refer to the replicas field in STS. So the only way to get an updated count of STS in each pod is to redeploy with the new value of replicas in values.yaml:

replicas: 3

Update 2:

According to this link, you can mount a secret now to each pod and stay up to date about replicas count.

  • Could you please expand your answer with some more detail, and perhaps an example? – Bruce Becker Jul 22 '20 at 10:48
  • @BruceBecker apparently, the assumption is that helm is being used to create the statefulset and Hakob is suggesting adding something like env: name: REPLICAS value: {{.Values.replicas}} to the template. -1, bad practice IMHO. – LLlAMnYP Jul 22 '20 at 11:01
  • @LLlAMnYP and I have worked with charts for a long time and IMHO can assure you that it is the best practice to do in this situation. Check a chart of any tool which have sts in it. – Hakob Jul 22 '20 at 12:56
  • I find it problematic that in order to keep the sts internally consistent (ie REPLICAS reflecting the true replica count) you now have to recreate every pod in the set when you want to scale horizontally. It's like "sure, let's employ an antipattern, since it's not likely to be a big deal". – LLlAMnYP Jul 22 '20 at 20:11
  • @LLlAMnYP You can find it problematic unless you thought only about REPLICAS environment variable, but I didn't mention about env anywhere in my answer. @flowerprogrammer if you want to have a replicas count up to date in every pod, it's better to keep the count in a file, which will be mounted to each pod as ConfigMap. Again you still need to keep your resources in a chart in order to render that ConfigMap's value as replicas count – Hakob Jul 24 '20 at 13:55

I suggest you try to pass this information as an update to the statefulset.

See this for reference: https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/#updating-statefulsets


There are two ways either increase the number in your DeploymentConfig directly or use oc scale statefulset <name_of_statfulset> --replicas=2

apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
  app: jekyll
  name: jekyll
  namespace: test
  replicas: 1 <----------- here increase the number

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.