Rails in Kubernetes not picking up environment variables provided by configmap

11/5/2021

I have a simple .env file with content like this

APP_PORT=5000

I add the values of that file with Kustomize. When I apply my Rails app, it crashes because it cannot find the environment vars:

I also tried to place a puts ENV['APP_PORT'] in application.rb but that one is nil

Rails Version & environment: 6.1.4.1 - development
! Unable to load application: KeyError: key not found: "APP_PORT"
Did you mean?  "APP_HOME"
bundler: failed to load command: puma (/app/vendor/bundle/ruby/2.7.0/bin/puma)
KeyError: key not found: "APP_PORT"
Did you mean?  "APP_HOME"
  /app/config/environments/development.rb:2:in `fetch'
  /app/config/environments/dev

when I change my image to image: nginx then the env vars are not there:

env
KUBERNETES_SERVICE_PORT_HTTPS=443
ELASTICSEARCH_PORT_9200_TCP_PORT=9200
KUBERNETES_SERVICE_PORT=443
ELASTICSEARCH_PORT_9200_TCP_ADDR=10.103.1.6
ELASTICSEARCH_SERVICE_HOST=10.103.1.6
HOSTNAME=myapp-backend-api-56b44c7445-h9g5m
ELASTICSEARCH_PORT=tcp://10.103.1.6:9200
PWD=/
ELASTICSEARCH_PORT_9200_TCP=tcp://10.103.1.6:9200
PKG_RELEASE=1~buster
HOME=/root
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
ELASTICSEARCH_SERVICE_PORT_9200=9200
NJS_VERSION=0.6.2
TERM=xterm
SHLVL=1
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP_PORT=443
ELASTICSEARCH_SERVICE_PORT=9200
ELASTICSEARCH_PORT_9200_TCP_PROTO=tcp
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
NGINX_VERSION=1.21.3
_=/usr/bin/env

This is my current state:

kustomization.yml

kind: Kustomization
configMapGenerator:
  - name: backend-api-configmap
    files:
      - .env
bases:
  - ../../base
patchesStrategicMerge:
  - api-deployment.yml

api-deployment.yml

apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
        - image: nginx
          imagePullPolicy: Never #imagePullPolicy: Never: the image is assumed to exist locally. No attempt is made to pull the image.              envFrom:
            - configMapRef:
                name: backend-api-configma

That is the describe pod:

❯ k describe pod xxx-backend-api-56774c796d-s2zkd
Name:         xxx-backend-api-56774c796d-s2zkd
Namespace:    default
Annotations:  <none>
Status:       Running
IP:           172.17.0.6
IPs:
  IP:           172.17.0.6
Controlled By:  ReplicaSet/xxx-backend-api-56774c796d
Containers:
  xxx-backend-api:
    Container ID:   docker://5ee3112b0805271ebe4b32d7d8e5d1b267d8bf4e220f990c085638f7b975c41f
    Image:          xxx-backend-api:latest
    Image ID:       docker://sha256:55d96a68267d80f19e91aa0b4d1ffb11525e9ede054fcbb6e6ec74356c6a3c7d
    Port:           5000/TCP
    Ready:          False
    Restart Count:  3
    Environment Variables from:
      backend-api-configmap-99fbkbc4c9  ConfigMap  Optional: false
    Environment:                        <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-w5czc (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  kube-api-access-w5czc:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason     Age               From               Message
  ----     ------     ----              ----               -------
  Normal   Scheduled  55s               default-scheduler  Successfully assigned default/xxx-backend-api-56774c796d-s2zkd to minikube
  Normal   Pulled     8s (x4 over 54s)  kubelet            Container image "xxx-backend-api:latest" already present on machine
  Normal   Created    8s (x4 over 54s)  kubelet            Created container xxx-backend-api
  Normal   Started    8s (x4 over 54s)  kubelet            Started container xxx-backend-api
  Warning  BackOff    4s (x4 over 48s)  kubelet            Back-off restarting failed container

and that is the describe configmap

❯ k describe configmaps backend-api-configmap-99fbkbc4c9
Name:         backend-api-configmap-99fbkbc4c9
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
.env:
----
RAILS_MAX_THREADS=5
APPLICATION_URL=localhost:8000/backend
FRONTEND_URL=localhost:8000
APP_PORT=3000

BinaryData
====

Events:  <none>
-- Jan
environment-variables
kubernetes
kustomize
ruby-on-rails

1 Answer

11/5/2021

I got it:

Instead of using files: in my configMapGenerator I had to use envs: like so:

configMapGenerator:
  - name: backend-api-configmap
    envs:
      - .env
-- Jan
Source: StackOverflow