kustomize (七) patches、patchesJson6902、patchesStrategicMerge详解

简介: kustomize (七) patches、patchesJson6902、patchesStrategicMerge详解

文章目录

1. patches介绍

1.1 patches示例

2. patches添加

3. patches替换

3.1 通过Kustomization 直接编写替换/新增资源清单属性

3.2 如果通过.yaml 文件进行修改/新增资源清单属性

4. patchesJson6902

5. patchesStrategicMerge

6. Target 选择

1. patches介绍

kustomization.yaml 支持通过 Strategic Merge PatchJSON patch来自定义资源。自 3.1.0 起,一个 patch 可以修改多个资源。

这可以通过指定 patch 和它所修改的 target 来完成,如下所示:

patches:
- path: <PatchFile>
  target:
    group: <Group>
    version: <Version>
    kind: <Kind>
    name: <Name>
    namespace: <Namespace>
    labelSelector: <LabelSelector>
    annotationSelector: <AnnotationSelector>

|

op: [add,replace]

替换 /新增的方式有三种

- op: replace
path: /metadata/name
value: beautiful-country-bigdata

labelSelectorannotationSelector 都应遵循 label selector 中的约定。Kustomize 选择匹配target中所有字段的目标来应用 patch 。

1.1 patches示例

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
patches:
- path: patch.yaml
  target:
    group: apps
    version: v1
    kind: Deployment
    name: deploy.*
    labelSelector: "env=dev"
    annotationSelector: "zone=west"
- patch: |-
    - op: replace
      path: /some/existing/path
      value: new value
  target:
    kind: MyKind
    labelSelector: "env=dev"

2. patches添加

下面的示例展示了如何为所有部署资源注入 sidecar 容器。

创建一个包含 Deployment 资源的 kustomization 。

DEMO_HOME=$(mktemp -d)
cat <<EOF >$DEMO_HOME/kustomization.yaml
resources:
- deployments.yaml
EOF
cat <<EOF >$DEMO_HOME/deployments.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy1
spec:
  template:
    metadata:
      labels:
        old-label: old-value
    spec:
      containers:
        - name: nginx
          image: nginx
          args:
          - one
          - two
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy2
spec:
  template:
    metadata:
      labels:
        key: value
    spec:
      containers:
        - name: busybox
          image: busybox
EOF

声明 Strategic Merge Patch 文件以注入 sidecar 容器:

cat <<EOF >$DEMO_HOME/patch.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: not-important
spec:
  template:
    spec:
      containers:
        - name: istio-proxy
          image: docker.io/istio/proxyv2
          args:
          - proxy
          - sidecar
EOF

在 kustomization.yaml 中添加 patches 字段

cat <<EOF >>$DEMO_HOME/kustomization.yaml
patches:
- path: patch.yaml
  target:
    kind: Deployment
EOF

运行 kustomize build $DEMO_HOME,可以在输出中确认两个 Deployment 资源都已正确应用。

test 2 ==  $(kustomize build $DEMO_HOME | grep "image: docker.io/istio/proxyv2" | wc -l);  echo $?

输出如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy1
spec:
  template:
    metadata:
      labels:
        old-label: old-value
    spec:
      containers:
      - args:
        - proxy
        - sidecar
        image: docker.io/istio/proxyv2
        name: istio-proxy
      - args:
        - one
        - two
        image: nginx
        name: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy2
spec:
  template:
    metadata:
      labels:
        key: value
    spec:
      containers:
      - args:
        - proxy
        - sidecar
        image: docker.io/istio/proxyv2
        name: istio-proxy
      - image: busybox
        name: busybox

3. patches替换

3.1 通过Kustomization 直接编写替换/新增资源清单属性

deployment.yamlservice.yaml

cat <<EOF > deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: java
  name: java
spec:
  selector:
    matchLabels:
      app: java
  template:
    metadata:
      labels:
        app: java
    spec:
      containers:
      - image: java
        name: java
        ports:
        - containerPort: 8080
          name: web
EOF
cat <<EOF > service.yaml
apiVersion: v1
kind: Service
metadata:
  name: java
spec:
  selector:
    app: java
  ports:
  - name: http
    port: 8001
    targetPort: 8001
