首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >502部署在K8集群上的Angular应用上的坏网关

502部署在K8集群上的Angular应用上的坏网关
EN

Stack Overflow用户
提问于 2020-04-14 19:26:48
回答 2查看 1.1K关注 0票数 2

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

我已经尝试了设置Nginx入口控制器,但也出现了同样的问题。我知道应用程序的生产版本对于最终版本会更好,但据我所知,开发人员访问应该仍然是可能的。Traefik入口按照IP和端口正确路由,但在此过程中某处出现故障。我已经执行了‘前端’pod和curl确认页面是托管在localhost:4200如预期。

我的docker-compose文件如下:

代码语言:javascript
复制
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如下:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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: {}

入口显示:

代码语言:javascript
复制
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显示:

代码语言:javascript
复制
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前端:

代码语言:javascript
复制
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地址,则连接被拒绝。

代码语言:javascript
复制
* 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

从前端吊舱内部卷曲给我:

代码语言:javascript
复制
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

正如您所看到的,它最初会失败,但可以正确解决。有什么解决方案吗?

你知道为什么angular前端的网关不好,而mongo db或express api不好吗?

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

EN

回答 2

Stack Overflow用户

发布于 2020-04-14 20:30:19

看起来你把前端和s3服务器pod混在一起了。该服务看起来很好,因为它填充了Endpoints。连接被拒绝pod IP通常表示pod中没有容器在监听您要执行curl的端口(4200)。

票数 1
EN

Stack Overflow用户

发布于 2020-04-14 23:26:25

您似乎已经将您的服务定义为LoadBalancer类型。LoadBalancer类型是您在“最外层”作用域中使用并向外部网络公开的类型,而ClusterIp服务更适合集群本身。

你的实际入口控制器将为你处理负载均衡和路由(而那个控制器实际上应该有一个负载均衡器服务,以防你在一个使用它的平台上)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61206617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档