【问题标题】:There's any way, in K8S, to source an env file, dynamically generated during an initcontainer?在 K8S 中,有什么方法可以获取在 initcontainer 期间动态生成的 env 文件?
【发布时间】:2021-08-16 13:54:39
【问题描述】:

我打算有一个 initcontainer 来处理一些加密内容,然后生成一个源文件以供 container 获取。 源文件将动态生成,VARS 将是动态的,这意味着我永远不会知道 VAR 名称或其内容。这也意味着我不能使用 k8s env。 文件名将始终相同。

我知道我可以从我的应用程序中更改 Dockerfile 并包含一个入口点以在运行工作负载以获取文件之前执行脚本,但是,这仍然是唯一的选择吗? 在k8s中没有办法实现这个吗?

我的 container 可以挂载 initcontainer 创建文件的目录。但它不能以某种方式获取文件?

apiVersion: v1
kind: Pod
metadata:
  name: pod-init
  namespace: default
spec:
  nodeSelector:
    env: sm
  initContainers:
    name: genenvfile
    image: busybox
    imagePullPolicy: Always
    command: ["/bin/sh"]
    # just an example, there will be a software here that will translate some encrypted stuff into VARS and then append'em to a file
    args: ["-c", "echo MYVAR=func > /tmp/sm/filetobesourced"]
    volumeMounts:
    - mountPath: /tmp/sm
      name: tmpdir
  containers:
    image: gcr.io/google.com/cloudsdktool/cloud-sdk:slim
    imagePullPolicy: IfNotPresent
    name: mypod-cm
    tty: true
    volumeMounts:
    - mountPath: /tmp/sm
      readOnly: true
      name: tmpdir
  volumes:
    name: tmpdir
    emptyDir:
      medium: Memory

我想的一步一步是:

  1. initcontainer 挂载 /tmp/sm 并生成一个名为 /tmp/sm/filetobesourced 的文件
  2. 容器挂载/tmp/sm
  3. 容器来源 /tmp/sm/filetobesourced
  4. 工作负载使用上一步获取的所有变量运行

我错过了完成第三步的事情吗?

【问题讨论】:

  • secret 是否必须与 Pod 的生命周期紧密耦合在一起(即每个新容器启动都需要一个新的 secret)?
  • 您是否尝试将生成的文件挂载为/etc/environment
  • @SYN 我相信 /etc/environment、/etc/profile、/etc/profile.d/*.sh.. 或任何其他 /etc/ 不适用于工作负载。这些是在用户登录期间执行的。对吗?
  • @congbaoguier 是的。加密的“东西”仅在 pod 的生命周期内有效。

标签: kubernetes kubernetes-pod


【解决方案1】:

将主容器上的command 和/或args 更改为更像bash -c 'source /tmp/sm/filetobesourced && exec whatevertheoriginalcommandwas'

【讨论】:

  • 感谢@coderanger 的贡献。尽管如此,我仍在寻找不会改变 Dockerfile 行为的东西。您是在要求我覆盖入口点,对吗?我不能,因为我们有一个实时环境并且使用了不同类型的入口点(+应用程序)。或者,也许我误解了你的答案。也许您可以提供一个先运行某些东西的示例,然后继续按原样执行入口点/工作负载。
  • 不,没有满足这些要求的选项。 Docker 不会以通用方式公开原始命令。
猜你喜欢
  • 2019-08-11
  • 1970-01-01
  • 2016-05-07
  • 2014-12-28
  • 2011-05-10
  • 1970-01-01
  • 1970-01-01
  • 2021-11-26
  • 1970-01-01
相关资源
最近更新 更多