kustomize入门示例及基本语法使用说明

网友投稿 383 2022-11-05


kustomize入门示例及基本语法使用说明

本文对kustomize进行学习,参见文档​​GitHub​​​, ​​Document​​。

​​kustomize​​lets you customize raw, template-free YAML files for multiple purposes, leaving the original YAML untouched and usable as is.

在​​v1.14​​​之后,the kustomize build system被合到kubectl中。安装kustomize请参见​​Install​​。

一般使用base+overlays的方式来管理yaml文件,base中包含resource yaml文件以及自己的kustomization.yaml文件,overlays中包含base的变种,用来对base中的yaml文件进行修改,适应于不同的环境。

通常的文件结构如下:

~/someApp├── base│ ├── deployment.yaml│ ├── kustomization.yaml│ └── service.yaml└── overlays ├── development │ ├── cpu_count.yaml │ ├── kustomization.yaml │ └── replica_count.yaml └── production ├── cpu_count.yaml ├── kustomization.yaml └── replica_count.yaml

使用下面命令来生成YAML文件,也可以直接apply生成的YAML文件。

kustomize build ~/someApp/overlays/productionkustomize build ~/someApp/overlays/production | kubectl apply -f -

下面来讲解具体使用Kustomization.yaml来定制化k8s yaml文件。

Kustomization.yaml中通常包含3种类型的标注:

Generator用来为Kustomize提供Resource Config,像:​​bases​​​, ​​resources​​, ​​configMapGenerator​​​,​​secretGenerator​​;Transformers 用来修改Resource Config, 包括:​​images​​​,​​namespace​​​,​​namePrefix​​​,​​nameSuffix​​​,​​vars​​​,​​commonAnnotations​​​,​​commonLabels​​​,​​patchesJson6902​​​,​​patchesStrategicMerge​​;Meta 用来配置Generator和Transformers的行为,包括:​​configurations​​​, ​​generatorOptions​​。

-bases:

bases用来提供一系列包含kustomization.yaml文件的目录或git repo。

示例:

apiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationbases:- path/to/dir/with/kust/- Config file,每个文件中可定义多个resource config并使用"\n---\n"来分隔。

示例:

apiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomization# list of files containing Resource Config to addresources:- path/to/resource.yaml- another/path/to/resource.yaml

-configMapGenerator:

包含一系列需要生成的ConfigMaps。默认情况下,会将configmap的hash作为name的后缀,放在nameSuffix之后。

ConfigMap data的更改将会创建具有新name的ConfigMap,并滚动更新正在被使用的ConfigMap。PodTemplates中使用ConfigMap需要使用configMapGenerator的name,这样configmap name更新后会自动更新refer的configmap name。

通过指定generatorOptions.disableNameSuffixHash=true并将该kustomization.yaml作为base,将使得生成的configmap中不包含hash后缀。但这样就不会有滚动升级来实时更新configmap。

包含如下的参数:

behavior:定义在base中时指定merge行为,包括:create, replace, merge。env:指定生成configmap的文件,文件内容以key=value指定;files: 指定用来生成configmap的多个文件,文件名作为configmap key,文件内容作为value;literals:使用key=value对来生成configmap;name:configmap的名称,会被nameprefix和namesuffix修改;namespace:指定configmap的namespace,会被kustomize的namespace修改。

示例:

apiVersion: kustomize.config.k8s.io/v1beta1kind: KustomizationconfigMapGenerator:# generate a ConfigMap named my-java-server-props- where each file# in the list appears as a data entry (keyed by base filename).- name: my-java-server-props files: - application.properties - more.properties# generate a ConfigMap named my-java-server-env-vars- where each literal# in the list appears as a data entry (keyed by literal key).- name: my-java-server-env-vars literals: - JAVA_HOME=/opt/java/jdk - JAVA_TOOL_OPTIONS=-agentlib:hprof# generate a ConfigMap named my-system-env- where each key/value pair in the# env.txt appears as a data entry (separated by \n).- name: my-system-env env: env.txt

-secretGenerator

用来生成Secrets。跟configMapGenerator类似,secretGenerator的name后也会有hash值并在hash值变化时创建新的secret进行滚动升级,同样,可以取消。

具有configMapGenerator中所有的参数并且意义相同。同时,存在参数type来指定Secret的类型,若为"​​and "tls.crt".

示例:

apiVersion: kustomize.config.k8s.io/v1beta1kind: KustomizationsecretGenerator: # generate a tls Secret- name: app-tls files: - secret/tls.cert - secret/tls.key type: "kubernetes.io/tls"- name: env_file_secret # env is a path to a file to read lines of key=val # you can only specify one env file per secret. env: env.txt type: Opaque

