How do I copy a Kubernetes configmap to a write enabled area of a pod?

5/15/2019

I am trying to deploy a redis sentinel deployment in Kubernetes. I have accomplished that but want to use ConfigMaps to allow us to change the IP address of the master in the sentinel.conf file. I started this but redis cant write to the config file because the mount point for configMaps are readOnly.

I was hoping to run an init container and copy the redis conf to a different dir just in the pod. But the init container couldn't find the conf file.

What are my options? Init Container? Something other than ConfigMap?

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: redis-sentinel
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: redis-sentinel
    spec:
      hostNetwork: true
      containers:
      - name: redis-sentinel
        image: IP/redis-sentinel
        ports:
          - containerPort: 63790
          - containerPort: 26379
        volumeMounts:
          - mountPath: /redis-master-data
            name: data
          - mountPath: /usr/local/etc/redis/conf
            name: config
      volumes:
        - name: data
          emptyDir: {}
        - name: config
          configMap:
            name: sentinel-redis-config
            items:
            - key: redis-config-sentinel
              path: sentinel.conf
-- Dylan
configmap
docker
kubernetes
redis

2 Answers

5/15/2019

Create a startup script. In that copy the configMap file that is mounted in a volume to writable location. Then run the container process.

-- P Ekambaram
Source: StackOverflow

5/17/2019

According to @P Ekambaram proposal, you can try this one:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: redis-sentinel
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: redis-sentinel
    spec:
      hostNetwork: true
      containers:
      - name: redis-sentinel
        image: redis:5.0.4
        ports:
          - containerPort: 63790
          - containerPort: 26379
        volumeMounts:
          - mountPath: /redis-master-data
            name: data
          - mountPath: /usr/local/etc/redis/conf
            name: config
      initContainers:
      - name: copy
        image: redis:5.0.4
        command: ["bash", "-c", "cp /redis-master/redis.conf /redis-master-data/"]
        volumeMounts:
        - mountPath: /redis-master
          name: config
        - mountPath: /redis-master-data
          name: data
     volumes:
     - name: data
       emptyDir: {}
     - name: config
       configMap:
         name: example-redis-config
         items:
         - key: redis-config
           path: redis.conf

In this example initContainer copy the file from ConfigMap into writable dir.

Note:

An emptyDir volume is first created when a Pod is assigned to a Node, and exists as long as that Pod is running on that node. As the name says, it is initially empty. Containers in the Pod can all read and write the same files in the emptyDir volume, though that volume can be mounted at the same or different paths in each Container. When a Pod is removed from a node for any reason, the data in the emptyDir is deleted forever.

-- Hanx
Source: StackOverflow