0
0
Kubernetesdevops~5 mins

Using ConfigMaps as mounted volumes in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your application needs configuration files that can change without rebuilding the app. ConfigMaps let you store these files separately and mount them inside your app's container as files.
When you want to provide configuration files to your app without baking them into the container image.
When you need to update configuration files without restarting or rebuilding your app container.
When multiple pods need to share the same configuration files consistently.
When you want to separate app code from environment-specific settings.
When you want to keep configuration files versioned and managed by Kubernetes.
Config File - configmap-volume-pod.yaml
configmap-volume-pod.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: example-config
data:
  app.properties: |
    color=blue
    size=large
---
apiVersion: v1
kind: Pod
metadata:
  name: configmap-volume-pod
spec:
  containers:
  - name: app-container
    image: busybox
    command: ["sleep", "3600"]
    volumeMounts:
    - name: config-volume
      mountPath: /etc/config
  volumes:
  - name: config-volume
    configMap:
      name: example-config

This file has two parts separated by ---:

ConfigMap: Stores a file named app.properties with two settings.

Pod: Runs a simple container that mounts the ConfigMap as a volume at /etc/config. The app.properties file will appear there.

Commands
This command creates the ConfigMap and the Pod that mounts it as a volume. It sets up the configuration file inside the container.
Terminal
kubectl apply -f configmap-volume-pod.yaml
Expected OutputExpected
configmap/example-config created pod/configmap-volume-pod created
This command checks inside the running pod to see the contents of the mounted configuration file, verifying the ConfigMap is correctly mounted.
Terminal
kubectl exec configmap-volume-pod -- cat /etc/config/app.properties
Expected OutputExpected
color=blue size=large
This command lists pods to confirm the pod is running after creation.
Terminal
kubectl get pods
Expected OutputExpected
NAME READY STATUS RESTARTS AGE configmap-volume-pod 1/1 Running 0 10s
Key Concept

If you remember nothing else from this pattern, remember: ConfigMaps let you provide configuration files to your app by mounting them as files inside the container.

Common Mistakes
Not specifying the correct ConfigMap name in the pod volume definition.
The pod will fail to mount the volume because it cannot find the ConfigMap, causing errors or missing files.
Ensure the volume's configMap name exactly matches the ConfigMap resource name.
Trying to mount a ConfigMap volume to a path that already has files in the container image.
The mount hides the original files, which can cause the app to fail if it expects those files.
Mount ConfigMap volumes to empty or dedicated directories inside the container.
Summary
Create a ConfigMap with configuration data as files.
Define a pod that mounts the ConfigMap as a volume at a specific path.
Verify the pod is running and the configuration files are accessible inside the container.