<p>For my AKS container setup, I would like to pass the requested number of replicas of a given statefulset to each pod through environment variables.</p>

<p>I was trying to do this without repeating myself (once in the "replicas" setting and once in the setting of the environment variables).</p>

<p>The only real solution I could find of how to do this was using anchors and aliases as such (based on <a href="https://stackoverflow.com/questions/48632096/kubernetes-statefulset-obtain-spec-replicas-metadata-and-reference-elsewhere-i">Kubernetes StatefulSet - obtain spec.replicas metadata and reference elsewhere in configuration</a>):</p>

<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: solr
spec:
  selector:
    matchLabels:
      app: solr
  serviceName: solr-hs
  replicas: &amp;numReplicas 3
  updateStrategy:
    type: RollingUpdate
  # Make sure pods get created sequentially
  podManagementPolicy: OrderedReady
  template:
    metadata:
      labels:
        app: solr
    spec:
      containers:
      - name: kubernetes-solr
        imagePullPolicy: Always
        image: "..."
        resources:
          requests:
            memory: "8Gi"
            cpu: "0.5"
        ports:
        - containerPort: 8983
        env:
        - name: N_O_REPLICAS
          value: *numReplicas
</code></pre>

<p>Unfortunately, it seems like the "env" value has to be a string, and the integer value of "replicas" does not get cast or transformed. The following error gets thrown instead:</p>

<blockquote>
  <p>v1.EnvVar.v1.EnvVar.Value: ReadString: expects " or n, but found 3,
  error found in #10 byte of ...|,"value":3},{"name":|..., bigger
  context ...|:"solr-config"}}},{"name":"N_O_REPLICAS","value":3},</p>
</blockquote>

<p>I tried casting to a string manually by writing:</p>

<pre><code>value: !!str *numReplicas
</code></pre>

<p>But this also doesn't work and throws the following error:</p>

<blockquote>
  <p>error converting YAML to JSON: yaml: line 52: did not find expected key</p>
</blockquote>

<p>Is there any way to create a Kubernetes YAML file that allows the reusing of integer values as strings? Or is there another solution for this particular situation?</p>

Xuntar
<p><a href="https://helm.sh" rel="nofollow noreferrer">Helm</a> is what you need. </p>

<p>Actually, <code>Helm</code> is something more, than what you need, but it has a template engine (just like in Ansible), which could help with your case. Moreover, today using <code>Helm</code> it's almost mandatory with the Kubernetes, just because it has got a huge library of charts, which could help you to deploy different software very fast, e.g Elastic stack or Redis via one command...(almost). So, try this out, it could improve your work with the Kubernetes</p>
