Kubernetes: Managing environment config

9/21/2016

The reccomended method for managing environment configuration containers running in a pod is through the use of configmap. See the docs here.

This is great although we have containers that require massive amounts of environment variables, this will only expand in the future. Using the prescribed configmap method this become unweildy and hard to manage.

For example a simple deplyment file becomes massive:

apiVersion: v1
kind: Service
metadata:
  name: my-app-api
  labels:
    name: my-app-api
    environment: staging
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 80
      protocol: TCP
      name: http
  selector:
    name: my-app-api
    environment: staging
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-app-api
spec:
  replicas: 2
  revisionHistoryLimit: 10
  template:
    metadata:
      labels:
        name: my-app-api
        environment: staging
    spec:
      containers:
      - name: my-app-api
        imagePullPolicy: Always
        image: myapp/my-app-api:latest
        ports:
            - containerPort: 80
        env:
            - name: API_HOST
              value: XXXXXXXXXXX
            - name: API_ENV
              value: XXXXXXXXXXX
            - name: API_DEBUG
              value: XXXXXXXXXXX
            - name: API_KEY
              value: XXXXXXXXXXX
            - name: EJ_API_ENDPOINT
              value: XXXXXXXXXXX
            - name: WEB_HOST
              value: XXXXXXXXXXX
            - name: AWS_ACCESS_KEY
              value: XXXXXXXXXXX
            - name: AWS_SECRET_KEY
              value: XXXXXXXXXXX
            - name: CDN
              value: XXXXXXXXXXX
            - name: STRIPE_KEY
              value: XXXXXXXXXXX
            - name: STRIPE_SECRET
              value: XXXXXXXXXXX
            - name: DB_HOST
              value: XXXXXXXXXXX
            - name: MYSQL_ROOT_PASSWORD
              value: XXXXXXXXXXX
            - name: MYSQL_DATABASE
              value: XXXXXXXXXXX
            - name: REDIS_HOST
              value: XXXXXXXXXXX
      imagePullSecrets:
        - name: my-registry-key 

Are there alternate easy to inject a central environment configuration?

UPDATE

This was proposed for 1.5 although did not make the cut and looks like it will be included in 1.6. Fingers crossed...

-- AndrewMcLagan
docker
environment-variables
kubernetes
yaml

1 Answer

9/21/2016

There is a proposal currently targeted for 1.5 that aims to make this easier. As proposed, you would be able to pull all variables from a ConfigMap in one go, without having to spell out each one separately.

If implemented, it would allow you to do something like this:

Warning: This doesn't actually work yet!

ConfigMap:

apiVersion: v1
data:
  space-ships: 1
  ship-type: battle-cruiser
  weapon: laser-cannon
kind: ConfigMap
metadata:
  name: space-config

Deployment:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: space-simulator
spec:
  template:
    metadata:
      labels:
        app: space-simulator
    spec:
      containers:
      - name: space-simulator
        image: foo/space-simulator
      # This is the magic piece that would allow you to avoid all that boilerplate!
      - envFrom:
        configMap: space-config
-- Pixel Elephant
Source: StackOverflow