Answers: <p>If you launch the statefulset with the following key set</p>
  <pre><code>{&quot;spec&quot;:{&quot;template&quot;:{&quot;spec&quot;:{&quot;serviceAccountName&quot;:&quot;myserviceaccount&quot;}}}}
  </code></pre>
  <p>you can run</p>
  <pre><code>curl \
  https://kubernetes/apis/apps/v1/namespaces/&lt;NAMESPACE&gt;/statefulsets/&lt;STATEFULSET&gt; \
  -k -H &quot;Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)&quot; \
  | jq '.spec.replicas'
  </code></pre>
  <p>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.</p>
  <p>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.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-07-27T12:23:44.220Z
Answers: P.S. You can get the namespace with the following command:
  `ns=$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace)`
Answers: Hakob
Answers: CC BY-SA 4.0
Answers: 2020-07-27T13:20:05.683Z
Answers: @Hakob good point, feel free to edit that in.
Answers: LLlAMnYP
Answers: CC BY-SA 4.0
Answers: 2020-07-24T16:00:13.997Z
Answers: 2020-07-24T16:00:13.997Z
Answers: LLlAMnYP
Answers: <p>Define the number of replicas in the <code>values.yaml</code> and pass it anywhere in resources (statefulset)</p>
  <p><strong>Update 1:</strong> 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 <code>values.yaml</code>:</p>
  <pre class="lang-yaml prettyprint-override"><code>replicas: 3
  </code></pre>
  <p><strong>Update 2:</strong></p>
  <p>According to this <a href="https://kubernetes.io/docs/concepts/configuration/secret/#mounted-secrets-are-updated-automatically" rel="nofollow noreferrer">link</a>, you can mount a secret now to each pod and stay up to date about replicas count.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-07-22T10:48:27.597Z
Answers: Could you please expand your answer with some more detail, and perhaps an example?
Answers: Bruce Becker
Answers: CC BY-SA 4.0
Answers: 2020-07-22T11:01:31.907Z
Answers: @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.
Answers: LLlAMnYP
Answers: CC BY-SA 4.0
Answers: 2020-07-22T12:56:35.830Z
Answers: @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.
Answers: Hakob
Answers: CC BY-SA 4.0
Answers: 2020-07-22T20:11:29.507Z
Answers: 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".
Answers: LLlAMnYP
Answers: CC BY-SA 4.0
Answers: 2020-07-24T13:55:04.010Z
Answers: @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
Answers: Hakob
Answers: CC BY-SA 4.0
Answers: 2020-07-24T15:34:06.590Z
Answers: @Hakob that makes sense - you could have a configmap with the count, that file would be dynamically updated. But then you'd still have to reread the file by the app in the pod. If one is going to these lengths, one might as well just curl the k8s api to get the live count (in a fashion similar to config reloaders).
Answers: LLlAMnYP
Answers: CC BY-SA 4.0
Answers: 2020-07-27T10:06:22.330Z
Answers: Re: update - I assumed this feature was implicitly kept in mind. Secrets and ConfigMaps have always worked like this (the ImmutableVolumes feature flag is irrelevant).
Answers: LLlAMnYP
Answers: CC BY-SA 4.0
Answers: 2020-07-22T09:04:32.790Z
Answers: 2020-07-27T07:44:28.143Z
Answers: 2020-07-27T07:44:28.143Z
Answers: Hakob
Answers: <p>There are two ways either increase the number in your DeploymentConfig directly or use <code>oc scale statefulset &lt;name_of_statfulset&gt; --replicas=2</code></p>
  <pre><code>apiVersion: apps.openshift.io/v1
  kind: DeploymentConfig
  metadata:
    labels:
    app: jekyll
    name: jekyll
    namespace: test
  spec:
    replicas: 1 &lt;----------- here increase the number
   
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2020-07-25T10:01:02.443Z
Answers: 2020-08-05T11:18:29.967Z
Answers: 2020-08-05T11:18:29.967Z
Answers: Dashrath Mundkar
Answers: <p>I suggest you try to pass this information as an update to the statefulset.</p>
  <p>See this for reference: <a href="https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/#updating-statefulsets" rel="nofollow noreferrer">https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/#updating-statefulsets</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2020-07-26T05:54:51.020Z
Answers: 2020-07-26T05:54:51.020Z
Answers: user1999424
Body: <p>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?</p>
  
Tags: <yaml><openshift>
Title: Passing replicas number into container
UserDisplayName: flowerProgrammer
ThreadText: # Passing replicas number into container
  
  (Asked by: username_0 on 2020-07-22)
  
  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?
  
  **Tags:** yaml, openshift
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_1 on 2020-07-24. Score: 4
  
  If you launch the statefulset with the following key set
  
  ```
  {"spec":{"template":{"spec":{"serviceAccountName":"myserviceaccount"}}}}
  
  ```
  
  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.
  
  > **Comments:**
  > **username_2**: P.S. You can get the namespace with the following command: `ns=$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace)`  
  > **username_1**: @username_2 good point, feel free to edit that in.  
  
  ---
  
  ### Answer by username_2 on 2020-07-22. Score: 2
  
  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`:
  
  ```yaml
  replicas: 3
  
  ```
  
  **Update 2:**
  
  According to this [link](https://kubernetes.io/docs/concepts/configuration/secret/#mounted-secrets-are-updated-automatically), you can mount a secret now to each pod and stay up to date about replicas count.
  
  > **Comments:**
  > **username_3**: Could you please expand your answer with some more detail, and perhaps an example?  
  > **username_1**: @username_3 apparently, the assumption is that helm is being used to create the statefulset and username_2 is suggesting adding something like `env: name: REPLICAS value: {{.Values.replicas}}` to the template. -1, bad practice IMHO.  
  > **username_2**: @username_1 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.  
  > **username_1**: 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".  
  > **username_2**: @username_1 You can find it problematic unless you thought only about `REPLICAS` environment variable, but I didn't mention about env anywhere in my answer. @username_0 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  
  > **username_1**: @username_2 that makes sense - you could have a configmap with the count, that file would be dynamically updated. But then you'd still have to reread the file by the app in the pod. If one is going to these lengths, one might as well just curl the k8s api to get the live count (in a fashion similar to config reloaders).  
  > **username_1**: Re: update - I assumed this feature was implicitly kept in mind. Secrets and ConfigMaps have always worked like this (the ImmutableVolumes feature flag is irrelevant).  
  
  ---
  
  ### Answer by username_4 on 2020-07-25. Score: 0
  
  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
  metadata:
    labels:
    app: jekyll
    name: jekyll
    namespace: test
  spec:
    replicas: 1 <----------- here increase the number
   
  
  ```
  
  
  ---
  
  ### Answer by username_5 on 2020-07-26. Score: 0
  
  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>
  
  
  ---
  
  