Many applications don’t use environment variables but instead, use one or more config files.
However, it could be challenging to override an existing file by using ConfigMap.
I will walk through replacing an existing file in a pod. For the sake of simplicity, I will replace the /etc/host.conf
file.
The config file on my computer:
➜ ~ cat host-config-file.txt
multi off
You may want to create a ConfigMap entry in an imperative way:
kubectl create configmap host-config-data --from-file=host-config-file.txt
But if you create the config map, in an imperative way, the local filename is assigned as the data key.
kubectl get configmaps host-config-data -o yaml
apiVersion: v1
data:
host-config-file.txt: |
multi off
Declarative way:
apiVersion: v1
kind: ConfigMap
metadata:
name: host-config-data
data:
data_custom_key: |-
multi off
Create the config map:
➜ ~ kubectl create -f configmap.yml
configmap/host-config-data created
➜ ~ kubectl get configmaps host-config-data -o yaml
apiVersion: v1
data:
data_custom_key: multi off
Create the deployment file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: memcached-server-deployment
spec:
replicas: 2
selector:
matchLabels:
cache: memcached
template:
metadata:
labels:
cache: memcached
spec:
containers:
- name: memcached-container
image: memcached:bullseye
volumeMounts:
- name: config-override
mountPath: /etc/host.conf
subPath: custom_path
volumes:
- name: config-override
configMap:
name: host-config-data
items:
- key: data_custom_key
path: custom_path
I used a different word for each segment so you can distinguish which parts are related to each other.
Result:
kubectl exec -it memcached-server-deployment-67858696f7-b72vn -- /bin/bash
memcache@memcached-server-deployment-67858696f7-b72vn:/$ cat /etc/host.conf
multi off