<p>I'm trying to configure kubernetes and in my project I've separeted UI and API.
I created one Pod and I exposed both as services.</p>

<p>How can I set API_URL inside pod.yaml configuration in order to send requests from user's browser?</p>

<p>I can't use localhost because the communication isn't between containers.</p>

<p>pod.yaml</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: project
  labels:
    name: project
spec:
  containers:
  - image: 'ui:v1'
    name: ui
    ports:
    - name: ui
      containerPort: 5003
      hostPort: 5003
    env:
    - name: API_URL
      value: &lt;how can I set the API address here?&gt;
  - image: 'api:v1'
    name: api
    ports:
    - name: api
      containerPort: 5000
      hostPort: 5000
    env:
    - name: DATABASE_URL
      valueFrom:
      secretKeyRef:
        name: postgres-url
        key: url
</code></pre>

<p>services.yaml</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: api
  labels:
    name: api
spec:
  type: NodePort
  ports:
  - name: 'http'
    protocol: 'TCP'
    port: 5000
    targetPort: 5000
    nodePort: 30001
  selector:
    name: project
---
apiVersion: v1
kind: Service
metadata:
  name: ui
  labels:
    name: ui
spec:
  type: NodePort
  ports:
  - name: 'http'
    protocol: 'TCP'
    port: 80
    targetPort: 5003
    nodePort: 30003
  selector:
    name: project
</code></pre>

<p>The service IP is already available in a environment variable inside the pod, because Kubernetes initializes a set of environment variables for each service that exists at that moment.</p>

<p>To list all the environment variables of a pod</p>

<pre><code>kubectl exec &lt;pod-name&gt; env
</code></pre>

<p>If the pod was created before the service you must delete it and create it again.</p>

<p>Since you named your service <code>api</code>, one of the variables the command above should list is <code>API_SERVICE_HOST</code>.</p>

<p>But you don't really need to lookup the service IP address inside environment variables. You can simply use the service name as the hostname. Any pod can connect to the service <code>api</code>, simply by calling <code>api.default.svc.cluster</code> (assuming your service is in the <code>default</code> namespace).</p>