EOF
cat <<EOF > kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ./deployment.yaml
- ./service.yaml
commonLabels:
  app: bigdata
images:
- name: java
  newName: registry.cn-qingdao.aliyuncs.com/nqkj-snapshot/sky-bigdata
  newTag: develop-be43cc32
patches:
- patch: |
    - op: replace
      path: /metadata/name
      value: bigdata
    - op: replace
      path: /spec/template/spec/containers/0/name
      value: bigdata
    - op: replace
      path: /spec/template/spec/containers/0/ports/0/containerPort
      value: 8001
  target:
    group: apps
    kind: Deployment
    version: v1
- patch: |
    - op: replace
      path: /metadata/name
      value: bigdata
  target:
    kind: Service
EOF

运行kubectl kustomize ./

apiVersion: v1
kind: Service
metadata:
  labels:
    app: bigdata
  name: bigdata
spec:
  ports:
  - name: http
    port: 8001
    targetPort: 8001
  selector:
    app: bigdata
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: bigdata
  name: bigdata
spec:
  selector:
    matchLabels:
      app: bigdata
  template:
    metadata:
      labels:
        app: bigdata
    spec:
      containers:
        image: registry.cn-qingdao.aliyuncs.com/nqkj-snapshot/sky-bigdata:develop-ad8ed411
        name: bigdata
        ports:
        - containerPort: 8001
          name: web

3.2 如果通过.yaml 文件进行修改/新增资源清单属性

准备patch.yaml,内容如下

cat <<EOF>patch.yaml
- op: replace
  path: /metadata/name
  value: oauth-server
- op: replace
  path: /spec/template/spec/containers/0/name
  value: oauth-server
EOF
cat <<EOF > kustomization.yaml
resources:
- ./deployment.yaml
- ./service.yaml
commonLabels:  # 标签
  app: oauth-server
images:
- name: java
  newName: my-registry/my-postgres
patches:
  - path: patch.yaml
    target:
      group: apps
      kind: Deployment
      version: v1
EOF

kustomize查看编排结果

$ kustomize build .
apiVersion: v1
kind: Service
metadata:
  labels:
    app: oauth-server
  name: java
spec:
  ports:
  - name: http
    port: 8001
    targetPort: 8001
  selector:
    app: oauth-server
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: oauth-server
  name: oauth-server
spec:
  selector:
    matchLabels:
      app: oauth-server
  template:
    metadata:
      labels:
        app: oauth-server
    spec:
      containers:
      - image: my-registry/my-postgres
        name: oauth-server
        ports:
        - containerPort: 8080
          name: web

4. patchesJson6902

通过json 文件的方式替换/新增资源清单的属性值

patchesJson6902 不光可以使用json文件也是可以使用yaml文件与上面的patches如出一辙


注意: 在通过target: 匹配资源清单的同时必须加入target>name: 属性,属性值为Deployment> 的name,这是与patches的区别


我们还是使用patches所使用的deployment.yaml和service.yaml 作为基础模版文件


准备 patch.json 和 patch-svc.json

cat <<EOF >patch.json
[
    { "op": "replace", "path": "/metadata/name", "value": "oauth-server" },
    { "op": "add", "path": "/spec/template/spec/containers/0/name", "value": "oauth-server"}
]
EOF
cat <<EOF >patch-svc.json
[
  { "op": "replace", "path": "/metadata/name", "value": "oauth-server" }
]
EOF
cat <<EOF > kustomization.yaml
resources:
  - ../../../template
commonLabels:  # 标签
  app: oauth-server
images:
- name: java
  newName: my-registry/my-postgres
patchesJson6902:
  - path: patch.json
    target:
      group: apps
      kind: Deployment
      version: v1
      name: java
  - path: patch-svc.json # 指定json
    target:
      version: v1
      kind: Service
      name: java
EOF

kustomize查看编排结果

$ kustomize build .
apiVersion: v1
kind: Service
metadata:
  labels:
    app: oauth-server
  name: oauth-server
spec:
  ports:
  - name: http
    port: 8001
    targetPort: 8001
  selector:
    app: oauth-server
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: oauth-server
  name: oauth-server
