how to add configmap created from a .txt to a pod? - kubernetes

I am trying to make a simple config map from a config.txt file:
config.txt:
----------
key1=val1
key2=val2
this is the pod yaml:
apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
name: nginx
command: [ "/bin/sh", "-c", "env" ]
env:
- name: KEY_VALUES
valueFrom:
configMapKeyRef:
name: keyvalcfgmap
key1: key1
key2: key2
by running kubectl create configmap keyvalcfgmap --from-file=<filepath> -o yaml > configmap.yaml and applying the created configmap, I supposedly can use it in a pod. the question is how? I tried adding it as a volume or calling it using --from-file= and even envFrom but the best I could get was that the volume just mounted the file itself and not the configmap.

You can use envFrom like this
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: keyvalcfgmap #<--------------Here
restartPolicy: Never
or you can use configmap as env variables
env:
- name: NAME
valueFrom:
configMapKeyRef:
name: keyvalcfgmap #<--------------Here
key: key1
- name: NAME
valueFrom:
configMapKeyRef:
name: keyvalcfgmap #<--------------Here
key: key2

Related

ConfigMap value as input for another variable inside container

How to use ConfigMap for $LOCAL_IP_DB variable declared in below section as input for another variable declared? $LOCAL_IP_DB is a generic key defined inside db-secret configmap, but there is another environment variable which needs it? How to make it work?
spec:
containers:
- env:
- name: LOCAL_IP_DB
valueFrom:
configMapKeyRef:
name: db-secret
key: LOCAL_IP_DB
- name: LOG_Files
value: \\${LOCAL_IP_DB}\redis\files\
The key is using: $() instead of ${}
example-pod.yaml:
apiVersion: v1
kind: Pod
metadata:
name: example
spec:
containers:
- name: example
image: bash
args: [printenv]
env:
- name: LOCAL_IP_DB
valueFrom:
configMapKeyRef:
name: db-secret
key: LOCAL_IP_DB
- name: LOG_FILES
value: \$(LOCAL_IP_DB)\redis\files\
example-configmap.yaml:
apiVersion: v1
data:
LOCAL_IP_DB: 192.168.0.1
kind: ConfigMap
metadata:
name: db-secret
test:
controlplane $ kubectl apply -f example-pod.yaml -f example-configmap.yaml
controlplane $ kubectl logs example | grep 192
LOCAL_IP_DB=192.168.0.1
LOG_FILES=\192.168.0.1\redis\files\
You can find more information about this function here: link
Note, if you want to manage secrets Secret is the recommended way to do that.

Providing a .env file in Kubernetes

How do I provide a .env file in Kubernetes. I am using a Node.JS package that populates my process.env via my .env file.
You can do it in two ways:
Providing env variable for the container:
During creation of a pod, you can set environment variables for the containers that run in that Pod. To set environment variables, include the env field in the configuration file.
ex:
apiVersion: v1
kind: Pod
metadata:
name: envar-demo
labels:
purpose: demonstrate-envars
spec:
containers:
- name: envar-demo-container
image: gcr.io/google-samples/node-hello:1.0
env:
- name: DEMO_GREETING
value: "Hello from the environment"
- name: DEMO_FAREWELL
value: "Such a sweet sorrow"
Using ConfigMaps:
first you need to create a ConfigMaps, ex is below, here data field refers your values in a key-value pair.
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm
Now, use envFrom to define all of the ConfigMap's data as container environment variables, ex:
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: special-config
restartPolicy: Never
you can even specify individual field by giving env like below:
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: SPECIAL_LEVEL
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: SPECIAL_TYPE
Ref: configmap and env set

Is there a way to configure environment variables in a pod from a configmap created from a file?

I have a config map that was created from an application.properties file:
apiVersion: v1
data:
application.properties: |-
datasource-url: xxx
web-service-url: https://xxx
kind: ConfigMap
name: my-configmap
namespace: mynamespace
I would like to create environment variables from some of those values, e.g.:
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: my-configmap
key: datasource-url
However this doesn't work, it can't access the datasource-url property from the file.
in your case it won't work since you define data as application.properties file. It needs to be key:value maps, see here
in your case:
apiVersion: v1
data:
datasource-url: xxx
web-service-url: https://xxx
kind: ConfigMap
name: my-configmap
namespace: mynamespace

How to mount the properties file in Kubernetes configmap using manifest yaml

