【问题标题】:kustomize: how to pass `newTag` from command linekustomize:如何从命令行传递`newTag`
【发布时间】:2021-07-09 15:34:22
【问题描述】:

我正在使用https://kustomize.io/,下面是我的kustomization.yaml 文件,

我有多个 docker 镜像,并且在部署期间都有相同的 tag。我可以手动更改所有tag 值,我可以通过命令提示符运行kubectl apply -k .

问题是,我不想手动更改此文件,我想在kubectl apply -k . 命令中将tag 值作为命令行参数发送。有没有办法做到这一点?谢谢。

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
images:
- name: foo/bar
  newTag: "36"
- name: zoo/too
  newTag: "36"

【问题讨论】:

  • 我现在没有时间测试它,但也许你可以通过一些技巧来使用 ENV vars。检查github.com/kubernetes-sigs/kustomize/issues/2301
  • 谢谢@whites11。我没有完全明白。只要您有时间,请建议在我的情况下如何做。欣赏

标签: kubernetes kustomize


【解决方案1】:

我认为使用 "file" 方法是正确的方法和最佳解决方案,即使在使用您的测试场景时也是如此。

正确的方式我的意思是这就是你应该如何使用 kustomize - 将你的环境特定数据保存到单独的目录中。

kustomize 支持将整个配置存储在版本控制系统中的最佳做法。

  1. 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"

  1. 使用envsubst 方法:
  • deployment.yamlkustomization.yamlbase 目录中:
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
  1. 您可以随时将结果从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 构建之前执行的一些封装工作流程的一部分

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    相关资源
    最近更新 更多