<p>As per <a href="https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/" rel="nofollow noreferrer">Kubernetes Doc</a> , to reference a env variable in config use expr $(ENV_VAR).<br />
But, this is not working for me.
In Readiness and Liveliness Probe API, I am getting token value as <strong>$(KUBERNETES_AUTH_TOKEN)</strong> instead of <strong>abcdefg</strong></p>
<pre><code>containers:
    env:
      - name: KUBERNETES_AUTH_TOKEN
        value: &quot;abcdefg&quot;
    lifecycle:
      preStop:
        exec:
          command:
            - &quot;sh&quot;
            - &quot;-c&quot;
            - |
              curl -H &quot;Authorization: $(KUBERNETES_AUTH_TOKEN)&quot; -H &quot;Content-Type: application/json&quot; -X GET http://localhost:9443/pre-stop
    readinessProbe:
      failureThreshold: 3
      httpGet:
        path: /status
        port: 9853
        scheme: HTTP
        httpHeaders:
          - name: Authorization
            value: $(KUBERNETES_AUTH_TOKEN)

    livenessProbe:
      failureThreshold: 3
      httpGet:
        path: /status
        port: 9853
        scheme: HTTP
        httpHeaders:
          - name: Authorization
            value: $(KUBERNETES_AUTH_TOKEN)
</code></pre>

limo_756
<p>There is an issue opened about a very similar question <a href="https://github.com/kubernetes/kubernetes/issues/40846" rel="nofollow noreferrer">here</a></p>
<p>Anyway, i want to share some yaml lines.<br />
This is not intended for production env, obviously it's just to play around with the commands.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  labels:
    run: ngtest
  name: ngtest
spec:
  volumes:
    - name: stackoverflow-volume
      hostPath:
        path: /k8s/stackoverflow-eliminare-vl
  initContainers:
  - name: initial-container
    volumeMounts:
      - mountPath: /status/
        name: stackoverflow-volume
    image: bash
    env:
      - name: KUBERNETES_AUTH_TOKEN
        value: &quot;abcdefg&quot;
    command:
      # this only writes a text file only to show the purpose of initContainers (here you could create a bash script)
      - &quot;sh&quot;
      - &quot;-c&quot;
      - &quot;echo $(KUBERNETES_AUTH_TOKEN) &gt; /status/$(KUBERNETES_AUTH_TOKEN).txt&quot; # withi this line, substitution occurs with $() form
  containers:
  - image: nginx    
    name: ngtest
    ports:
      - containerPort: 80
    env:
      - name: KUBERNETES_AUTH_TOKEN
        value: &quot;abcdefg&quot;
    volumeMounts:
      - mountPath: /status/
        name: stackoverflow-volume    
    lifecycle:
      preStop:
        exec:
          command:
            - &quot;sh&quot;
            - &quot;-c&quot;
            - |
              echo $KUBERNETES_AUTH_TOKEN &gt; /status/$KUBERNETES_AUTH_TOKEN.txt  &amp;&amp;
              echo 'echo $KUBERNETES_AUTH_TOKEN &gt; /status/anotherFile2.txt' &gt; /status/exec-$KUBERNETES_AUTH_TOKEN-2.sh  &amp;&amp; . /status/exec-$KUBERNETES_AUTH_TOKEN-2.sh &amp;&amp;
              echo 'echo $(KUBERNETES_AUTH_TOKEN) &gt; /status/anotherFile3.txt' &gt; /status/exec-$(KUBERNETES_AUTH_TOKEN)-3.sh  &amp;&amp; . /status/exec-$(KUBERNETES_AUTH_TOKEN)-3.sh &amp;&amp;
              echo 'curl -H &quot;Authorization: $KUBERNETES_AUTH_TOKEN&quot; -k https://www.google.com/search?q=$KUBERNETES_AUTH_TOKEN &gt; /status/googleresult.txt &amp;&amp; exit 0' &gt; /status/exec-a-query-on-google.sh  &amp;&amp; . /status/exec-a-query-on-google.sh 
            # first two lines are working
            # the third one with $(KUBERNETES_AUTH_TOKEN), do not
            # last one with a bash script creation works, and this could be a solution
    resources: {}

    # # so, instead of use http liveness
    # livenessProbe:
    #   failureThreshold: 3    
    #   httpGet:
    #     path: /status
    #     port: 80
    #     scheme: HTTP
    #     httpHeaders:
    #       - name: Authorization
    #         value: $(KUBERNETES_AUTH_TOKEN)

    # u can use the exec to call the endpoint from the bash script.
    # reads the file of the initContainer (optional) 
    # here i omitted the url call , but you can copy from the above examples
    livenessProbe:
      exec:
        command:
        - sh
        - -c
        - &quot;cat /status/$KUBERNETES_AUTH_TOKEN.txt &amp;&amp; echo -$KUBERNETES_AUTH_TOKEN- `date` top! &gt;&gt; /status/resultliveness.txt &amp;&amp; 
          exit 0&quot; 
      initialDelaySeconds: 15
      periodSeconds: 5  
  dnsPolicy: ClusterFirst
  restartPolicy: Always

</code></pre>
<p>This creates a pod with an hostPath volume (only to show you the output of the files) where will be create files based on the command of the yaml.
More details on the yaml.
If you go in you cluster machine, you can view the files produced.</p>
<p>Anyway, you should use ConfigMap, Secrets and/or <a href="https://helm.sh/docs/chart_template_guide/values_files/" rel="nofollow noreferrer">https://helm.sh/docs/chart_template_guide/values_files/</a>, which  allow us to create your own charts an separate you config values from the yaml templates.</p>
<p>Hopefully, it helps.</p>
<p>Ps. This is my first answer on StackOverflow, please don't be too rude with me!</p>
