gpt4 book ai didi

node.js - 你如何让客户端部署与 Kubernetes 中的 api 部署通信

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

我所得到的只是在本地主机和云中出现的 CORS 错误。如果我手动输入 localhost 或手动获取服务外部 IP 并将其输入到 k8s 部署文件中,则它可以工作,然后再部署它,但是如果我必须启动服务、获取外部 IP 和然后在我每次启动之前将其放入配置中。

API服务

apiVersion: v1
kind: Service
metadata:
labels:
app: api
name: api-service
spec:
ports:
- port: 8080 # expose the service on internal port 80
protocol: TCP
targetPort: 8080 # our nodejs app listens on port 8080
selector:
app: api # select this application to service
type: LoadBalancer
status:
loadBalancer: {}

客户服务

apiVersion: v1
kind: Service
metadata:
name: client-service
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
app: client
type: LoadBalancer
status:
loadBalancer: {}

API 部署

apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: api
name: api-deployment
spec:
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- image: mjwrazor/docker-js-stack-api:latest
name: api-container
imagePullPolicy: IfNotPresent
resources: {}
stdin: true
tty: true
workingDir: /app
ports:
- containerPort: 8080
args:
- npm
- run
- start
envFrom:
- configMapRef:
name: server-side-configs
restartPolicy: Always
volumes: null
serviceAccountName: ""
status: {}

客户端部署

apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: client
name: client-deployment
spec:
selector:
matchLabels:
app: client
template:
metadata:
labels:
app: client
spec:
restartPolicy: Always
serviceAccountName: ""
containers:
- image: mjwrazor/docker-js-stack-client:latest
name: client-container
imagePullPolicy: IfNotPresent
resources: {}
ports:
- containerPort: 80
status: {}

我尝试添加入口

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: app-ingress
annotations:
nginx.ingress.kubernetes.io/enable-cors: "true"
nginx.ingress.kubernetes.io/cors-allow-methods: "PUT, GET, POST, DELETE, OPTIONS"
nginx.ingress.kubernetes.io/cors-allow-origin: http://client-service.default
nginx.ingress.kubernetes.io/cors-allow-credentials: "true"
spec:
rules:
- host: api-development.default
http:
paths:
- backend:
serviceName: api-service
servicePort: 8080

但也没有帮助。这是 server.js

const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();

app.use(bodyParser.json());

app.use(bodyParser.urlencoded({ extended: true }));

app.use(cors());

app.get("/", (req, res) => {
res.json({ message: "Welcome" });
});

require("./app/routes/customer.routes.js")(app);

// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});

但是就像我说的那样,我试图通过服务的主机名来解决这个问题,而不必使用外部 IP,这是可能的还是我在此过程中误解了某些事情。

客户端发送 axios 请求。无法使用环境变量,因为在通过 webpack 和 docker 构建项目后,您无法从 k8s 中注入(inject)环境变量到镜像中。我确实找到了一种非常老套的方法来创建一个带有窗口全局变量的文件,然后让 k8s 用新的窗口变量覆盖该文件。但是我必须先获取 api 的外部 IP,然后再这样做。

最佳答案

正如我们在评论中讨论的那样,您需要获得一个真实的域名才能使其在您的情况下作为自动 DNS 解析工作基本上需要它。

关于node.js - 你如何让客户端部署与 Kubernetes 中的 api 部署通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61002043/

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