I use minikube on windows 10 and try to test Kubernetes ConfigMap with both literal type and outer file type. First I make below manifest yaml file to make ConfigMap.
apiVersion: v1
kind: ConfigMap
metadata:
name: simple-config
data:
mysql_root_password: password
mysql_password: password
mysql_database: test
---
apiVersion: v1
kind: Pod
metadata:
name: blog-db
labels:
app: blog-mysql
spec:
containers:
- name: blog-mysql
image: mysql:latest
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
configMapKeyRef:
name: simple-config
key: mysql_root_password
- name: MYSQL_PASSWORD
valueFrom:
configMapKeyRef:
name: simple-config
key: mysql_password
- name: MYSQL_DATABASE
valueFrom:
configMapKeyRef:
name: simple-config
key: mysql_database
ports:
- containerPort: 3306
The above configmap yaml file throws no errors. It works successfully. This time I try to test kubernetes configmap with file.
== configmap.properties
mysql_root_password=password
mysql_password=password
mysql_database=test
But I am stuck with this part. Most of configmap examples use kubectl command with --from-file option like below,
kubectl create configmap simple-config --from-file=configmap.properties
But I have no idea how to mount the properties file using manifest yaml file grammer. Any advice?
You can not directly mount a properties file in a pod without first creating a ConfigMap from the properties file.You can create configMap from env file as below
kubectl create configmap simple-config \
--from-env-file=configmap.properties

Populate ConfigMap by importing data from file in k8s

I have a requirement where i push bunch of key value pairs to a text/json file. Post that, i want to import the key value data into a configMap and consume this configMap within a POD using kubernetes-client API's.
Any pointers on how to get this done would be great.
TIA
You can do it in two ways.
Create ConfigMap from file as is.
In this case you will get ConfigMap with filename as a key and filedata as a value.
For example, you have file your-file.json with content {key1: value1, key2: value2, keyN: valueN}.
And your-file.txt with content
key1: value1
key2: value2
keyN: valueN
kubectl create configmap name-of-your-configmap --from-file=your-file.json
kubectl create configmap name-of-your-configmap-2 --from-file=your-file.txt
As result:
apiVersion: v1
kind: ConfigMap
metadata:
name: name-of-your-configmap
data:
your-file.json: |
{key1: value1, key2: value2, keyN: valueN}
apiVersion: v1
kind: ConfigMap
metadata:
name: name-of-your-configmap-2
data:
your-file.txt: |
key1: value1
key2: value2
keyN: valueN
After this you can mount any of ConfigMaps to a Pod, for example let's mount your-file.json:
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh","-c","cat /etc/config/keys" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: name-of-your-configmap
items:
- key: your-file.json
path: keys
restartPolicy: Never
Now you can get any information from your /etc/config/your-file.json inside the Pod. Remember that data is read-only.
Create ConfigMap from file with environment variables.
You can use special syntax to define pairs of key: value in file.
These syntax rules apply:
Each line in a file has to be in VAR=VAL format.
Lines beginning with # (i.e. comments) are ignored.
Blank lines are ignored.
There is no special handling of quotation marks (i.e. they will be part of the ConfigMap value)).
You have file your-env-file.txt with content
key1=value1
key2=value2
keyN=valueN
kubectl create configmap name-of-your-configmap-3 --from-env-file=you-env-file.txt
As result:
apiVersion: v1
kind: ConfigMap
metadata:
name: name-of-your-configmap-3
data:
key1: value1
key2: value2
keyN: valueN
Now you can use ConfigMap data as Pod environment variables:
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod-2
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: name-of-your-configmap-3
key: key1
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: name-of-your-configmap-3
key: key2
- name: SOME_VAR
valueFrom:
configMapKeyRef:
name: name-of-your-configmap-3
key: keyN
restartPolicy: Never
Now you can use these variables inside the Pod.
For more information check for documentation
I can also recommend Kustomize for this task. You can use it as part of your deployment pipeline to generate the K8s configuration (not only ConfigMaps, but also Deployments, NetworkPolicies, Services etc.).
In kustomize you'd need a ConfigMapGenerator. There are different options. In your case env is suitable.
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
# generate a ConfigMap named my-system-env-<some-hash> where each key/value pair in the
# env.txt appears as a data entry (separated by \n).
- name: my-system-env
env: env.txt
Other options like files will load the whole content of the file into a single value of the ConfigMap.
export the key value pairs in env or text file as is identical in the container environment variables of pod using
create a config map from configmap using
kubectl create configmap special-config --from-env-file=<key value pairs file>
update the spec for the container of pod that needs these key value pairs to
envFrom:
- configMapRef:
name: special-config
Example:
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: special-config
restartPolicy: Never