IP Pod to container environment variable

2/20/2019

I have an angular app and some node containers for backend, in my deployment file, how i can get container backed for connect my front end.

    apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: frontend
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: frontend
    spec:
      containers:
      - name: frontend
        image: container_imaer_backend
        env:
          - name: IP_BACKEND
            value: here_i_need_my_container_ip_pod                
        ports:
        - containerPort: 80
          protocol: TCP    
-- mleaf
kubernetes

2 Answers

2/20/2019

You could use Pod field values for environment(ref: here). That way you can set POD IP in environment variable.

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: mysql
  name: mysql
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.6
        imagePullPolicy: IfNotPresent
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: root
        - name: POD_IP
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: status.podIP
        ports:
        - containerPort: 3306
          name: mysql
          protocol: TCP
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: data
      volumes:
      - name: data
        emptyDir: {}
-- nightfury1204
Source: StackOverflow

2/20/2019

I would recommend instead of using the IP to use the DNS Name there's more info here: https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/

But basically it's http://metadata-name.namespace.svc.cluster.local so in the case for that deployment it's http://frontend.default.svc.cluster.local

It's better this way because the local IP address can change.

-- acrogenesis
Source: StackOverflow