Score:1

How to link configmap to external file?

ke flag

With this we can create a configmap linked to external file.

kubectl create configmap database-config --from-file=database.properties

But when we edit the yaml, it will show that complete content of file is dumped there.

Is there anyway to just point to a file in yaml also so that I will keep the properties file in the same folder as the configmap yaml and appy the folder all the yamls with:

kubectl apply -f target_folder\

Please suggest.

Score:0
in flag

What configmap is

A ConfigMap is an API object used to store non-confidential data in key-value pairs. 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.

In other words when configmap is created from external file (like your case) or from other values, API server will validate if everything is correct and then will save it to etcd. This also explains reasons why when you edit your configmap you see its entire context. This happens because configmap is read from etcd, not any external sources.

This is also one of the reasons why it's not recommended to store save big files as configmaps or secrets - it will affect kubernetes cluster performance as all cluster's objects are stored within etcd.

Kustomize

This is one of the way to achieve your requirement to run one command on creating/configuring configmap based on the file in the same directory. It is a standalone tool to customize Kubernetes objects through a kustomization file.

I created a short and simple example to show the idea:

$ tree
.
├── application.properties
└── kustomization.yaml
0 directories, 2 files

$ cat kustomization.yaml 
generatorOptions:
  disableNameSuffixHash: true # this flag is used to avoid creation of new configmap, instead it will be modified when file context is changed
configMapGenerator:
- name: application-config
  files:
  - application.properties

$ cat application.properties 
listen.url=localhost:9010
client.url=some_url:3000

Test this, this command will only render configmap, it won't create it yet:

$ kubectl kustomize application.settings/
apiVersion: v1
data:
  application.properties: |
    listen.url=localhost:9010
    client.url=some_url:3000
kind: ConfigMap
metadata:
  name: application-config

Final step is to apply it:

$ kubectl apply -k application.settings/
configmap/application-config created

$ kubectl get cm
NAME                 DATA   AGE
application-config   1      23s

Short explanation to command above:

To apply those Resources, run kubectl apply with --kustomize or -k flag.

Wytrzymały Wiktor avatar
it flag
Hello @uday. Does this [answer your question](https://stackoverflow.com/help/someone-answers)?
emeraldhieu avatar
al flag
I think what the OP means is to link the config map to an external file without using any command. He wants to apply the manifest file only. There hasn't seem to be a way so far. See https://stackoverflow.com/questions/60912595/how-to-mount-the-properties-file-in-kubernetes-configmap-using-manifest-yaml
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.