Kubernetes using service cluster IP and port as environment variables

7/14/2017

I have a backend service on cluster IP 10.101.71.17 and port 26379. I have a frontend deployment where I intend to pass this service IP as an environment variable.

frontend-deployment.yaml

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: frontend
  namespace: my-namespace
spec:
  replicas: 2
  template:
    metadata:
      labels:
        name: frontend
    spec:

      containers:
      - name: frontend
        image: localhost:5000/frontend
        command: [ "/usr/local/bin/node"]
        args: [ "./index.js" ]
        imagePullPolicy: IfNotPresent
        env:
        - name: NODE_ENV
          value: production
        - name: API_URL
          value: BACKEND_HTTP_SERVICE_HOST // Here
        - name: BASIC_AUTH
          value: "true"
        - name: SECURE
          value: "true"
        - name: PORT
          value: "443"
        ports:
        - containerPort: 443
        - containerPort: 80
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 8079
      nodeSelector:
        beta.kubernetes.io/os: linux
---

I can get all the environment variables inside the pod but I am not sure what's the proper way of assigning it to the environment variable value.

-- Umair Sarfraz
kubectl
kubernetes

1 Answer

7/15/2017

I assume in your front end application your referring your back-end service in API_URL environment variable.

If this is the case just replace BACKEND_HTTP_SERVICE_HOST with 10.101.71.17:26379

        env:
        - name: NODE_ENV
          value: production
        - name: API_URL
          value: 10.101.71.17:26379 
        - name: BASIC_AUTH
          value: "true"
        - name: SECURE
          value: "true"
        - name: PORT
          value: "443"

your should consider using the DNS name for services.

-- sfgroups
Source: StackOverflow