spec:
  selector:
    matchLabels:
      app: oauth-server
  template:
    metadata:
      labels:
        app: oauth-server
    spec:
      containers:
      - image: my-registry/my-postgres
        name: oauth-server
        ports:
        - containerPort: 8080
          name: web

5. patchesStrategicMerge

通过.yaml 文件的方式为要生成的资源清单定义补丁


注意补丁的yaml 文件的name,要跟模版清单一致,下面是deployment.yaml模版清单,increase_replicas.yaml和set_memory.yaml为补丁。在kustomization声明引用补丁文件,最后合并成带补丁的资源清单。

cat <<EOF > deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  selector:
    matchLabels:
      run: my-nginx
  replicas: 2
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx
        ports:
        - containerPort: 80
EOF
# Create a patch increase_replicas.yaml
cat <<EOF > increase_replicas.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  replicas: 3
EOF
# Create another patch set_memory.yaml
cat <<EOF > set_memory.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  template:
    spec:
      containers:
      - name: my-nginx
        resources:
        limits:
          memory: 512Mi
EOF
cat <<EOF >./kustomization.yaml
resources:
- deployment.yaml
patchesStrategicMerge:
- increase_replicas.yaml
- set_memory.yaml
EOF

kustomize查看编排结果

$ kustomize build .
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      run: my-nginx
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - image: nginx
        limits:
          memory: 512Mi
        name: my-nginx
        ports:
        - containerPort: 80

6. Target 选择

选择名称与 name* 匹配的资源

target:
  name: name*

选择所有 Deployment 资源

target:
  kind: Deployment

选择 label 与 app=hello 匹配的资源

target:
  labelSelector: app=hello

选择 annotation 与 app=hello 匹配的资源

target:
  annotationSelector: app=hello

选择所有 label 与 app=hello 匹配的 Deployment 资源

target:
  kind: Deployment
  labelSelector: app=hello

更多细节:

https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/strategic-merge-patch.md


kustomize API 使用手册


扩展阅读:


kustomize (一) 管理yaml部署入门hello world

kustomize (二) ConfigMap的生成和滚动更新

kustomize (三) devops和开发配合管理配置数据behavior: merge、namePrefix、nameSuffix

kustomize (四) generatorOptions详解

kustomize (五) 使用vars将 k8s runtime数据注入容器

kustomize(六)命令行常用编排

kustomize (七)patches、patchesJson6902、patchesStrategicMerge详解

kustomize (八)生成secret

kustomize(九)使用终章


相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
相关文章
|
2月前
|
前端开发 开发者
@workspace 使用指南
我是一位前端开发工程师,使用通义灵码@workspace进行项目了解、问题解答及代码优化建议,效率提升80%。通过询问项目赔付方式、架构信息和特定文件的优化建议,快速掌握项目细节,极大提高开发效率。
|
3月前
|
机器学习/深度学习 存储 算法
latent space
【9月更文挑战第23天】
70 7
|
Java
image-comparison
image-comparison
104 0
|
存储 JSON 缓存
译 | Packages as layers, not groups
译 | Packages as layers, not groups
74 0
|
数据可视化 Java
CiteSpace的安装
CiteSpace的安装
266 0
|
存储
Creating a workspace:创建一个工作空间
Creating a workspace:创建一个工作空间
324 0
Creating a workspace:创建一个工作空间
|
开发工具
Warning! This package referenced a Flutter repository via the .packages file that is no longer avail
Warning! This package referenced a Flutter repository via the .packages file that is no longer avail
127 0
Warning! This package referenced a Flutter repository via the .packages file that is no longer avail
安装@parcel/transformer-image注意的问题
安装@parcel/transformer-image注意的问题
安装@parcel/transformer-image注意的问题
|
JSON jenkins 持续交付
成功解决mxnet.base.MXNetError: C:\Jenkins\workspace\mxnet-tag\mxnet\3rdparty\dmlc-core\src\io\local_file
成功解决mxnet.base.MXNetError: C:\Jenkins\workspace\mxnet-tag\mxnet\3rdparty\dmlc-core\src\io\local_file
成功解决mxnet.base.MXNetError: C:\Jenkins\workspace\mxnet-tag\mxnet\3rdparty\dmlc-core\src\io\local_file