使用kubernetes服务将角度前端连接到API

时间:2019-11-27 16:27:14

标签: kubernetes

在我的有角前端的环境文件中,我将API端点设置为localhost:8000,因为我的API侦听该端口,但这是在单独的pod中是正确的还是我想使用我给的名称部署文件中的后端服务。其次,如何连接后端服务是如何在下面正确的部署文件中完成它?

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ai-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ai-api
  template:
    metadata:
      labels:
        app: ai-api
    spec:
      nodeSelector:
        "beta.kubernetes.io/os": linux
      containers:
      - name: ai-api
        image: test.azurecr.io/api:v5
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 250m
            memory: 256Mi
        ports:
        - containerPort: 8000
          name: ai-api
---
apiVersion: v1
kind: Service
metadata:
  name: ai-api
spec:
  ports:
  - port: 8000
  selector:
    app: ai-api
---
# Frontend
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ai-front
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ai-front
  template:
    metadata:
      labels:
        app: ai-front
    spec:
      nodeSelector:
        "beta.kubernetes.io/os": linux
      containers:
      - name: ai-front
        image: test.azurecr.io/front-end:v5.1
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 250m
            memory: 256Mi
        ports:
        - containerPort: 80
        env:
        - name: api
          value: "ai-api"
---
apiVersion: v1
kind: Service
metadata:
  name: ai-front
spec:
  type: LoadBalancer
  ports:
  - port: 80
  #Tells loadbalancer which deployment to use
  selector:
    app: ai-front

1 个答案:

答案 0 :(得分:0)

您提到您已将前端的API端点设置为localhost:8000,因为 localhost 所指的是从其发送请求的同一容器,所以这是不正确的“连接到我自己”)。将其更改为ai-api:8000。还要确保您的api服务器正在0.0.0.0:8000上监听,而不是localhost:8000

我还看到您正在将后端服务的名称传递给前端窗格:

env:
- name: api
  value: "ai-api"

,如果您使用此环境连接到后端应用程序,则它将与您先前声明要连接到 localhost:8000 的陈述相矛盾。