Using configmap for ports

6/5/2017

Is it possible to use configMap values for port values like containerPort or targetPort?

Here's the possible example how it could work:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: auth
spec:
  template:
    metadata:
        labels:
          app: auth
    spec:
      containers:
        - name: auth
          image: {{someImage}}
          ports:
            - name: CONTAINER_PORT
              containerPort:
                valueFrom:
                  configMapKeyRef:
                    name: auth-config
                    key: PORT
          env:
            - name: PORT
              valueFrom:
                configMapKeyRef:
                  name: auth-config
                  key: PORT
-- Peter Parada
kubernetes

1 Answer

6/5/2017

No, it is not possible for the ports section.

You can use env keys in container's commands and args. Find more here: https://github.com/kubernetes/community/blob/master/contributors/design-proposals/expansion.md

Usually most docker images have static port numbers encoded in the image with EXPOSE keyword, so having a dynamically configurable port is not a best practice from configuration standpoint. Try sticking to fixed port numbers as you can always remap them while exposing the port on Service.

-- AhmetB - Google
Source: StackOverflow