Kubernetes 将环境变量传递给 "kubectl create"
Kubernates pass env variable to "kubectl create"
我需要将动态环境变量传递给 kubectl create
。像这样
kubectl create -f app.yaml --Target=prod
基于目标代码部署在不同的服务器上。
您可以通过两种方式实现:
使用 Helm. It is a "package manager" for Kubernetes and is built exactly for your use case (dynamic variables to configure behaviour of your resources). If it is only a single variable, "converting" your deployment is as simple as creating a new Helm chart, copy your files into templates/
, modify values.yaml
and use {{ .Values.target }}
in your templates. See the quickstart guide 更深入地介绍 Helm。
如果您认为 Helm 对于单个变量来说过于强大,请使用 kubectl
从标准输入读取的功能。您将需要一个额外的模板工具(例如 mustache)。重写您的部署以适应您的模板工具。在部署过程中创建动态 data.yml
(例如,从环境变量读取的简单 bash 脚本)和 运行 类似 mustache data.yml deployment.mustache | kubectl apply -f -
.
kubectl config set-context 允许您配置集群、命名空间、用户凭据等,并将其保存为 ~/.kube/config.
中的 "context"
您可以完全按照您在示例中使用 --Target 的方式使用 kubectl 的 --context 选项。
如果您想避免安装第 3 方插件,那么您可以使用 sed "s/orginal/change/" 替换文本。有效。我在 Jenkins shell 中使用了它。
猫app.yaml | sed "s/l3-apps/l2-apps/" | kubectl create -f -
我发布了一个命令行工具 ysed,它也可以执行您需要的操作。
我需要将动态环境变量传递给 kubectl create
。像这样
kubectl create -f app.yaml --Target=prod
基于目标代码部署在不同的服务器上。
您可以通过两种方式实现:
使用 Helm. It is a "package manager" for Kubernetes and is built exactly for your use case (dynamic variables to configure behaviour of your resources). If it is only a single variable, "converting" your deployment is as simple as creating a new Helm chart, copy your files into
templates/
, modifyvalues.yaml
and use{{ .Values.target }}
in your templates. See the quickstart guide 更深入地介绍 Helm。如果您认为 Helm 对于单个变量来说过于强大,请使用
kubectl
从标准输入读取的功能。您将需要一个额外的模板工具(例如 mustache)。重写您的部署以适应您的模板工具。在部署过程中创建动态data.yml
(例如,从环境变量读取的简单 bash 脚本)和 运行 类似mustache data.yml deployment.mustache | kubectl apply -f -
.
kubectl config set-context 允许您配置集群、命名空间、用户凭据等,并将其保存为 ~/.kube/config.
中的 "context"您可以完全按照您在示例中使用 --Target 的方式使用 kubectl 的 --context 选项。
如果您想避免安装第 3 方插件,那么您可以使用 sed "s/orginal/change/" 替换文本。有效。我在 Jenkins shell 中使用了它。
猫app.yaml | sed "s/l3-apps/l2-apps/" | kubectl create -f -
我发布了一个命令行工具 ysed,它也可以执行您需要的操作。