mount configmap as volume with ini files
- 2 minutes read - 309 wordsToday I spent about 1 hours in the task Deploy Lamp Stack on Kubernetes Cluster - Task. This task deepen my understanding of ini file handling and volume mounting with subpath. I think there are 2 tricky parts make the task interesting.
First, the similarity between --from-literal=variables_order=EGPCS and variables_order = "EGPCS" in php.ini make one try to create a configmap at first. However it is not the case. the actual volumemount will mount each key as a file
kubectl create configmap php-config --from-literal=variables_order=EGPCS --from-literal=variables_test=abc
# or
cat <<EOF | kubectl apply -f -
apiVersion: v1
data:
variables_order: EGPCS
kind: ConfigMap
metadata:
creationTimestamp: null
name: php-config
EOF
# the actual mouting
ls -lath
drwxr-xr-x 2 root root 4.0K Feb 5 12:56 ..2024_02_05_12_56_29.3248398402
lrwxrwxrwx 1 root root 32 Feb 5 12:56 ..data -> ..2024_02_05_12_56_29.3248398402
lrwxrwxrwx 1 root root 22 Feb 5 12:56 variables_order -> ..data/variables_order
Second, mounting confimap volume to /opt/docker/etc/php directly will lose other existing contents in the image of the containers. I thought of this immedately. However I fell into the first trap first. The mix of this one and first one made me take a while to figure out. The another troublesome part is that the incorrect configuration due to the two traps will cause the container httpd-php-container into crashloopbackoff, no way to get logs. I think that contribute to the long time I took to troubleshoot.
volumes:
- name: vol-conf
configMap:
name: php-config-cm
items:
- key: php.ini
path: php.ini
containers:
- image: webdevops/php-apache:alpine-3-php7
name: httpd-php-container
resources: {}
volumeMounts:
- name: vol-conf
mountPath: /opt/docker/etc/php/php.ini
subPath: php.ini
# the mounting
ls -lath
drwxr-xr-x 2 root root 4.0K Feb 5 12:39 ..2024_02_05_12_39_15.195875685
lrwxrwxrwx 1 root root 31 Feb 5 12:39 ..data -> ..2024_02_05_12_39_15.195875685
lrwxrwxrwx 1 root root 14 Feb 5 12:39 php.ini -> ..data/php.ini
The lessons I learnt were . How configmap work . How mountPath and subPath work together