【问题标题】:Connect back-end with front-end over rest api call running on kubernetes通过在 kubernetes 上运行的 rest api 调用将后端与前端连接起来
【发布时间】:2020-01-28 14:14:15
【问题描述】:

我有一个两层应用程序。 前端通过简单的http rest调用调用webapi层 http://mywebapi:5000/ 我的工作 docker compose 代码如下,应用程序正常运行

version: '3'

services:
  webfrontend:
    image: webfrontend
    build: ./nodeexpress-alibaba-ci-tutorial
    ports:
      - "3000:3000"
    networks: 
      - my-shared-network  

  mywebapi:
    image: mywebapi
    build: ./dotnetcorewebapi-alibaba-ci-tutorial
    ports:
      - "5000:5000"
    networks: 
      - my-shared-network  
networks:
  my-shared-network: {}      

现在我正试图让它在 kubernetes 上运行。 我为 webfrontend 创建了两个部署和两个服务负载均衡器,为 mywebapi 创建了 clusterip

但是,在部署之后,我发现来自 mywebapi 的数据没有到达前端。我可以通过负载均衡器公网ip在浏览器上查看前端。

Mywebapi 部署 yaml

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: '3'
  creationTimestamp: '2019-09-28T13:31:32Z'
  generation: 3
  labels:
    app: mywebapi
    tier: backend
  name: mywebapi
  namespace: default
  resourceVersion: '1047268388'
  selfLink: /apis/apps/v1beta2/namespaces/default/deployments/mywebapi
  uid: 493ab5e0-e1f4-11e9-9a64-d63fe9981162
spec:
  progressDeadlineSeconds: 600
  replicas: 2
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: mywebapi
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      annotations:
        aliyun.kubernetes.io/deploy-timestamp: '2019-09-28T14:36:01Z'
      labels:
        app: mywebapi
    spec:
      containers:
        - image: >-
            registry-intl-vpc.ap-southeast-1.aliyuncs.com/devopsci-t/mywebapi:1.0
          imagePullPolicy: Always
          name: mywebapi
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 2
  conditions:
    - lastTransitionTime: '2019-09-28T14:51:18Z'
      lastUpdateTime: '2019-09-28T14:51:18Z'
      message: Deployment has minimum availability.
      reason: MinimumReplicasAvailable
      status: 'True'
      type: Available
    - lastTransitionTime: '2019-09-28T14:49:55Z'
      lastUpdateTime: '2019-09-28T14:51:19Z'
      message: ReplicaSet "mywebapi-84cf98fb4f" has successfully progressed.
      reason: NewReplicaSetAvailable
      status: 'True'
      type: Progressing
  observedGeneration: 3
  readyReplicas: 2
  replicas: 2
  updatedReplicas: 2

Mywebapi 服务 yaml

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: '2019-09-28T13:31:33Z'
  name: mywebapi-svc
  namespace: default
  resourceVersion: '1047557879'
  selfLink: /api/v1/namespaces/default/services/mywebapi-svc
  uid: 49e21207-e1f4-11e9-9a64-d63fe9981162
spec:
  clusterIP: None
  ports:
    - name: mywebapiport
      port: 5000
      protocol: TCP
      targetPort: 5000
  selector:
    app: mywebapi
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

我什至尝试将 http rest 调用 url 更新为 http://mywebapi-svc:5000/,但仍然无法正常工作。 在 webfrontend pod 日志中,我发现以下错误

Got error: getaddrinfo ENOTFOUND mywebapi-svc mywebapi-svc:5000

衷心感谢所有帮助

谢谢

.................................................. ......

更新..

更改了 mywebapi-svc 以禁用无头。当前的 YAML 如下。问题还是一样..

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: '2019-09-29T15:21:44Z'
  name: mywebapi-svc
  namespace: default
  resourceVersion: '667545270'
  selfLink: /api/v1/namespaces/default/services/mywebapi-svc
  uid: d84503ee-e2cc-11e9-93ec-a65f0b53b1fa
spec:
  clusterIP: 172.19.0.74
  ports:
    - name: mywebapiport
      port: 5000
      protocol: TCP
      targetPort: 5000
  selector:
    app: mywebapi-default
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

【问题讨论】:

  • 你还有问题吗?
  • 错过了您的评论,是的,我愿意

标签: kubernetes alibaba-cloud


【解决方案1】:

因为你使用的是headless service

我猜你不需要无头服务来完成这项工作。因为如果标签选择器匹配多个 pod,DNS 服务器不会返回单个 IP 地址。从您的服务中删除 spec.ClusterIP 字段。

apiVersion: v1
kind: Service
metadata:
  name: mywebapi-svc
  namespace: default
spec:
  # clusterIP: None <-- remove
  ports:
    - name: mywebapiport
      port: 5000
      protocol: TCP
      targetPort: 5000
  selector:
    app: mywebapi
  type: ClusterIP

现在您可以从前端调用http://service-name.namespace-name.svc:port 端点。它应该可以工作。

如果您的 webapi 是有状态的,那么您应该使用 statefulset 而不是 deployment

【讨论】:

  • 我不是从集群外部调用的。 webfrontend 和 mywebapi 是同一个集群的一部分。
  • @Arnab 我编辑了我的答案,尝试这样做。它可能会解决您的问题。
  • 那么,mywebapi-svc.default.svc:5000 将是我的端点地址??你能告诉我任何说明这一点的文件吗,Tx
  • 同一个文档有“假设在 Kubernetes 命名空间栏中有一个名为 foo 的服务。在命名空间栏中运行的 Pod 可以通过简单地对 foo 进行 DNS 查询来查找该服务。”
猜你喜欢
  • 2012-02-19
  • 1970-01-01
  • 1970-01-01
  • 2019-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-23
  • 1970-01-01
相关资源
最近更新 更多