首页 > 解决方案 > 在 K8S 中,有什么方法可以获取在 initcontainer 期间动态生成的 env 文件?

问题描述

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

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

我的容器可以挂载由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. 工作负载使用上一步获取的所有变量运行

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

标签: kuberneteskubernetes-pod

解决方案


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


推荐阅读