简体   繁体   English

如何在不创建 ConfigMap 的情况下将动态生成的文件挂载到 pod 中?

[英]How can I mount a dynamically generated file into a pod without creating a ConfigMap?

In my application, I have a control plane component which spawns Jobs on my k8s cluster.在我的应用程序中,我有一个控制平面组件,它在我的 k8s 集群上生成作业。 I'd like to be able to pass in a dynamically generated (but read-only) config file to each Job.我希望能够将动态生成(但只读)的配置文件传递给每个作业。 The config file will be different for each Job.每个作业的配置文件都不同。

One way to do that would be to create, for each new Job, a ConfigMap containing the desired contents of the config file, and then set the ConfigMap as a VolumeMount in the Job spec when launching the Job.一种方法是为每个新作业创建一个包含配置文件所需内容的 ConfigMap,然后在启动作业时将 ConfigMap 设置为作业规范中的 VolumeMount。 But now I have two entities in the cluster which are semantically tied together but don't share a lifetime, ie if the Job ends, the ConfigMap won't automatically go away.但是现在我在集群中有两个实体,它们在语义上联系在一起但不共享生命周期,即如果作业结束,ConfigMap 不会自动消失。

Is there a way to directly "mount a string" into the Job's Pod, without separately creating some backing entity like a ConfigMap to store it?有没有办法直接将字符串“挂载”到 Job 的 Pod 中,而不需要单独创建一些像 ConfigMap 这样的后备实体来存储它? I could pass it in as an environment variable, I guess, but that seems fragile due to length restrictions.我想我可以将它作为环境变量传入,但由于长度限制,这似乎很脆弱。

The way that is traditionally done is via an initContainer and an emptyDir volumeMount that allows the two containers to "communicate" over a private shared piece of disk:即在传统上做的方式是经由initContaineremptyDir volumeMount允许两个容器为“通信”过盘的私人共享片:

spec:
  initContainers:
  - name: config-gen
    image: docker.io/library/busybox:latest
    command:
    - /bin/sh
    - -ec
    # now you can use whatever magick you wish to generate the config
    - |
      echo "my-config: is-generated"   > /generated/sample.yaml
      echo "some-env: ${SOME_CONFIG}" >> /generated/sample.yaml
    env:
    - name: SOME_CONFIG
      value: subject to injection like any other kubernetes env var
    volumeMounts:
    - name: shared-space
      mountPath: /generated
  containers:
  - name: main
    image: docker.example.com:1234
    # now you can do whatever you want with the config file
    command:
    - /bin/cat
    - /config/sample.yaml
    volumeMounts:
    - name: shared-space
      mountPath: /config
  volumes:
  - name: shared-space
    emptyDir: {}

If you want to work around using configmaps and environment variables for passing configuration, the options you are left with are command line arguments and configuration files .如果您想使用configmaps环境变量来传递配置,剩下的选项是命令行参数配置文件

You can pass the config as command line argument to each of your Job.您可以将配置作为命令行参数传递给您的每个作业。

You can also mount the config as file in to your Job pod.您还可以将配置作为文件挂载到您的 Job pod 中。 But since your controller which generates the config and Job which consumes it might be running on different nodes, you need a way to pass the config to the Job pod.但是由于生成配置的控制器和使用它的作业可能运行在不同的节点上,因此您需要一种将配置传递给作业 pod 的方法。 If you have a network attached storage which is accessible for all nodes, your controller can write to location on shared storage and Job can read from it.如果您有一个可供所有节点访问的网络附加存储,您的控制器可以写入共享存储上的位置,而 Job 可以从中读取。 Else, if you have a service(database, cache etc) that can act like a data store, your controller can write to the datastore and Job can read from there.否则,如果您有一个可以充当数据存储的服务(数据库、缓存等) ,您的控制器可以写入数据存储,而 Job 可以从那里读取。

If you do not want to modify your Job to read the config from various sources, you can have an initContainer which does the job of reading the configuration from a certain source and writes it to a local pod volume(emptyDir) and Job can just read from the local file.如果您不想修改您的 Job 以从各种来源读取配置,您可以使用initContainer来完成从某个来源读取配置并将其写入本地 pod 卷(emptyDir)的工作,而 Job 只能读取从本地文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不重新生成 configmap 的情况下将已生成的 ConfigMap 拉入新的 pod? - How to pull in an already generated ConfigMap to a new pod without regenerating the configmap? 如何在不清除文件夹的情况下挂载包含 ConfigMap 键/值内容的文件? - How can I mount a file with the contents of key/value of a ConfigMap without clearing the folder? 如何在不使用卷的情况下将 ConfigMap 作为文件挂载 - How to mount a ConfigMap as a file without using volume Kubernetes 卷装载 ConfigMap 到 pod 内的文件,而不覆盖所有其他文件 - Kubernetes Volume Mount ConfigMap to File within the pod without overwriting all other files 如何在 DaemonSet 中将文件挂载为 ConfigMap? - How do I mount file as ConfigMap inside DaemonSet? 如何将多个 configmap 放在 kubernetes 的同一挂载路径中? - How can I put multi configmap in the same mount path in kubernetes? 无法在运行 Sparkapplication 的 pod 上使用 configmap 挂载配置文件 - unable to mount the config file using configmap on a pod running Sparkapplication kubernetes-ConfigMap装入单个文件 - kubernetes - ConfigMap mount into single file 如何在不创建的情况下从目录生成ConfigMap? - How can I generate ConfigMap from directory without create it? 如何在不重启 pod 的情况下使对 configmap 的更改立即生效? - How to make changes to a configmap take effect immediately without restart the pod?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM