Passing replicas number into container
If you launch the statefulset with the following key set
{&quot;spec&quot;:{&quot;template&quot;:{&quot;spec&quot;:{&quot;serviceAccountName&quot;:&quot;myserviceaccount&quot;}}}}

you can run
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'

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.