I have a env
variable called app_conf_path
which points to a \location\file.yaml
which in turn contains all the values required for the application to work.The application needs this app_conf_path
which has the location of file.yaml
to run the application. How can i create a configmap
for this type of setup. Right now i am having that file.yaml
in a persistentvolume
and have that env
variable pointing to that mountlocation
. I came to know about configmaps
only recently. Any help on this would be appreciated.
Configmaps can be created using either YAML manifests or kubectl.
kubectl create configmap app_conf_path --from-file=file.yaml
OR
configmap/configmap-multikeys.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app_conf_path
data:
Key1: very
Key2: value2
kubectl create -f filename.yaml
apiVersion: v1
kind: Pod
metadata:
name: busy
spec:
containers:
- name: busy
image: busybox
volumeMounts:
- name: config
mountPath: /etc/config
volumes:
- name: config
configMap:
name: app_conf_path
Would generally set the config in the configmap like:
data:
file.yaml: |
# Some config
# content goes here
And then in your pod template you have something like:
containers:
- ...
env:
- name: app_conf_path
value: /config/file.yml
volumeMounts:
- name: config
mountPath: /config
volumes:
- name: config
configMap:
name: your-configmap
I have a
env
variable calledapp_conf_path
which points to a\location\file.yaml
which in turn contains all the values required for the application to work.The application needs thisapp_conf_path
which has the location offile.yaml
to run the application. How can i create aconfigmap
for this type of setup?
I'll begin talking about the concepts of ConfigMaps:
ConfigMap can be treated like a volume in kubernetes:
- The data stored in a
ConfigMap
object can be referenced in a volume of typeconfigMap
and then consumed by containerized applications running in a Pod.- When referencing a
configMap
object, you can simply provide its name in the volume to reference it. You can also customize the path to use for a specific entry in the ConfigMap
Creating a ConfigMap From File:
kubectl create configmap <CONFIGMAP_NAME> --from-file=/location/file.yaml
--from-file
argument, example:kubectl create configmap <CONFIGMAP_NAME> \
--from-file=path/db.properties \
--from-file=path/ui.properties
I want to stop mounting the
persistentvolume
which has thisfile.yaml
and thefile.yaml
is a simpleyaml
file with details ofdbconnectionstrings
andpaths
for otherapps
From the concepts we saw above, your intention to stop having to mount the file to a PV to serve the config file can be fully realized using a ConfigMap
.
Reproducible Example:
This will be a example to show you how to achieve the portion your question requires.
ubuntu
pod which has a config file mounted in /tmp/file.yaml
and that file path will be a Env variable called app_conf_path
.First, I'll create a file called file.yaml
and add 3 values:
$ cat file.yaml
key1: value1
key2: value2
key3: value3
NOTE: the name file.yaml
is not very common, I'm using it to emulate your environment, usually we use something like app.properties
and it does not require any previous structure, just all values in a key:value
pair form, like in my example.
app.config
from the file file.yaml
. The file is on the same folder I'm running the command, thus I don't have to specify the full path:$ kubectl create configmap app.config --from-file=file.yaml
configmap/app.config created
The filename becomes the reference inside the configmap and will be used later.
$ kubectl describe configmap app.config
Name: app.config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
file.yaml:
----
key1: value1
key2: value2
key3: value3
Events: <none>
app_conf_path
to /tmp/file.yaml
, here is the app-deploy.yaml
for that:apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
labels:
app: my-app
spec:
replicas: 1
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: ubuntu
image: ubuntu
command: [ "/bin/bash", "-c", "--" ]
args: [ "while true; do sleep 3000; done;" ]
volumeMounts:
- name: config-volume
mountPath: /tmp
env:
- name: app_conf_path
value: "/tmp/file.yaml"
volumes:
- name: config-volume
configMap:
name: app.config
NOTE: This is a very interesting step. We create a volume
using the configmap
and we set the location desired to mount
that volume
. Each section of the configmap
will be a file inside that folder. Since we created it from only 1 file, it's the only file that will be mounted. We also set the ENV name
you need with the value
as the path to the file.
kubectl exec -it <POD_NAME> -- /bin/bash
to see our result:$ kubectl apply -f app-deploy.yaml
deployment.apps/my-app created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
my-app-68b5b69fc8-xxqpw 1/1 Running 0 3s
$ kubectl exec -it my-app-68b5b69fc8-xxqpw -- /bin/bash
root@my-app-68b5b69fc8-xxqpw:/# printenv | grep app_conf_path
app_conf_path=/tmp/file.yaml
root@my-app-68b5b69fc8-xxqpw:/# cat $app_conf_path
key1: value1
key2: value2
key3: value3
Now we reached the goal of your request.
Inside the pod there is a configuration file called file.yaml
with the configuration settings we used to generate the config file.
You don't have to worry about creating and maintaining the volume separately.
If you still have any question about it let me know in the comments.