<p>I am trying to deploy an app to Kubernetes. I have 2 containers: Angular app, hosted by nginx, and Node.js server. I run those containers in the same pod. The issue is that Angular app can't access the Node.js api.
Here is my nginx's default.conf:</p>
<pre><code>server {
    listen 80;
    server_name localhost;

    location / {
        root /usr/share/nginx/html;
        try_files $uri $uri/ /index.html;
        index index.html;
    }

    location /api/ {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
    }
}
</code></pre>
<p>Here is my Dockerfile for the Angular app:</p>
<pre><code>FROM node:lts-alpine AS build

WORKDIR /app
COPY . .

RUN npm install

RUN npm run build:prod

FROM nginx:stable-alpine

COPY nginx/default.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/dist/mag-ui /usr/share/nginx/html
</code></pre>
<p>Here is my deploy.yml that I run on a Kubernetes cluster:</p>
<pre><code>kind: Namespace
metadata:
  name: mag
---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: mag
  name: mag
  labels:
    app: mag
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mag
  minReadySeconds: 10
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  template:
    metadata:
      labels:
        app: mag
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: mag-api
        image: mag-api
        imagePullPolicy: &quot;Always&quot;
        ports:
        - containerPort: 3000
      - name: mag-ui
        image: mag-ui
        imagePullPolicy: &quot;Always&quot;
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  namespace: mag
  name: mag-svc
  labels:
    app: mag
spec:
  ports:
  - port: 80
    name: ui
    targetPort: 80
  selector:
    app: mag
</code></pre>
<p>So when I deploy this to my local cluster (and forward port 8080:80 of service/mag-svc) and browse localhost:8080, the ui app tries to query data from Node.js server and fails with: <code>GET http://localhost:3000/api/mag/models net::ERR_CONNECTION_REFUSED</code>.</p>
<p><strong>However</strong>, if I connect to Angular app container's shell and curl the <code>localhost:3000/api/mag/models</code>, it works fine and I get the expected response.</p>
<p>Looks like it tries to access localhost of my host vm, instead of localhost of a container where the Angular app is running. So, how to make the Angular app call Node.js api, that runs in the same pod?</p>

<p><a href="https://stackoverflow.com/a/45307259/1423507"><code>angular</code> runs in your browser</a> so the connections to <code>http://localhost:3000</code> from the app running in your browser are to your PC's <code>localhost:3000</code>.</p>
<p>You can create a <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer"><code>Service</code></a> for the <code>nodejs</code> container:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
  namespace: mag
  name: mag-svc-api
  labels:
    app: mag
spec:
  ports:
  - port: 3000
    name: mag-api
    targetPort: 3000
  selector:
    app: mag
</code></pre>
<p>... then forward traffic for <code>localhost:3000</code> to the <code>Service</code>: <code>kubectl port-forward -n mag mag-svc-api 3000:3000</code>. Connections from the app running in your browser to <code>http://localhost:3000</code> would be forwarded to the <code>Service</code> -&gt; container running in <a href="https://kubernetes.io/" rel="nofollow noreferrer">Kubernetes</a>.</p>