-commonAnnotations

用来为所有resources添加或更新指定annotation。

示例:

apiVersion: kustomize.config.k8s.io/v1beta1kind: KustomizationcommonAnnotations: annotationKey1: "annotationValue2" annotationKey2: "annotationValue2"

-commonLabels

类似于commonAnnotations,用来为所有resource添加或更新指定label。但不同的是,label也会添加到PodTemplates中的label selector和label字段。

示例:

apiVersion: kustomize.config.k8s.io/v1beta1kind: KustomizationcommonLabels: labelKey1: "labelValue1" labelKey2: "labelValue2"

-images

用来更新所有匹配image name的[spec.template.]spec.containers.image字段中的image name和image tag。

若image名称为name:tag,digest用来替代tag来refer image的另一种方法。具有如下参数:

name 用于获取匹配image name的所有image;newName 用来替换所有匹配image的name;newTag 用来替换所有匹配image的tag;digest 用来替换所有匹配image的tag;

示例:

apiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationimages: - name: postgres newName: my-registry/my-postgres newTag: v1 - name: nginx newTag: 1.8.0 - name: my-demo-app newName: my-app - name: alpine digest: sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3

-patchesJson6902

用来根据path指定的json或yaml文件来生成target指定的k8s resource。Target中可以包含group, kind, name, namespace, version等信息,Path中指定patch file的路径。

示例:

apiVersion: kustomize.config.k8s.io/v1beta1kind: KustomizationpatchesJson6902:- target: version: v1 kind: Deployment name: my-deployment path: add_init_container.yaml- target: version: v1 kind: Service name: my-service path: add_service_annotation.yaml

其中,path字段指定的JSON patch文件,格式参见​​JSON patch​​。示例如下:

- op: add path: /some/new/path value: value - op: replace path: /some/existing/path value: new value

-patchesStrategicMerge

为匹配的resource config实施patches。推荐使用只修改单个资源的小patch。

示例:

apiVersion: kustomize.config.k8s.io/v1beta1kind: KustomizationpatchesStrategicMerge:- service_port_8888.yaml- deployment_increase_replicas.yaml- deployment_increase_memory.yaml

-namespace

用来指定所有resource的namespace,会覆盖resource中已经指定的namespace。

示例:

apiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationnamespace: "my-app-namespace"

-namePrefix/nameSuffix

用来为所有resource name指定前缀或后缀。

示例:

apiVersion: kustomize.config.k8s.io/v1beta1kind: KustomizationnamePrefix: "my-app-name-prefix-"nameSuffix: "-my-app-name-suffix"

-vars

用来定义Pod container中的参数或环境变量,使用$(MY_VAR_NAME)来使用变量。

包含如下参数:

name 变量的名称,以${name}方式使用objref Reference to the object containing the field to be referenced.fieldref Reference to the field in the object, 默认为metadata.name

示例:

apiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationvars: - name: SOME_SECRET_NAME objref: kind: Secret name: my-secret apiVersion: v1 - name: MY_SERVICE_NAME objref: kind: Service name: my-service apiVersion: v1 fieldref: fieldpath: metadata.name - name: ANOTHER_DEPLOYMENTS_POD_RESTART_POLICY objref: kind: Deployment name: my-deployment apiVersion: apps/v1 fieldref: fieldpath: spec.template.spec.restartPolicy

使用示例:

containers:- image: myimage command: ["start", "--host", "$(MY_SERVICE_NAME)"] env: - name: SECRET_TOKEN value: $(SOME_SECRET_NAME)

Meta options用来控制Kustomize怎么生成来更新resource config。包含configurations和generatorOptions。

-configurations

用来配置built-in Kustomize Transformers指定CRD过程中的行为。

-generatorOptions

用来定制化configmap/secret generator的行为。

示例:

apiVersion: kustomize.config.k8s.io/v1beta1kind: KustomizationgeneratorOptions: # labels to add to all generated resources labels: kustomize.generated.resources: somevalue # annotations to add to all generated resources annotations: kustomize.generated.resource: somevalue # disableNameSuffixHash is true disables the default behavior of adding a # suffix to the names of generated resources that is a hash of # the resource contents. disableNameSuffixHash: true

样例Kustomization.yaml参见​​Kustomization.yaml​​。

参考链接:


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java 实现增量同步和自定义同步的操作
下一篇:火车票预订查询API(火车票预订查询G2596)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~