kubernetes configmap set from-file in yaml configuration

14,099

Solution 1

That won't work, because kubernetes isn't aware of the local file's path. You can simulate it by doing something like this:

kubectl create configmap --dry-run=client somename --from-file=./conf/nginx.conf --output yaml

The --dry-run flag will simply show your changes on stdout, and not make the changes on the server. This will output a valid configmap, so if you pipe it to a file, you can use that:

kubectl create configmap --dry-run=client somename --from-file=./conf/nginx.conf --output yaml | tee somename.yaml

Solution 2

You could use kustomize, and it manages not only configmaps but other resources easily. I think you wanted to create configmap from a file in yaml, so you could do something like the following in a kustomization.yaml file:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
- files:
  - ./conf/nginx.conf
  name: nginx-config

Additionally, kustomize is very handy to manage all the deployments (particularly very handy for declarative management), and you can have everything in a single kustomize file as shown below:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
secretGenerator:
- envs:
  - .env
  name: my-secrets
configMapGenerator:
- files:
  - ./conf/nginx.conf
  name: nginx-config
resources:
- ./nginx-deployment.yaml

The to deploy everything you could run it like this:

$ kustomize build | kubectl apply -f -

For more information please refer here

Solution 3

Almost 3 years old question with an accepted answer, but just for those new people who are visiting.

This can be achieved with helm chart as well. If you are using the helm chart, you can put these files under files/ directory in the chart and refer these files from YAML as

{{ .Files.Get "files/filename.ext" }}

This inclusion can also be encoded based on available function in go, such as

{{ .Files.Get "files/filename.ext" | b64enc }}
Share:
14,099
Maoz Zadok
Author by

Maoz Zadok

Highly knowledgeable and creative Linux Guru and DevOps professional with extensive hands-on design and implementation experience in various Agile development environments, automation tools and processes for Continuous Integration and Deployment, Network protocols, Infrastructures and Security. Strong leadership, teamwork and project management skills “Out of the box” thinker and implementer

Updated on August 02, 2022

Comments

  • Maoz Zadok
    Maoz Zadok almost 2 years

    how can I describe this command in yaml format?

    kubectl create configmap somename --from-file=./conf/nginx.conf
    

    I'd expect to do something like the following yaml, but it doesn't work

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: somename
      namespace: default
    fromfile: ./conf/nginx.conf
    

    any idea?