K
Q

Kubernetes Front and Back end communication

July 19, 2017

I have been struggling for a few hours on this one. I have a very simple 2 tier dotnet core skeleton app (mvc and webapi) hosted on Azure using Kubernetes with Windows as the orchestrator. The deployment works fine and I can pass basic environment variables over. The challenge I have is that I cannot determine how to pass the backend service IP address over to the frontend variables. if I stage the deployments, I can manually pass the exposed IP of the backend into the frontend. Ideally, this needs to be deployed as a service.

Any help will be greatly appreciated.

Deployment commands:

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

backend-deploy.yaml

<!-- begin snippet: js hide: false console: true babel: false --> <!-- language: lang-html -->

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"

<!-- end snippet -->

backend-service.yaml

<!-- begin snippet: js hide: false console: true babel: false --> <!-- language: lang-html -->
kind: Service
apiVersion: v1
metadata:
  name: acme-app
spec:
  selector:
    app: acme-app
    tier: backend
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer
<!-- end snippet -->

frontend-deploy.yaml

<!-- begin snippet: js hide: false console: true babel: false --> <!-- language: lang-html -->
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"
<!-- end snippet -->

frontend-service.yaml

<!-- begin snippet: js hide: false console: true babel: false --> <!-- language: lang-html -->
kind: Service

apiVersion: v1

metadata:

  name: frontend

spec:

  selector:

    app: acme

    tier: frontend

  ports:
    - protocol: "TCP"

      port: 80

      targetPort: 80

  type: LoadBalancer
<!-- end snippet -->
-- qdrive
kubernetes
.net-core
kubectl

1 Answer

July 20, 2017

If your backend service was created BEFORE the frontend pods, you should have the environment variables

ACME_APP_SERVICE_HOST
and
ACME_APP_SEVICE_PORT
inside the pods.

If your backend service was created AFTER the frontend pods, then delete the pods and wait for them to be restarted. The new pods should have those variables.

To check the environment variables do:

$ kubectl exec podName env
-- rrh
Source: StackOverflow