Configmap for a env variable that points to a file

4/6/2020

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.

-- doc_noob
kubernetes

3 Answers

4/6/2020

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
-- Linuxian
Source: StackOverflow

4/6/2020

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
-- coderanger
Source: StackOverflow

4/6/2020

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?

I'll begin talking about the concepts of ConfigMaps:

  • ConfigMap is a dictionary of configuration settings. It consists of key-value pairs of strings.
  • ConfigMaps are useful to keep your code separate from configuration.
  • You can generate a configmap from directories, from literal values or what we want: from a file.
  • ConfigMap can be treated like a volume in kubernetes:

    • The data stored in a ConfigMap object can be referenced in a volume of type configMap 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:

  • To create a configmap you run the command:

kubectl create configmap <CONFIGMAP_NAME> --from-file=/location/file.yaml

  • You can also add more than one file to a single configmap, just repeat the --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 this file.yaml and the file.yaml is a simple yaml file with details of dbconnectionstrings and paths for other apps

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.

  • I'd like to suggest you The ITHollow ConfigMap Example. I was going to use it here but your app is already built with a function to look for the configuration file outside. I'll leave this link so you can see how you could use a ConfigMap to other apps that needs external configuration and are not hardcoded to look for it in a specific file.

Reproducible Example:

  • This will be a example to show you how to achieve the portion your question requires.

    • It will be a simple 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.

  • Now we will create the configmap called 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.

  • Let's see the configmap we created:
$ 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>
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.

  • Now let's apply it and open a shell inside the pod with 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.

-- willrof
Source: StackOverflow