Kustomize需要您指定整个资源来更改一个值吗?

huangapple go评论132阅读模式
英文:

Does Kustomize require you specify an entire resource to change one value?

问题

我理解了,Kustomize 可以解决我的Kubernetes配置管理需求,例如,如果我想在开发和测试环境中将某个资源的maxReplicas设置为3,而在生产环境中设置为7,我可以轻松实现这一点。我想会有一个基本文件,然后一个覆盖文件,只需重新指定需要更改的值。我的目标是,如果我有多个配置(配置?节点?集群?我仍然对K8s术语有些困惑。我指的是开发、测试、生产环境),如果它们共同的某个值需要更改,我可以在一个地方,即基本配置中进行更改,而不是在三个不同的配置文件中进行更改。本质上,就像在编程中一样,我希望将所有环境共有的部分提取出来。

但是我看 https://www.densify.com/kubernetes-tools/kustomize/ 得到了不同的印象。在这里,hpa.yml 文件的开发版本只是用来更改maxReplicas和averageUtilization的值。所以我认为覆盖文件应该如下所示,就像在.NET Core应用程序中,appsettings.dev.json 只需要指定它要覆盖的appsettings.json中的设置一样:

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: frontend-deployment-hpa
spec:
  maxReplicas: 2
  target:
    averageUtilization: 90

而不是给出的示例中的整个定义。所以,如果我开始时所有实例的minReplicas都为1,我想将其更改为3,我将不得不在每个覆盖文件中进行更改,而不仅仅在基本文件中进行更改。

这是正确的吗?如果是的话,是否有工具可以使配置管理按照我希望的方式工作?

英文:

I'd understood that Kustomize would be the solution to my Kubernetes configuration management needs where, for example, if I want maxReplicas for a resource to be 3 on my dev and test environments but 7 in production, I could do that easily. I imagined there'd be a base file, and then an overlay file that would respecify just the values needing changing. My goal is that if I have multiple configurations (configurations? nodes? clusters? I'm still having trouble with K8s terminology. I mean dev, test, prod.), any time a value common to all of them needed changing, I could change it in one place, the base configuration, instead of in three different config files. Essentially, just as in programming, I want to factor out what's common to all the environments.

But I'm looking at https://www.densify.com/kubernetes-tools/kustomize/ and getting a different impression. Here, the dev version of an hpa.yml file is only meant to change the values of maxReplicas and averageUtilization. So I'd thought the overlay would look as follows, in the same way that, in a .NET Core application, appsettings.dev.json only needs to specify the settings from appsettings.json that it's overriding:

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: frontend-deployment-hpa
spec:
  maxReplicas: 2
    target:
      averageUtilization: 90

instead of the whole definition that's in the example given. So, if I started with all instances having minReplicas = 1 and I wanted to change it to 3 for all of them, I'd have make that change in every overlay instead of just in the base.

Is this correct? If so, is there a tool that will allow configuration management to work as I'm looking to have it work?

答案1

得分: 3

以下是您要翻译的内容:

> Is this correct?

No.

Kustomize does not require to specify the entire resource in order to change a single value; the entire point of Kustomize is its ability to transform manifests through patches and other mechanisms to produce the desired output.

For example, assume the following layout:

. 
├── base 
│   ├── deployment.yaml 
│   └── kustomization.yaml 
└── overlays 
    ├── dev 
    │   └── kustomization.yaml 
    └── prod 
        └── kustomization.yaml 

In base/deployment.yaml we have:

apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: demo 
spec: 
  replicas: 1 
  template: 
    spec: 
      containers: 
        - name: web 
          image: docker.io/traefik/whoami:latest 

And in base/kustomization.yaml we have:

apiVersion: kustomize.config.k8s.io/v1beta1 
kind: Kustomization 
commonLabels: 
  app: kustomize-demo 

resources: 
- deployment.yaml 

If in my dev environment I want to keep replicas: 1, I would create overlays/dev/kustomization.yaml like this:

apiVersion: kustomize.config.k8s.io/v1beta1 
kind: Kustomization 
resources: 
- ../../base 

This simply includes the resources from base verbatim.

On the other hand, if in my production environment I want to run with three replicas, I would patch the Deployment resource in overlays/prod/kustomization.yaml like this:

apiVersion: kustomize.config.k8s.io/v1beta1 
kind: Kustomization 
resources: 
- ../../base 

patches: 
  - patch: | 
      apiVersion: apps/v1 
      kind: Deployment 
      metadata: 
        name: demo 
      spec: 
        replicas: 3 

This type of patch is called a "strategic merge patch"; you can also apply changes using "JSONPatch" patches. That might look like:

apiVersion: kustomize.config.k8s.io/v1beta1 
kind: Kustomization 
resources: 
- ../../base 

patches: 
  - target: 
      kind: Deployment 
      name: demo 
    patch: | 
      - op: replace 
        path: /spec/replicas 
        value: 3 

The kustomize documentation has a variety of examples of patching and other transformations. For example, the commonLabels directive I show in base/kustomization.yaml uses the labels transformer to produce this output:

apiVersion: apps/v1 
kind: Deployment 
metadata: 
  labels: 
    app: kustomize-demo 
  name: demo 
spec: 
  replicas: 1 
  selector: 
    matchLabels: 
      app: kustomize-demo 
  template: 
    metadata: 
      labels: 
        app: kustomize-demo 
    spec: 
      containers: 
      - image: docker.io/traefik/whoami:latest 
        name: web 

Notice how the labels defined in commonLabels have been applied both to:

  1. The top-level /metadata/labels element
  2. The /spec/selector/matchLabels element
  3. The /spec/template/metadata/labels element
英文:

> Is this correct?

No.

Kustomize does not require to specify the entire resource in order to change a single value; the entire point of Kustomize is its ability to transform manifests through patches and other mechanisms to produce the desired output.

For example, assume the following layout:

.
├── base
│   ├── deployment.yaml
│   └── kustomization.yaml
└── overlays
    ├── dev
    │   └── kustomization.yaml
    └── prod
        └── kustomization.yaml

In base/deployment.yaml we have:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo
spec:
  replicas: 1
  template:
    spec:
      containers:
        - name: web
          image: docker.io/traefik/whoami:latest

And in base/kustomization.yaml we have:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
commonLabels:
  app: kustomize-demo

resources:
- deployment.yaml

If in my dev environment I want to keep replicas: 1, I would create overlays/dev/kustomization.yaml like this:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base

This simply includes the resources from base verbatim.

On the other hand, if in my production environment I want to run with three replicas, I would patch the Deployment resource in overlays/prod/kustomization.yaml like this:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base

patches:
  - patch: |
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: demo
      spec:
        replicas: 3

This type of patch is called a "strategic merge patch"; you can also apply changes using "JSONPatch" patches. That might look like:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base

patches:
  - target:
      kind: Deployment
      name: demo
    patch: |
      - op: replace
        path: /spec/replicas
        value: 3

The kustomize documentation has a variety of examples of patching and other transformations. For example, the commonLabels directive I show in base/kustomization.yaml uses the labels transformer to produce this output:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: kustomize-demo
  name: demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kustomize-demo
  template:
    metadata:
      labels:
        app: kustomize-demo
    spec:
      containers:
      - image: docker.io/traefik/whoami:latest
        name: web

Notice how the labels defined in commonLabels have been applied both to:

  1. The top-level /metadata/labels element
  2. The /spec/selector/matchLabels element
  3. The /spec/template/metdata/labels element

huangapple
  • 本文由 发表于 2023年2月19日 09:04:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定