Mounted ConfigMap not setting environment variables

2/1/2017

I'm relatively new to K8S and hit a road block. I've created a ConfigMap to create a central location so that all Deployments can pull in and mount the same vars - something like so:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
    name: site
    namespace: staging
spec:
    revisionHistoryLimit: 1
    replicas: 1
    template:
        metadata:
            labels:
                app: site
    spec:
        containers:
            - name: nginx
              image: gcr.io/XXX/builds/nginx:develop-latest
              imagePullPolicy: Always
              command: ["nginx", "-g", "daemon off;"]
              ports:
                - containerPort: 80
              livenessProbe:
                httpGet:
                  path: /
                  port: 80
                initialDelaySeconds: 10
              readinessProbe:
                httpGet:
                  path: /
                  port: 80
                initialDelaySeconds: 10

            - name: php
              image: gcr.io/XXX/builds/php:develop-latest
              imagePullPolicy: Always
              command: ["php-fpm7.0", "--nodaemonize", "-R"]
              livenessProbe:
                tcpSocket:
                  port: 9000
                initialDelaySeconds: 10
              readinessProbe:
                tcpSocket:
                  port: 9000
                initialDelaySeconds: 10
              env:
                - name: ENV_PATH
                  value: /etc/config/.env
              volumeMounts:
                - name: config-volume
                  mountPath: /etc/config
        volumes:
            - name: config-volume
              configMap:
                name: staging-config
                items:
                  - key: site
                    path: .env

It mounts the file correctly -- I can see it when I ls inside the pod, but I'm struggling to expose the contents of the file to act as environment variables that my Laravel app can use. When defining them normally everything works as expected (see below), but taking the above approach they simply do not get set. The docs doesn't lend any further help and I'm not seeing anything online... Any pointers?

env:
  - name: variable
    value: val
  - name: var2
    value: var2val
-- DockerRocker
docker
kubernetes
laravel

1 Answer

2/1/2017

If you want to set values from your configMap directly as env vars inside your pod, then you do not need to mount them as files. Instead use:

env:
  - name: MY_ENV_VAR
    valueFrom:
      configMapKeyRef:
        name: <your-configmap-name>
        key: <key-of-your-value>

If you want to stick with the mounting option, another way might be to use your docker-entrypoint.sh script of your laravel container to somehow source the mounted env file when you start the container.

-- fishi0x01
Source: StackOverflow