简体   繁体   中英

How to access NodeJS server that is running in the same pod?

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:

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;
    }
}

Here is my Dockerfile for the Angular app:

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

Here is my deploy.yml that I run on a Kubernetes cluster:

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: "Always"
        ports:
        - containerPort: 3000
      - name: mag-ui
        image: mag-ui
        imagePullPolicy: "Always"
        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

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: GET http://localhost:3000/api/mag/models net::ERR_CONNECTION_REFUSED .

However , if I connect to Angular app container's shell and curl the localhost:3000/api/mag/models , it works fine and I get the expected response.

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?

angular runs in your browser so the connections to http://localhost:3000 from the app running in your browser are to your PC's localhost:3000 .

You can create a Service for the nodejs container:

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

... then forward traffic for localhost:3000 to the Service : kubectl port-forward -n mag mag-svc-api 3000:3000 . Connections from the app running in your browser to http://localhost:3000 would be forwarded to the Service -> container running in Kubernetes .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM