Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error communicating with the configmap

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

like image 895
Ennar.ch Avatar asked Jun 25 '18 16:06

Ennar.ch


People also ask

How do I connect my pod to ConfigMap?

Add the ConfigMap name under the volumes section of the Pod specification. This adds the ConfigMap data to the directory specified as volumeMounts. mountPath (in this case, /etc/config ). The command section lists directory files with names that match the keys in ConfigMap.

How do I check my ConfigMaps?

There are three main ways to ConfigMaps can be accessed: Mounting a ConfigMap as a data volume. Accessing the ConfigMap remotely by pods in the same namespace. Defining a ConfigMap separately from pods and using them for other components of the Kubernetes cluster.

Should I restart pod after editing ConfigMap?

Updating the config map in Kubernetes POD requires the restart of POD. A possible approach to auto-update configmap inside pod without restart is mounting it's content as volume.

Is it possible to pass ConfigMaps as environment variables?

Pods can consume ConfigMaps as environment variables, command-line arguments, or as configuration files in a volume. A ConfigMap allows you to decouple environment-specific configuration from your container images, so that your applications are easily portable.


2 Answers

Try configmap --from-env-file=configm

like image 138
Maciek Sawicki Avatar answered Oct 15 '22 14:10

Maciek Sawicki


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"
like image 41
MohammadMahdi Eilbeigi Avatar answered Oct 15 '22 14:10

MohammadMahdi Eilbeigi