【问题标题】:Cannot make GET request to service from angular pod on kubernetes cluster无法从 Kubernetes 集群上的 Angular pod 向服务发出 GET 请求
【发布时间】:2020-07-21 21:37:02
【问题描述】:

我有一个运行在 Nginx 上的 Angular 应用程序以及 Spring Boot 休息服务。 当我在本地启动 docker 容器时,一切正常,所以我唯一的猜测是 Kubernetes 配置有问题。

我在 Chrome 控制台中收到此错误,提供的 IP 地址为Failed to load resource: net::ERR_CONNECTION_TIMED_OUT 但是,使用 DNS 名称,我得到:Failed to load resource: net::ERR_NAME_NOT_RESOLVED

奇怪的是,我可以从 radial/busyboxplus:curl pod 卷曲我的服务,但我无法从我的前端 pod ping 我的服务(构建中没有 curl)。

我正在通过入口访问前端:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: main-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
    - http:
        paths:
          - path: /
            backend:
              serviceName: front
              servicePort: 80

我的前端:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: product-adviser-front-deployment
  labels:
    app: angular-front
    version: v1
spec:
  replicas: 1
  selector:
    matchLabels:
      name: product-adviser-front-deployment
  template:
    metadata:
      labels:
        name: product-adviser-front-deployment
    spec:
      containers:
        - name: product-adviser-front-app
          image: aurrix/seb:product-adviser-front
          imagePullPolicy: Always
          ports:
            - containerPort: 80
          env:
            - name: API_URL
              value: http://back.default.svc.cluster.local/
          readinessProbe:
            initialDelaySeconds: 30
            httpGet:
              path: /healthz
              port: 80
          livenessProbe:
            initialDelaySeconds: 30
            httpGet:
              path: /healthz
              port: 80
      restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
  name: front
spec:
  selector:
    name: product-adviser-front-deployment
  ports:
    - port: 80
  type: NodePort

我的后台:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: product-adviser-back-deployment
  labels:
    app: backend-service
    version: v1
spec:
  replicas: 1
  selector:
    matchLabels:
      name: product-adviser-back-deployment
  template:
    metadata:
      labels:
        name: product-adviser-back-deployment
    spec:
      containers:
        - name: product-adviser-back-deployment
          image: aurrix/seb:product-adviser
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
          readinessProbe:
            initialDelaySeconds: 30
            httpGet:
              path: /actuator/health
              port: 8080
          livenessProbe:
            initialDelaySeconds: 30
            httpGet:
              path: /actuator/health
              port: 8080
      restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
  name: back
spec:
  selector:
    name: product-adviser-back-deployment
  ports:
    - port: 80
      targetPort: 8080

【问题讨论】:

  • 浏览器应用程序试图连接到哪个地址?请记住,Angular 应用程序实际上是在浏览器中运行的,而浏览器对 Kubernetes 一无所知。
  • 在这种情况下似乎也很好,我在下面看到:http://back.default.svc.cluster.local/products和相应的错误Failed to load resource: net::ERR_NAME_NOT_RESOLVED
  • 您的浏览器无法访问该 URL。它必须是通过你的 Ingress 暴露的东西。
  • 执行kubectl get svc 以获取应用程序的外部IP。
  • @DavidMaze 好的,这实际上是有道理的。你能发布那个答案吗?

标签: angular spring-boot docker nginx kubernetes


【解决方案1】:

您已设置值:http://back.default.svc.cluster.local/ 作为 env,您是否从前端应用程序调用此端点以获取后端服务?如果是这样,这将不起作用,因为前端应用程序在浏览器上运行,并且将进行 API 调用的是浏览器。在这种情况下,您必须在入口中添加另一个规则并将请求路由到后端 pod。

我还注意到前端服务设置为 nodePort 并且从入口直接调用该服务。您可能希望将服务设置为集群 IP(默认),就像后端服务一样。

【讨论】:

    猜你喜欢
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多