TPP*_*PPZ 6 environment-variables kubernetes kubectl configmap kustomize
我有一个由 Pod 组成的环境,它根据一个名为 、 或 的环境变量来处理CONF_ENV其test目标stage环境prod。
Pod 内运行的应用程序跨环境具有相同的源代码,配置文件是根据CONF_ENV环境变量选择的。
我将其封装CONF_ENV在*.properties文件中只是因为稍后我可能需要添加更多环境变量,但我确保每个属性文件包含预期的CONF_ENV例如:
test.properites有CONF_ENV=test,prod.properties有CONF_ENV=prod, 等等...我很难使用 Kustomize 覆盖来完成这项工作,因为我想将 a 定义ConfigMap为同一覆盖内所有 Pod 的共享资源,例如test(每个 Pod 都在自己的目录中,需要时与其他内容一起)。
所以想法是:
\nbase/(共享)与 的定义Namespace,ConfigMap(以及潜在的其他共享资源base/pod1/定义 pod1 从共享中挑选ConfigMap(默认为test,但原则上可能不同)然后是叠加:
\noverlay/testCONF_ENV=test用(例如 foroverlay/test/pod1/等)修补基础overlay/prod/CONF_ENV=prod用(例如 foroverlay/prod/pod1/等)修补基础每个目录都有自己的kustomize.yaml.
上面的方法不起作用,因为当进入 egoverlay/test/pod1/并调用命令kubectl kustomize .来检查输出 YAML 时,我会收到各种错误,具体取决于我如何定义 YAML 键bases:或resources:.
我试图ConfigMap在整个CONF_ENV环境中共享,试图通过利用 Kustomize 的修补模式来最小化样板 YAML 。
Kubernetes / Kustomize YAML 目录结构的工作方式如下:
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 base\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 configuration.yaml # I am trying to share this!\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 my_namespace.yaml # I am trying to share this!\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 my-scheduleset-etl-misc\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 my_scheduleset_etl_misc.yaml\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 my-scheduleset-etl-reporting\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 my_scheduleset_etl_reporting.yaml\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test.properties # I am trying to share this!\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 overlay\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 kustomization.yaml # here I want tell "go and pick up the shared resources in the base dir"\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 my-scheduleset-etl-misc\n \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\n \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test.properties # I\'ve tried to share this one level above, but also to add this inside the "leaf" level for a given pod\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 my-scheduleset-etl-reporting\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\nRun Code Online (Sandbox Code Playgroud)\n使用 Kustomize 的命令kubectl:
error: merging from generator &{0xc001d99530 { map[] map[]} {{ my-schedule-set-props merge {[CONF_ENV=test] [] [] } <nil>}}}: \nid resid.ResId{Gvk:resid.Gvk{Group:"", Version:"v1", Kind:"ConfigMap", isClusterScoped:false}, Name:"my-schedule-set-props", Namespace:""} \ndoes not exist; cannot merge or replace\nRun Code Online (Sandbox Code Playgroud)\nerror: loading KV pairs: env source files: [../test.properties]: \nsecurity; file \'/my/path/to/yaml/overlay/test/test.properties\' \nis not in or below \'/my/path/to/yaml/overlay/test/my-scheduleset-etl-misc\'\nRun Code Online (Sandbox Code Playgroud)\nerror: accumulating resources: accumulation err=\'accumulating resources from \'../\': \n\'/my/path/to/yaml/overlay/test\' must resolve to a file\': \ncycle detected: candidate root \'/my/path/to/yaml/overlay/test\' \ncontains visited root \'/my/path/to/yaml/overlay/test/my-scheduleset-etl-misc\'\nRun Code Online (Sandbox Code Playgroud)\nkustomization.yamlpod 目录内的覆盖文件有:
error: merging from generator &{0xc001d99530 { map[] map[]} {{ my-schedule-set-props merge {[CONF_ENV=test] [] [] } <nil>}}}: \nid resid.ResId{Gvk:resid.Gvk{Group:"", Version:"v1", Kind:"ConfigMap", isClusterScoped:false}, Name:"my-schedule-set-props", Namespace:""} \ndoes not exist; cannot merge or replace\nRun Code Online (Sandbox Code Playgroud)\nkustomization.yaml覆盖层的根部有:
error: loading KV pairs: env source files: [../test.properties]: \nsecurity; file \'/my/path/to/yaml/overlay/test/test.properties\' \nis not in or below \'/my/path/to/yaml/overlay/test/my-scheduleset-etl-misc\'\nRun Code Online (Sandbox Code Playgroud)\n基本目录kustomization.yaml包含 ConfigMap 的配置:
error: accumulating resources: accumulation err=\'accumulating resources from \'../\': \n\'/my/path/to/yaml/overlay/test\' must resolve to a file\': \ncycle detected: candidate root \'/my/path/to/yaml/overlay/test\' \ncontains visited root \'/my/path/to/yaml/overlay/test/my-scheduleset-etl-misc\'\nRun Code Online (Sandbox Code Playgroud)\n含有configuration.yaml:
bases:\n - ../ # tried with/without this to share the ConfigMap\n - ../../../base/my-scheduleset-etl-misc/\nRun Code Online (Sandbox Code Playgroud)\n我该怎么做呢?
\nConfigMap如何通过尽可能多地共享所有内容和 Pod 定义来确保最大限度地减少 YAML 数量?
lar*_*sks 13
如果我正确理解你的目标,我认为你可能把事情过于复杂化了。我认为您需要在基础中定义一个通用属性文件,但您希望覆盖覆盖层中的特定属性。这是这样做的一种方法。
\n在基地,我有:
\n$ cd base\n$ tree\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 example.properties\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 pod1\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 pod.yaml\nRun Code Online (Sandbox Code Playgroud)\n其中example.properties包含:
SOME_OTHER_VAR=somevalue\nCONF_ENV=test\nRun Code Online (Sandbox Code Playgroud)\n并kustomization.yaml包含:
resources:\n - pod1\n\nconfigMapGenerator:\n - name: example-props\n envs:\n - example.properties\nRun Code Online (Sandbox Code Playgroud)\n我定义了两个覆盖层,test并且prod:
$ cd ../overlays\n$ tree\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 prod\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 example.properties\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\nRun Code Online (Sandbox Code Playgroud)\ntest/kustomization.yaml看起来像这样:
resources:\n- ../../base\nRun Code Online (Sandbox Code Playgroud)\n它只是导入而不进行任何更改,因为目录中base的值为。CONF_ENVbasetest
prod/kustomization.yaml看起来像这样:
apiVersion: kustomize.config.k8s.io/v1beta1\nkind: Kustomization\nresources:\n- ../../base\n\nconfigMapGenerator:\n - name: example-props\n behavior: merge\n envs:\n - example.properties\nRun Code Online (Sandbox Code Playgroud)\n看起来prod/example.properties像:
CONF_ENV=prod\nRun Code Online (Sandbox Code Playgroud)\n如果我运行kustomize build overlays/test,我会得到输出:
apiVersion: v1\ndata:\n CONF_ENV: test\n SOME_OTHER_VAR: somevalue\nkind: ConfigMap\nmetadata:\n name: example-props-7245222b9b\n---\napiVersion: v1\nkind: Pod\nmetadata:\n name: example\nspec:\n containers:\n - command:\n - sleep\n - 1800\n envFrom:\n - configMapRef:\n name: example-props-7245222b9b\n image: docker.io/alpine\n name: alpine\nRun Code Online (Sandbox Code Playgroud)\n如果我运行kustomize build overlays/prod,我会得到:
apiVersion: v1\ndata:\n CONF_ENV: prod\n SOME_OTHER_VAR: somevalue\nkind: ConfigMap\nmetadata:\n name: example-props-h4b5tc869g\n---\napiVersion: v1\nkind: Pod\nmetadata:\n name: example\nspec:\n containers:\n - command:\n - sleep\n - 1800\n envFrom:\n - configMapRef:\n name: example-props-h4b5tc869g\n image: docker.io/alpine\n name: alpine\nRun Code Online (Sandbox Code Playgroud)\n也就是说,一切看起来都如您所期望的,给定 中的配置base,但我们为 提供了一个新值CONF_ENV。
您可以在这里找到所有这些文件。
\n