<p>I need to use an environment variable inside my kubeconfig file to point the <code>NODE_IP</code> of the Kubernetes API server. </p>

<p>My config is:</p>

<pre><code>apiVersion: v1
clusters:
- cluster:
    insecure-skip-tls-verify: true
    server: https://$NODE_IP:6443
  name: docker-for-desktop-cluster
contexts:
- context:
    cluster: docker-for-desktop-cluster
    user: docker-for-desktop
  name: docker-for-desktop
current-context: docker-for-desktop
kind: Config
preferences: {}
users:
- name: docker-for-desktop
  user:
......
</code></pre>

<p>But it seems like the kubeconfig file is not getting rendered variables when I run the command:</p>

<pre class="lang-sh prettyprint-override"><code>kubectl --kubeconfig mykubeConfigFile get pods.
</code></pre>

<p>It complains as below:</p>

<blockquote>
  <p>Unable to connect to the server: dial tcp: lookup $NODE_IP: no such host</p>
</blockquote>

<p>Did anyone try to do something like this or is it possible to make it work?</p>

<p>Thanks in advance </p>

oguz
<p>This <a href="https://serverfault.com/questions/791715/using-environment-variables-in-kubernetes-deployment-spec">thread</a> contains explanations and answers:</p>

<ul>
<li>... either <a href="https://github.com/kubernetes/kubernetes/issues/23896" rel="nofollow noreferrer">wait</a> <a href="https://github.com/kubernetes/kubernetes/issues/23896" rel="nofollow noreferrer">Implement templates · Issue #23896 · kubernetes/kubernetes</a> for <a href="https://github.com/kubernetes/kubernetes/blob/master/docs/proposals/templates.md" rel="nofollow noreferrer">the implementation of the templating proposal</a> in <code>k8s</code> (<a href="https://github.com/kubernetes/kubernetes/pull/24777" rel="nofollow noreferrer">not</a> merged yet)</li>
<li>... or preprocess your yaml with tools like:

<ul>
<li><code>envsubst</code>: </li>
</ul>

<pre class="lang-sh prettyprint-override"><code>export NODE_IP="127.0.11.1"
envsubst &lt; mykubeConfigFile.yml | kubectl --kubeconfig mykubeConfigFile.yml get pods 
</code></pre>

<ul>
<li><code>sed</code>: </li>
</ul>

<pre class="lang-sh prettyprint-override"><code>cat mykubeConfigFile.yml | sed s/\$\$EXTERNAL_IP/127.0.11.1/ | kubectl --kubeconfig mykubeConfigFile.yml get pods 
</code></pre></li>
</ul>
