0

我正在尝试在 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
4

2 回答 2

5

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

笔记:

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

于 2019-05-17T09:09:41.543 回答
0

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

于 2019-05-15T18:29:35.743 回答