gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:12:57 24 4
gpt4 key购买 nike

我有两个部署,一个部署在后端,另一个部署在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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com