<h1>Gist</h1>

<p>I have a <code>ConfigMap</code> which provides necessary environment variables to my pods:</p>

<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: global-config
data:
  NODE_ENV: prod
  LEVEL: info

  # I need to set API_URL to the public IP address of the Load Balancer
  API_URL: http://&lt;SOME IP&gt;:3000

  DATABASE_URL: mongodb://database:27017
  SOME_SERVICE_HOST: some-service:3000
</code></pre>

<p>I am running my Kubernetes Cluster on Google Cloud, so it will automatically create a public endpoint for my service:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: gateway
spec:
  selector:
    app: gateway
  ports:
    - name: http
      port: 3000
      targetPort: 3000
      nodePort: 30000
  type: LoadBalancer
</code></pre>

<h1>Issue</h1>

<p>I have an web application that needs to make HTTP requests from the client's browser to the <code>gateway</code> service. But in order to make a request to the external service, the web app needs to know it's ip address.</p>

<p>So I've set up the pod, which serves the web application in a way, that it picks up an environment variable "<code>API_URL</code>" and as a result makes all HTTP requests to this url.</p>

<p>So I just need a way to set the <code>API_URL</code> environment variable to the public IP address of the <code>gateway</code> service to pass it into a pod when it starts. </p>

<p>I know this isn't the exact approach you were going for, but I've found that <a href="https://cloud.google.com/kubernetes-engine/docs/tutorials/configuring-domain-name-static-ip#step_2a_using_a_service" rel="nofollow noreferrer">creating a static IP address and explicitly passing it in</a> tends to be easier to work with.</p>

<p>First, create a static IP address:</p>

<pre><code>gcloud compute addresses create gke-ip --region &lt;region&gt;
</code></pre>

<p>where <code>region</code> is the GCP region your GKE cluster is located in.</p>

<p>Then you can get your new IP address with:</p>

<pre><code>gcloud compute addresses describe gke-ip --region &lt;region&gt;
</code></pre>

<p>Now you can add your static IP address to your service by specifying an explicit <code>loadBalancerIP</code>.<sup>1</sup></p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: gateway
spec:
  selector:
    app: gateway
  ports:
    - name: http
      port: 3000
      targetPort: 3000
      nodePort: 30000
  type: LoadBalancer
  loadBalancerIP: "1.2.3.4"
</code></pre>

<p>At this point, you can also hard-code it into your <code>ConfigMap</code> and not worry about grabbing the value from the cluster itself.</p>

<p><sup>1</sup>If you've already created a <code>LoadBalancer</code> with an auto-assigned IP address, setting an IP address won't change the IP of the underlying GCP load balancer. Instead, you should delete the <code>LoadBalancer</code> service in your cluster, wait ~15 minutes for the underlying GCP resources to get cleaned up, and then recreate the <code>LoadBalancer</code> with the explicit IP address.</p>
