Kubernetes configMap - only one file

6/2/2017

I have a configMap created from file:

kubectl create configmap ssportal-apache-conf --from-file=ssportal.conf=ssportal.conf

and then I need to mount this file into the deployment:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: ssportal
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: ssportal
    spec:
      containers:
        - name: ssportal
          image: eu.gcr.io/my-project/ssportal:0.0.0
          ports:
          - containerPort: 80
          volumeMounts:
            - name: apache2-config-volume
              mountPath: /etc/apache2/
      volumes:
        - name: apache2-config-volume
          configMap:
            name: ssportal-apache-conf
            items:
              - key: ssportal.conf
                path: sites-enabled/ssportal.conf

But this effectively removes the existing /etc/apache2/ directory from the container and replaces it with one an only file /etc/apache2/sites-enabled/ssportal.conf.

Is it possible to overlay only one file over the existing config directory?

-- Misko
kubernetes

3 Answers

6/2/2017

Okay, it's a bit tricky. The final working YAML spec is

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: ssportal
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: ssportal
    spec:
      containers:
        - name: ssportal
          image: eu.gcr.io/my-project/ssportal:0.0.0
          command: ["sleep","120d"]
          ports:
          - containerPort: 80
          volumeMounts:
            - name: test
              mountPath: /etc/apache2/conf-enabled/test.conf
              subPath: test.conf
      volumes:
        - name: test
          configMap:
            name: sstest

and configMap creation steps:

echo "# comment" > test.conf
kubectl create configmap sstest --from-file=test.conf=test.conf 
-- Misko
Source: StackOverflow

6/2/2017

Yes. In volumeMounts set subPath: ssportal.conf and mountPath: /etc/apache2/ssportal.conf. You may remove the items: ... too.

Read more here: https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath

-- Janos Lenart
Source: StackOverflow

3/7/2018

This works for me too. Useful in case you have more than one file inside the configmap.

            volumeMounts:
            - name: test
              mountPath: /etc/apache2/conf-enabled/test.conf
              subPath: test.conf
      volumes:
        - name: test
          configMap:
            name: test
            - key: test.conf
              path: test.conf
-- Cesar Gomez Velazquez
Source: StackOverflow