Error communicating with the configmap

6/25/2018

Hi I am using google kubernetes engine to deploy my application. I tried to add a configMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: configmap
  namespace: default
data:
  database_user: root
  database_password: root
  database_db: db
  database_port: 5432
  database_host: mypostgres

And then in my application deployment file I mapped my envirement variables like the following

 spec:
      containers:
      - env:
        - name: DATABASE_HOST
          valueFrom:
            configMapKeyRef:
              name: configmap
              key: database_host
        - name: DATABASE_NAME
          valueFrom:
            configMapKeyRef:
              name: configmap
              key:  database_db
        - name: DATABASE_PASSWORD
          valueFrom:
            configMapKeyRef:
              name: configmap
              key: database_password
        - name: DATABASE_USER
          valueFrom:
            configMapKeyRef:
              name: configmap
              key: database_user

        - name: DATABASE_PORT
          valueFrom:
            configMapKeyRef:
              name: configmap
              key: database_port

My service I not running and I got the

CreateContainerConfigError When I try to show the result of the pod

When I do "describe my pod " I got

Error: Couldn't find key database_host

My question is, why my deployment file are not commincating with the configmap I defined

I created the configmap via this command

kubectl create configmap configmap --from-file=configmap.yaml

-- Ennar.ch
configmap
kubernetes

3 Answers

6/25/2018

Try configmap --from-env-file=configm

-- Maciek Sawicki
Source: StackOverflow

9/7/2019

Do not use --from-file command. Try kubectl apply -f configmap.yaml

-- Rahul Reddy Kandula
Source: StackOverflow

12/17/2018

As mentioned in "kubectl create configmap --help": --from-env-file='': Specify the path to a file to read lines of key=val pairs to create a configmap (i.e. a Docker .env file).

so you just need to make a file named conf with value like:

database_user= root
database_password= root
database_db= db
database_port= 5432
database_host= mypostgres

and run: "kubectl create configmap coco-config --from-env-file=conf"

UPDATE: If you put your data in " ", problem will be fixed

apiVersion: v1
kind: ConfigMap
metadata:
  name: configmap
  namespace: default
data:
  database_user: "root"
  database_password: "root"
  database_db: "db"
  database_port: "5432"
  database_host: "mypostgres"
-- MohammadMahdi Eilbeigi
Source: StackOverflow