【问题标题】:How do I copy a Kubernetes configmap to a write enabled area of a pod?如何将 Kubernetes configmap 复制到 pod 的可写区域?
【发布时间】:2019-05-15 18:21:35
【问题描述】:

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

我希望运行一个 init 容器并将 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

【问题讨论】:

    标签: docker kubernetes redis configmap


    【解决方案1】:

    根据@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 复制到可写目录中。

    注意:

    emptyDir 卷在将 Pod 分配给 Node 时首先创建,并且只要该 Pod 在该节点上运行就存在。顾名思义,它最初是空的。 Pod 中的容器都可以在 emptyDir 卷中读取和写入相同的文件,尽管该卷可以安装在每个容器中相同或不同的路径上。当 Pod 因任何原因从节点中移除时,emptyDir 中的数据将被永久删除。

    【讨论】:

    • 抱歉耽搁了,我以为这个问题已经不存在了,但它确实存在。我能够创建 init 容器并将配置复制到共享驱动器并更改 docker 映像以从该位置提取。谢谢!
    • 如果直接将 configmap 挂载到 /etc/cassandra ,该文件夹将被 configmap 的内容覆盖。您可以使用 Init 容器进行复制 - 这是一个示例 stackoverflow.com/a/60665890/429476
    【解决方案2】:

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

    【讨论】:

    • 请澄清一下,启动脚本在哪里运行,如何运行?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 2021-06-26
    • 2021-09-27
    • 2022-01-20
    • 1970-01-01
    • 2019-02-23
    • 2019-11-26
    相关资源
    最近更新 更多