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
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.
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.
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.
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.
Try configmap --from-env-file=configm
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With