如何将 Kubernetes configmap 复制到 pod 的可写区域?

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

我正在尝试在 Kubernetes 中部署 redis sentinel 部署。我已经完成了,但是我想使用 ConfigMaps 来允许我们在 sentinel.conf 文件中更改主机的 IP 地址。我开始了这个但是 redis 不能写入配置文件,因为 configMaps 的挂载点是只读的。

我希望 运行 初始化容器并将 redis conf 复制到 pod 中的不同目录。但是 init 容器找不到 conf 文件。

我有哪些选择?初始化容器?除了 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

创建启动脚本。在该副本中,将卷中安装的 configMap 文件复制到可写位置。然后运行容器进程。

根据@P Ekambaram 的建议,你可以试试这个:

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

在此示例中,initContainer 将文件从 ConfigMap 复制到可写目录中。

注:

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.