<p>Is it possible to pass a Service's external IP (NodePort or LoadBalancer) as an environment variable to a container in a different service's Deployment?</p>

<p>For a concrete example, consider a Kubernetes cluster with namespaces for multiple teammates so they each have their own environment for testing. In a single environment, there are at least two services:</p>

<ol>
<li>An API Gateway service, for routing traffic to other services</li>
<li>A service that can register DNS entries to the environment</li>
</ol>

<p>For Service #2, it needs to know the external IP address of service #1. So far I've only been able to find examples that make use of <code>kubectl</code> to describe service #1 to find this information. I was hoping it would be possible to do something like this:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: gateway
  namespace: "${ env }"
  labels:
    app: gateway
spec:
  type: LoadBalancer
  ports:
     - port: 8080
  selector:
    app: gateway
</code></pre>

<p>and</p>

<pre><code>apiVersion: extensions/v1
kind: Deployment
metadata:
  name: svc2-deployment
  namespace: "${ env }"
  labels:
    app: svc2
spec:
  template:
    metadata:
      labels:
        app: svc2
    spec:
      containers:
        - name: app
          env:
            - name: GATEWAY_IP
              valueFrom:
                fieldRef:
                  fieldPath: service.gateway.----.ingressIp
</code></pre>

<p>instead of using say an <code>initContainers</code> with a script that does <code>kubectl</code> things. Especially since I'm very new to Kubernetes :)</p>

<p>Not really but you can use <a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/" rel="nofollow noreferrer">DNS</a> for that. For example:</p>

<pre><code>apiVersion: extensions/v1
kind: Deployment
metadata:
  name: svc2-deployment
  namespace: "${ env }"
  labels:
    app: svc2
spec:
  template:
    metadata:
      labels:
        app: svc2
    spec:
      containers:
        - name: app
          env:
            - name: GATEWAY_IP
              value: gateway.&lt;namespace-where-gw-is-running&gt;.svc.cluster.local
</code></pre>
