gpt4 book ai didi

angular - 部署在K8群集上的Angular App上的502 Bad Gateway

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

我已经在使用流量入口 Controller 的k8集群上部署了3个服务。访问Angular构建的前端时出现502 Bad Gateway错误,但是后端节点服务器和mongo db可以正常工作。

我已经尝试过设置具有相同问题的Nginx入口 Controller 。我知道该应用程序的生产版本对于最终版本会更好,但据我所知,仍然可以进行开发人员访问。 Traefik入口根据IP和端口正确路由,但在此过程中某处失败。我已经执行了'frontend'pod,curl确认该页面正按预期托管在localhost:4200上。

我的docker-compose文件如下:

version: '3.7'

services:
frontend:
image: [image location]
ports:
- "4200"
volumes:
- ./frontend:/app

s3-server:
image: [image location]
ports:
- "3000"
links:
- database

database:
image: mongo
ports:
- "27017"

我的traefik yaml如下:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: frontend
annotations:
kubernetes.io/ingress.class: traefik
spec:
rules:
- host: [domainname]
http:
paths:
- path: /
backend:
serviceName: frontend
servicePort: 4200
- path: /api
backend:
serviceName: s3-server
servicePort: 3000
- path: /db
backend:
serviceName: database
servicePort: 27017

前端服务(使用compose创建)yaml:
apiVersion: v1
kind: Service
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.21.0 ()
creationTimestamp: null
labels:
io.kompose.service: frontend
name: frontend
spec:
ports:
- name: "4200"
port: 4200
targetPort: 4200
selector:
io.kompose.service: frontend
status:
loadBalancer: {}


入口显示:
Host              Path  Backends
---- ---- --------
api.cailean.tech
/ frontend:4200 (192.168.1.27:4200)
/api s3-server:3000 (192.168.2.20:3000)
/db database:27017 (192.168.2.14:27017)

pod 展示:
pod/database-798b8df4bd-zzxpx    1/1     Running   0          17h   192.168.2.14   kube-node-ea4d   <none>           <none>
pod/s3-server-76dd6b6b57-pq9lp 1/1 Running 0 15h 192.168.2.20 kube-node-ea4d <none> <none>
pod/nginx-86c57db685-qbcd4 1/1 Running 0 47m 192.168.1.26 kube-node-f94c <none> <none>
pod/frontend-5b8c7979d8-fggr6 1/1 Running 0 18m 192.168.1.27 kube-node-f94c <none> <none>

k描述svc前端:
Name:              frontend
Namespace: default
Labels: io.kompose.service=frontend
Annotations: kompose.cmd: kompose convert
kompose.version: 1.21.0 ()
kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{"kompose.cmd":"kompose convert","kompose.version":"1.21.0 ()"},"creationTim...
Selector: io.kompose.service=frontend
Type: ClusterIP
IP: 192.168.141.36
Port: 4200 4200/TCP
TargetPort: 4200/TCP
Endpoints: 192.168.1.27:4200
Session Affinity: None
Events: <none>

连接到Nginx服务器Pod(进行测试设置)时,如果我 curl 前端的IP地址,则会拒绝连接。
* Expire in 0 ms for 6 (transfer 0x559ae5cb5f50)
* Trying 192.168.1.27...
* TCP_NODELAY set
* Expire in 200 ms for 4 (transfer 0x559ae5cb5f50)
* connect to 192.168.1.27 port 4200 failed: Connection refused
* Failed to connect to 192.168.1.27 port 4200: Connection refused
* Closing connection 0
curl: (7) Failed to connect to 192.168.1.27 port 4200: Connection refused

从前端 pods 内部 curl 可以给我:
Rebuilt URL to: localhost:4200/
* Trying ::1...
* TCP_NODELAY set
* connect to ::1 port 4200 failed: Connection refused
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 4200 (#0)
> GET / HTTP/1.1
> Host: localhost:4200
> User-Agent: curl/7.52.1
> Accept: */*
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Access-Control-Allow-Origin: *
< Accept-Ranges: bytes
< Content-Type: text/html; charset=UTF-8
< Content-Length: 761
< ETag: W/"2f9-Ft4snhWFNqmPXU8vVB/M50CiWRU"
< Date: Tue, 14 Apr 2020 12:54:50 GMT
< Connection: keep-alive
<
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>S3 Manoeuvre Selector</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body class="mat-typography">
<app-root></app-root>
<script src="runtime.js" type="module"></script><script src="polyfills.js" type="module"></script><script src="styles.js" type="module"></script><script src="vendor.js" type="module"></script><script src="main.js" type="module"></script></body>
</html>
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact

如您所见,它最初会失败,但可以正确解决。有什么解决办法吗?

有什么想法为什么Bad Gateway用于 Angular 前端而不是mongo db或express api?

更新:
对服务yaml进行4-6次任意更改并使用'kubectl apply -f'将不会导致严重的网关错误并按预期工作,即使该服务yaml与最初用于启动该服务的yaml完全相同。无法找到任何可能的原因...

最佳答案

看来您正在与s3服务器Pod混合使用。该服务看起来不错,因为已填充了Endpoints。拒绝连接到Pod IP通常意味着Pod中没有容器监听正在执行 curl 的端口(4200)。

关于angular - 部署在K8群集上的Angular App上的502 Bad Gateway,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61206617/

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