kubernetes assign configmap as environment variables on deployment

9/10/2020

I am trying to deploy my image to Azure Kubernetes Service. I use command:

kubectl apply -f mydeployment.yml

And here is my deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-api
  template:
    metadata:
      labels:
        app: my-api
    spec:
      containers:
      - name: my-api
        image: mycr.azurecr.io/my-api
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 250m
            memory: 256Mi
        ports:
        - containerPort: 80
        envFrom:
          - configMapRef:
              name: my-existing-config-map

I have configmap my-existing-config-map created with a bunch of values in it but the deployment doesn't add these values as environment variables.

Config map was created from ".env" file this way:

kubectl create configmap my-existing-config-map --from-file=.env

What am I missing here?

-- Andrei
azure-aks
configmap
kubernetes

2 Answers

9/11/2020

If your .env file in this format

a=b
c=d

you need to use --from-env-file=.env instead.

To be more explanatory, using --from-file=aa.xx creates configmap looks like this

aa.xx: |
  file content here....
  ....
  ....

When the config map used with envFrom.configmapref, it just creates on env variable "aa.xx" with the content. In the case, that filename starts with '.' like .env , the env variable is not even created because the name violates UNIX env variable name rules.

-- Chayne P. S.
Source: StackOverflow

9/11/2020

As you are using the .env file the format of the file is important 1. Create config.env file in the following format which can include comments

<!-- begin snippet: js hide: false console: true babel: false --><!-- language: lang-js -->
 echo -e "var1=val1\n# this is a comment\n\nvar2=val2\n#anothercomment" > config.env
<!-- end snippet -->
  1. Create Config Map

<!-- begin snippet: js hide: false console: true babel: false --><!-- language: lang-js -->
kubectl create cm config --from-env-file=config.env 
<!-- end snippet -->
  1. Use config Map in your pod definition file
<!-- begin snippet: js hide: false console: true babel: false --><!-- language: lang-js -->
apiVersion: v1
kind: Pod
metadata:
labels:
    run: nginx
name: nginx
spec:
containers:
- image: nginx
    name: nginx
    resources: {}
    envFrom: 
    - configMapRef: 
        name: config
<!-- end snippet -->
-- Alok Adhao
Source: StackOverflow