Mounting Files with ConfigMap in Kubernetes

2023-03-11 10:01 (1 years ago) ytyng

Here is how to create a ConfigMap from a local file and mount it to a Pod.

Creating a ConfigMap from a File

First, create a ConfigMap from a local file using the following command:

kubectl -n my-namespace create configmap my-config-file --from-file=../my-config-file.conf

Mounting the ConfigMap in a Deployment

Next, mount this ConfigMap in a Deployment by specifying the volumeMounts and volumes sections in your YAML file:

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: torico
spec:
  ...
  template:
    spec:
      containers:
        - name: <container-name>
          ...
          volumeMounts:
            - name: my-config-file-volume
              mountPath: /app/config/my-config-file.conf
              subPath: my-config-file.conf
      volumes:
        - name: my-config-file-volume
          configMap:
            name: my-config-file

In this configuration, only the file my-config-file.conf inside my-config-file-volume will be mounted as /app/config/my-config-file.conf.

Currently unrated

Comments

Archive

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011