TransWikia.com

Passing replicas number into container

DevOps Asked on November 20, 2021

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?

4 Answers

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

Answered by user1999424 on November 20, 2021

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
 

Answered by Dashrath Mundkar on November 20, 2021

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.

Answered by LLlAMnYP on November 20, 2021

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.

Answered by Hakob on November 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP