Kubernetes前端和后端通信

时间:2017-07-19 19:44:52

标签: kubernetes .net-core kubectl

我一直在这个问题上苦苦挣扎几个小时。我有一个非常简单的2层dotnet核心骨架应用程序(mvc和webapi)托管在Azure上使用Kubernetes与Windows作为协调器。 部署工作正常,我可以传递基本的环境变量。我遇到的挑战是我无法确定如何将后端服务IP地址传递给前端变量。 如果我暂存部署,我可以手动将后端的公开IP传递到前端。理想情况下,这需要作为服务部署。

非常感谢任何帮助。

部署命令:

1 - kubectl create -f backend-deploy.yaml

2 - kubectl create -f backend-service.yaml

3 - kubectl create -f frontend-deploy.yaml

4 - kubectl create -f frontend-service.yaml

后端-deploy.yaml

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: acme
spec:
  replicas: 5
  template:
metadata:
  labels:
    app: acme-app
    tier: backend
spec:
  containers:
    - name: backend-container
      image: "some/image"
  imagePullSecrets:
    - name: supersecretkey
      env:
      - name: Config__AppName
        value: "Acme App"
      - name: Config__AppDescription
        value: "Just a backend application"
      - name: Config__AppVersion
        value: "1.0"
      - name: Config__CompanyName
        value: "Acme Trading Limited"

后端-service.yaml

kind: Service
apiVersion: v1
metadata:
  name: acme-app
spec:
  selector:
    app: acme-app
    tier: backend
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

前端-deploy.yaml

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: frontend
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: acme-app
        tier: frontend
    spec:
      containers:
        - name: frontend-container
          image: "some/image"
      imagePullSecrets:
        - name: supersecretkey
          env:
          - name: Config__AppName
            value: "Acme App"
          - name: Config__AppDescription
            value: "Just a frontend application"
          - name: Config__AppVersion
            value: "1.0"
          - name: Config__AppTheme
            value: "fx-theme-black"
          - name: Config__ApiUri
            value: ***THIS IS WHERE I NEED THE BACKEND SERVICE IP***
          - name: Config__CompanyName
            value: "Acme Trading Limited"

前端-service.yaml

kind: Service
apiVersion: v1
metadata:
  name: frontend
spec:
  selector:
    app: acme
    tier: frontend
  ports:
    - protocol: "TCP"
      port: 80
      targetPort: 80
  type: LoadBalancer

1 个答案:

答案 0 :(得分:2)

如果您的后端服务是在前端窗格之前创建的,那么您应该在窗格中包含环境变量ACME_APP_SERVICE_HOSTACME_APP_SEVICE_PORT

如果您的后端服务是在前端窗格之后创建的,则删除窗格并等待它们重新启动。新的pod应该有这些变量。

检查环境变量:

$ kubectl exec podName env