简体   繁体   English

如何访问在同一个 Pod 中运行的 NodeJS 服务器?

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

I am trying to deploy an app to Kubernetes.我正在尝试将应用程序部署到 Kubernetes。 I have 2 containers: Angular app, hosted by nginx, and Node.js server.我有 2 个容器:由 nginx 托管的 Angular 应用程序和 Node.js 服务器。 I run those containers in the same pod.我在同一个 pod 中运行这些容器。 The issue is that Angular app can't access the Node.js api.问题是 Angular 应用程序无法访问 Node.js api。 Here is my nginx's default.conf:这是我的 nginx 的 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:这是我的 Angular 应用程序的 Dockerfile:

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:这是我在 Kubernetes 集群上运行的 deploy.yml:

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 .因此,当我将其部署到本地集群(并转发 service/mag-svc 的端口 8080:80)并浏览 localhost:8080 时,ui 应用程序尝试从 Node.js 服务器查询数据并失败: 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.但是,如果我连接到 Angular 应用程序容器的外壳并卷曲localhost:3000/api/mag/models ,它工作正常并且我得到预期的响应。

Looks like it tries to access localhost of my host vm, instead of localhost of a container where the Angular app is running.看起来它试图访问我的主机虚拟机的 localhost,而不是运行 Angular 应用程序的容器的 localhost。 So, how to make the Angular app call Node.js api, that runs in the same pod?那么,如何让 Angular 应用调用 Node.js api,运行在同一个 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 . angular在您的浏览器中运行,因此从浏览器中运行的应用程序到http://localhost:3000的连接将连接到您 PC 的localhost:3000

You can create a Service for the nodejs container:您可以为nodejs容器创建一个Service

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 . ...然后将localhost:3000流量转发到Servicekubectl 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 .从浏览器中运行的应用程序到http://localhost:3000将被转发到Service -> container running in Kubernetes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 tinylr / nodejs-如何访问当前运行的服务器 - tinylr/nodejs - how to access the currently running server NodeJS服务器如何运行 - How NodeJS server running for ever 如何使用公共 IP 访问连接到 Android 设备 wifi 热点的计算机上运行的 NodeJs HTTP 服务器? - How to access a NodeJs HTTP Server running on a computer connected to wifi hot-spot of Android device with public IP? nodejs运行服务器时如何在前端访问全局变量/或使用的端口 - how to access global variables / or used port in front end, while running server by nodejs NodeJS + WS 访问当前运行的 WS 服务器实例 - NodeJS + WS access currently running WS server instance 如何检查Nginx服务器上是否正在运行NodeJS脚本 - How to check that NodeJS script is running on Nginx server 在与域相同的计算机上运行的NODEJS服务器的CORS错误 - CORS error for NODEJS server running on the same machine as domain Node.js-服务来自同一服务器上运行的tomcat的请求 - Nodejs - serve a request from tomcat running on same server 通过VPN从Linux上运行的nodejs访问Windows服务器上的数据库 - Access database on Windows server through VPN from nodejs running on Linux 如何在同一 NodeJs 服务器上使用 Mysql 和 MongoDB? - How to use Mysql and MongoDB on same NodeJs server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM