我认为使用 "file" 方法是正确的方法和最佳解决方案,即使在使用您的测试场景时也是如此。
“正确的方式”我的意思是这就是你应该如何使用 kustomize - 将你的环境特定数据保存到单独的目录中。
kustomize 支持将整个配置存储在版本控制系统中的最佳做法。
- 在
kustomize build . 之前,您可以使用以下方法更改这些值:
kustomize edit set image foo/bar=12.5
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
...
images:
- name: foo/bar
newName: "12.5"
- 使用
envsubst 方法:
-
deployment.yaml 和 kustomization.yaml 在base 目录中:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: the-deployment
spec:
selector:
matchLabels:
run: my-nginx
replicas: 1
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: test
image: foo/bar:1.2
带有测试覆盖的目录树:
├── base
│ ├── deployment.yaml
│ └── kustomization.yaml
└── overlays
└── test
└── kustomization2.yaml
- 在
overlay/test 目录中使用变量创建新的kustomization2.yaml:
cd overlays/test
cat kustomization2.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
images:
- name: foo/bar
newTag: "${IMAGE_TAG}"
export IMAGE_TAG="2.2.11" ; envsubst < kustomization2.yaml > kustomization.yaml ; kustomize build .
output:
apiVersion: apps/v1
kind: Deployment
metadata:
...
spec:
containers:
- image: foo/bar:2.2.11
name: test
envsubst 之后目录中的文件:
.
├── base
│ ├── deployment.yaml
│ └── kustomization.yaml
└── overlays
└── test
├── kustomization2.yaml
└── kustomization.yaml
- 您可以随时将结果从
kustomize build . 传送到kubectl 以即时更改图像:
kustomize build . | kubectl set image -f - test=nginx3:4 --local -o yaml
Output:
apiVersion: apps/v1
kind: Deployment
metadata:
...
spec:
containers:
- image: nginx3:4
name: test
注意:
build in solution
来自 CLI args 或 env 变量的构建时副作用
由于要构建的其他 >arguments 或 flags 或通过咨询构建代码中的 shell 环境变量 >values 来更改 kustomize 构建配置输出,会挫败该目标。
kustomize 改为提供 kustomization 文件编辑命令。像任何 shell >命令一样,它们可以接受环境变量参数。
例如,要将图像上使用的标签设置为匹配环境变量,请运行
kustomize edit set image nginx:$MY_NGINX_VERSION
作为在 kustomize 构建之前执行的一些封装工作流程的一部分