<p>I need to set an environment variable in my container to the IP address provided by a service that is already running in my cluster.</p>

<p>I had this working with the following:</p>

<pre><code>spec:
  containers:
    - name: new_container
      image: my_image:latest
      env:
        - name: ENV_VAR
          value: k8s_service_name
</code></pre>

<p>I modified the spec to provide the container all host network traffic via:</p>

<pre><code>spec:
  hostNetwork: true
  containers:
    - name: new_container
      image: my_image:latest
      env:
        - name: ENV_VAR
          value: k8s_service_name
</code></pre>

<p>However, the container no longer seems to understand what 'k8s_service_name' is, so I need to pass the actual IP value of that service.</p>

<p>The address I need is available in the container as K8S_SERVICE_NAME_SERVICE_HOST=255.255.255.255</p>

<p>But for the application to work I need to make ENV_VAR=255.255.255.255</p>

<p>The following did not work<br>
 <code>value: $K8S_SERVICE_NAME_SERVICE_HOST</code><br>
 <code>value: $$K8S_SERVICE_NAME_SERVICE_HOST</code><br>
 <code>value: $(K8S_SERVICE_NAME_SERVICE_HOST)</code><br>
 <code>value: {{K8S_SERVICE_NAME_SERVICE_HOST}}</code><br>
 <code>value: {{env.K8S_SERVICE_NAME_SERVICE_HOST}}</code>  </p>

<p>Expected:</p>

<pre><code>&gt;&gt;&gt; printenv | grep ENV_VAR
ENV_VAR=255.255.255.255
</code></pre>

<p>Actual Results:</p>

<pre><code>&gt;&gt;&gt; printenv | grep ENV_VAR
ENV_VAR=$K8S_SERVICE_NAME_SERVICE_HOST
</code></pre>

<p>or errors involving the value not converting from yaml to json properly.</p>

<p>A solution like below would be </p>

<pre><code>valueFrom:
  fieldRef:
    fieldName: k8s_service_name.clusterIP
</code></pre>

Chris A.
<p>Use: <code>value: $(K8S_SERVICE_NAME_SERVICE_HOST)</code></p>

<p>To get the service ip. (use parenthesis) </p>
