https://drive.google.com/file/d/13CuKvbDSEXqsM3CFP3X0bE_ETn5QMcdf/view?usp=sharing

๐ŸŽฏ What It Is

Instead of injecting config or secrets as environment variables, you can mount them as files inside a container โ€” ideal for config files like nginx.conf, app.yaml, or SSL certificates.

โœ… Why? Many apps expect config in files, not env vars โ€” and files can auto-update when the ConfigMap/Secret changes!

๐Ÿ’ก Real-World Analogy

Like placing a configuration file (/etc/myapp/config.ini) on your server โ€” but Kubernetes manages it for you and updates it automatically.


๐Ÿงช Example: Mount a ConfigMap as an Nginx Config File

Step 1: Create a ConfigMap with a config file

# nginx-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-conf
  namespace: dev
data:
  nginx.conf: |
    server {
      listen 80;
      location / {
        return 200 "Hello from ConfigMap!\\n";
        add_header Content-Type text/plain;
      }
    }

Apply it:

kubectl apply -f nginx-config.yaml

Step 2: Mount It as a File in a Pod

# nginx-pod.yaml
apiVersion: v1
kind: Pod
meta
  name: nginx
  namespace: dev
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - name: config-volume
      mountPath: /etc/nginx/conf.d  # Nginx auto-loads .conf files here
  volumes:
  - name: config-volume
    configMap:
      name: nginx-conf

Apply and test:

kubectl apply -f nginx-pod.yaml
kubectl port-forward nginx -n dev 8080:80 &
curl <http://localhost:8080>
# Output: Hello from ConfigMap!

โœ… Bonus: If you update the ConfigMap, the file in the Pod auto-updates in ~1 minute!


โœ… Summary YAML

# Mount ConfigMap as files
volumes:
- name: config-vol
  configMap:
    name: my-config

# Mount Secret as files
volumes:
- name: secret-vol
  secret:
    secretName: my-secret

# In container
volumeMounts:
- name: config-vol
  mountPath: /etc/app/config