Environment variables not available within pod/container when using "envFrom" and "configMapRef" (kustomize/kubernetes/laravel)

12/11/2019

I use kustomize as a standalone binary (installed with Homebrew) and have the following base/kustomization.yaml file:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
commonLabels:
  app: myservice
  app.kubernetes.io/name: myservice
  service: myservicename
  vendor: mycompany
  app.kubernetes.io/version: "0.1.0"
namePrefix: myservice-
namespace: mynamespace
commonAnnotations:
  supportMail: support@sagich.net
resources:
- pre-install-job.yaml
- cronjob.yaml
- service.yaml
- api.yaml
- worker.yaml
configMapGenerator:
- files:
  - env.properties
  name: configmap
secretGenerator:
- envs:
  - env.properties
  name: env-file-secret

The env.properties file looks like:

APP_NAME="vendor-service-whatever"
APP_ENV="production"
APP_KEY="base64:onKQb...LtU="
APP_DEBUG="false"
APP_URL="https://myamazingsite.com"
LOG_CHANNEL="stderr"
DB_CONNECTION="mysql"
DB_HOST="mariadb-slave"
DB_DATABASE="incredibledb"

And within my pre-install-job.yaml configuration I declare a container like this:

containers:
    - name: "pre-install-job"
      image: "registry.myamazingdomain.com/vendor/whatever/service:latest"
      imagePullPolicy: "Always"
      args:
        - /bin/bash
        - -c
        - php artisan migrate --force -v;
      envFrom:
        - configMapRef:
            name: "configmap"

The problem: the environment variables are not available within the pod/container. I also tried to mount the configmap as .env file within the container but no success as well. Maybe someone can point me in the right direction.

The resulting error is:

Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = forge and table_name = migrations and table_type = 'BASE TABLE')

Because the default values are used rather than the ones defined within the environment variables.

-- René Pardon
configmap
environment-variables
kubernetes
kustomize
laravel

1 Answer

12/11/2019

Ok, I tried again to mount the confirmap as .env file within the project. But this time I also provided a subPath definition. Now it works and the "php artisan migrate" command runs successful.

  volumes:
    - name: "env-volume"
      configMap:
        name: "configmap"
  containers:
    - name: "pre-install-job"
      image: "registry.myamazingdomain.com/vendor/whatever/service:latest"
      imagePullPolicy: "Always"
      args:
        - /bin/bash
        - -c
        - php artisan migrate --force -v;
      volumeMounts:
        - name: "env-volume"
          mountPath: "/var/www/html/app/.env"
          subPath: "env.properties"

The subPath definition is part of the configMapGenerator:

configMapGenerator:
  - name: configmap
    files:
      - env.properties

E.g.:

$ kubectl -n mynamespace get configmap blablubb-configmap-8c4m97m94g -o yaml

apiVersion: v1
data:
  env.properties: |
    APP_NAME="vendor-service-whatever"
    APP_ENV="production"
    APP_KEY="base64:onKQb...LtU="
    APP_DEBUG="false"
-- René Pardon
Source: StackOverflow