【问题标题】:K8S deployment executing shell scripts reading configuration dataK8S部署执行shell脚本读取配置数据
【发布时间】:2018-07-02 12:57:02
【问题描述】:

在 K8S 中,在部署时执行容器 (POD) 中的脚本一次的最佳方式是什么,该脚本从作为部署的一部分的配置文件中读取,并从 mongodb 中读取一次?

我的项目由 k8s 清单文件 + 配置文件组成

我希望能够在本地更新配置文件,然后通过 kubectl 或 helm 重新部署

在 docker-compose 中,我可以在配置文件所在的目录创建一个卷,然后在命令部分执行 bash -c cmds 从卷中的配置文件中读取。这在 K8S 中如何做到最好?我不想通过 dockerfile 在镜像中包含配置文件,迫使我在通过 kubectl 或 helm 重新部署之前重建镜像

【问题讨论】:

    标签: deployment kubernetes manifest


    【解决方案1】:

    如何在 K8S 中做到最好?

    给猫剥皮有多种方法,但我的建议是:

    • 将配置保存在configMap 中,并将其安装为单独的卷。这样的地图作为 k8s 清单保存,使其所有更改与 docker build 映像分开 - 无需在映像中重建或保留敏感数据。您也可以以与configMap 相同的方式使用secret 代替(或一起使用)。
    • 使用initContainers 在主容器上线之前进行初始化,自动覆盖您的“部署后”。或者(如果 init 操作不可重复)您可以改用 Jobs 并在必要时启动它。

    这是我们在 gitlab runner 上使用的示例的摘录:

    apiVersion: apps/v1beta1
    kind: StatefulSet
    metadata:
      name: ss-my-project
    spec:
      ...
      template:
        ....
        spec:
          ...
          volumes:
          - name: volume-from-config-map-config-files
            configMap:
              name: cm-my-config-files
          - name: volume-from-config-map-script
            projected:
              sources:
              - configMap:
                  name: cm-my-scripts
                  items:
                  - key: run.sh
                    path: run.sh
                    mode: 0755
          # if you need to run as non-root here is how it is done:
          securityContext:
            runAsNonRoot: true
            runAsUser: 999
            supplementalGroups: [999]
          containers:
          - image: ...
            name: ...
            command:
            - /scripts/run.sh
            ...
            volumeMounts:
            - name: volume-from-config-map-script
              mountPath: "/scripts"
              readOnly: true
            - mountPath: /usr/share/my-app-config/config.file
              name: volume-from-config-map-config-files
              subPath: config.file
          ...
    

    您可以,ofc,从配置映射挂载多个卷或将它们组合成一个,具体取决于您的更改频率和受影响的部分。这是一个示例,两个单独安装的configMaps 只是为了说明原理(并标记脚本可执行文件),但是您可以只对所有必需的文件使用一个,将多个文件放入一个文件中或将单个文件放入每个文件中 - 根据您的需要.

    此类 configMap 的示例如下:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: cm-my-scripts
    data:
      run.sh: |
        #!/bin/bash
        echo "Doing some work here..."
    

    configMap 覆盖配置文件的例子如下:

    kind: ConfigMap
    apiVersion: v1
    metadata:
      name: cm-my-config-files
    data:
      config.file: |
         ---
         # Some config.file (example name) required in project
         # in whatever format config file actually is (just example)
         ... (here is actual content like server.host: "0" or EFG=True or whatever)
    

    configMaps 中播放单个或多个文件可以产生您想要的结果,并且根据您的需要,您可以拥有任意数量的文件。

    在 docker-compose 中,我可以在配置文件所在的目录创建一个卷,然后在命令部分执行 bash -c cmds 从卷中的配置文件中读取。

    在 k8s 中,这相当于 hostPath,但这样会严重妨碍 k8s 将 pod 调度到不同节点的能力。如果您有单节点集群(或正在开发中)以简化配置文件的更改,这可能没问题,但对于实际部署,建议使用上述方法。

    【讨论】:

    • Np,如果接受答案不麻烦,那么按照SO help 的建议,谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 2013-06-14
    • 1970-01-01
    相关资源
    最近更新 更多