10

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
  • 659
  • 1
  • 8
  • 26

3 Answers3

3

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"
2

Try configmap --from-env-file=configm

Maciek Sawicki
  • 6,717
  • 9
  • 34
  • 48
  • Thank so much for your response. But whenever I run the command " kubectl create configmap configmap --from-env-file=configmap.yaml " I got this error: "apiVersion: v1" is not a valid key name: a valid environment variable name must consist of alphabetic characters, digits, '_', '-', or '.', and must not start with a digit (e.g. 'my.env-name', or 'MY_ENV.NAME', or 'MyEnvName1', regex used for validation is '[-._a-zA-Z][-._a-zA-Z0-9]*') – Ennar.ch Jun 26 '18 at 08:22
  • It worked!! I changed all the " : " to "=" and I run "kubectl create configmap configmap --from-env-file=configmap.yaml " command. But I didn't get it! why adding --from-env-file did solve the issue, and what if I add more key value pairs in my configmap that are not envirement variables – Ennar.ch Jun 26 '18 at 08:45
2

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