node.js - 如何配置NGINX代理API调用以部署到Kubernetes上的后端?

标签 node.js reactjs nginx kubernetes microservices

我有两个部署,一个部署在后端,另一个部署在Kubernetes集群上。

BackendDeployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend-deployement
  labels:
    app: backend-kube
spec:
  replicas: 1
  selector:
    matchLabels:
      app: backend-app-kube
  template:
    metadata:
      labels:
        app: backend-app-kube
    spec:
      containers:
        - name: backend-kube-image
          image: fxx/frox-backend-kube
          ports:
            - containerPort: 8111

BackendService.yaml
apiVersion: v1
kind: Service
metadata:
  name: backend-app-kube-service
spec:
  selector:
    app: backend-app-kube
  ports:
    - protocol: TCP
      port: 8111
      targetPort: 8111

后端正在完美部署

FrontendDeployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend-deployement
  labels:
    app: frontend-kube
spec:
  replicas: 1
  selector:
    matchLabels:
      app: fronted-app-kube
  template:
    metadata:
      labels:
        app: fronted-app-kube
    spec:
      containers:
        - name: fronted-kube-image
          image: fxx/frox-front-kube
          ports:
            - containerPort: 80

FrontendService.yaml
apiVersion: v1
kind: Service
metadata:
  name: fronted-app-kube-service
spec:
  selector:
    app: fronted-app-kube
  type: NodePort
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

Kubernetes文档建议
现在有了后端,您可以创建连接到后端的前端。前端使用给后端服务指定的DNS名称连接到后端工作程序Pod。

https://kubernetes.io/docs/tasks/access-application-cluster/connecting-frontend-backend/



nginx.conf
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri /index.html;                 
    }
    location /api {
    proxy_pass http://backend-app-kube-service;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection ‘upgrade’;
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

现在,当我创建前端部署时,它使我在构建容器时出错
[emerg] 1#1:在上游中找不到主机

有人可以提供解决方案吗
如何使用Nginx使前端与后端对话

最佳答案

您不需要Nginx与后端对话。

前端可以使用后端地址http://backend-app-kube-service:8111直接与后端通信(如果它们位于同一 namespace 中)。

如果它们在不同的命名空间中,则需要提供FQDN,就像http://backend-app-kube-service.namespace.svc.cluster.local:8111

关于node.js - 如何配置NGINX代理API调用以部署到Kubernetes上的后端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60561227/

相关文章:

javascript - NodeJS XMLDOM - 设置序列化时丢失的 DOM 值

c# - 在将 JS 用于 REST API 的 ASP MVC 应用程序中处理安全性

javascript - NodeJS、WebStorm 和 Jasmine : ReferenceError: describe is not defined when debugging

javascript - 忽略 For 循环期间的类型错误

javascript - 单击按钮时防止 onDocumentMouseDown?

javascript - 如何从 reactJS 中的文件中获取字节数组

Nginx 代理 - 太多重定向

reactjs - useEffect 仅当状态改变时

php - 调试 php 7 Xdebug 2.4.0RC3 mac os 时出现 502 Bad Gateway

redirect - 如何在外部文件中包含重定向?