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

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

我已经尝试过设置具有相同问题的Nginx入口控制器。我知道该应用程序的生产版本对于最终版本会更好,但据我所知,仍然可以进行开发人员访问。 Traefik入口根据IP和端口正确路由,但在此过程中某处失败。我已经执行了'frontend'窗格,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/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(用于测试)时,如果我卷曲前端的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

前端吊舱内的卷曲给我:

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用于有角度的前端而不是mongo db或express api?

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

jixiang1986 回答:在K8群集上部署的Angular App上的502 Bad Gateway

似乎您在将前端与s3服务器吊舱混在一起。该服务看起来不错,因为已填充了Endpoints。连接到Pod IP的连接被拒绝通常意味着Pod中没有容器监听正在执行卷曲的端口(4200)。

,

似乎您已将服务定义为LoadBalancer类型。 LoadBalancer类型是您在“最外部”范围内使用并公开给外部网络的类型,而ClusterIp服务更适合于群集本身。

您的实际入口控制器将为您处理负载平衡和路由(如果您在使用该负载的平台上,则实际上应该有一个负载平衡器服务)。

本文链接:https://www.f2er.com/2445744.html

大家都在问