扩展阅读:
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(九)使用终章
文章目录
- 在这里插入图片描述
- 1. kustomization API说明
- 2. bases
- 3. commonAnnotations
- 4. commonLabels
- 5. images
- 6. namePrefix
- 7. namespace
- 8. nameSuffix
1. kustomization API说明
2. bases
bases在v2.1.0中已弃用该字段
bases移到resonrces字段中。这使得基础(仍然是一个资源中心概念)相对于其他输入资源进行排序。
3. commonAnnotations
为所有资源添加注释,如果资源上已经存在注解键,该值将被覆盖。
cat <<EOF >./deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: EOF cat <<EOF >./kustomization.yaml namespace: my-namespace commonAnnotations: oncallPager: 800-555-1212 resources: - deployment.yaml EOF
运行kubectl kustomize ./
查看Deployment资源中设置的所有字段:
apiVersion: apps/v1 kind: Deployment metadata: annotations: oncallPager: 800-555-1212 labels: app: bingo name: dev-nginx-deployment-001 namespace: my-namespace spec: ....
4. commonLabels
为所有资源和 selectors 增加标签。如果资源上已经存在注解键,该值将被覆盖。
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 kustomization.yaml cat <<EOF >./kustomization.yaml namePrefix: dev- commonLabels: app: my-nginx resources: - deployment.yaml EOF
运行kubectl kustomize ./
查看Deployment资源中设置的所有字段:
apiVersion: apps/v1 kind: Deployment metadata: name: my-nginx spec: selector: matchLabels: run: my-nginx app: my-nginx replicas: 2 template: metadata: labels: run: my-nginx app: my-nginx spec: ....
5. images
修改镜像的名称、tag 或 image digest。
通过在kustomize .yaml的images字段中指定新的镜像来更改容器内使用的镜像。
cat <<EOF > deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: my-nginx spec: template: spec: containers: - name: my-nginx image: nginx ports: - containerPort: 80 EOF cat <<EOF >./kustomization.yaml resources: - deployment.yaml images: - name: nginx newName: my.image.registry/nginx newTag: 1.4.0 EOF
运行kubectl kustomize ./查看正在使用的镜像是否更新:
apiVersion: apps/v1 kind: Deployment metadata: name: my-nginx spec: template: spec: containers: - image: my.image.registry/nginx:1.4.0 name: my-nginx ports: - containerPort: 80
6. namePrefix
为所有资源和引用的名称添加前缀。
服务名称可能会发生更改。不建议在命令参数中硬编码服务名称。对于这种用法,Kustomize可以通过vars将服务名称注入容器
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 command: ["start", "--host", "\$(MY_SERVICE_NAME)"] EOF # Create a service.yaml file cat <<EOF > service.yaml apiVersion: v1 kind: Service metadata: name: my-nginx labels: run: my-nginx spec: ports: - port: 80 protocol: TCP selector: run: my-nginx EOF cat <<EOF >./kustomization.yaml namePrefix: dev- nameSuffix: "-001" resources: - deployment.yaml - service.yaml
运行kubectl kustomize ./查看注入容器的服务名称是dev-my-nginx-001:
apiVersion: apps/v1 kind: Deployment metadata: name: dev-my-nginx-001 spec: replicas: 2 selector: matchLabels: run: my-nginx template: metadata: labels: run: my-nginx spec: containers: - command: - start - --host - dev-my-nginx-001 image: nginx name: my-nginx
7. namespace
为所有资源添加 namespace。
如果在资源上设置了现有 namespace,则将覆盖现有 namespace;如果在资源上未设置现有 namespace,则使用现有 namespace
cat <<EOF >./deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx EOF cat <<EOF >./kustomization.yaml namespace: my-namespace resources: - deployment.yaml EOF
运行kubectl kustomize ./查看Deployment
apiVersion: apps/v1 kind: Deployment metadata: labels: app: nginx name: dev-nginx-deployment namespace: my-namespace
8. nameSuffix
为所有资源和引用的名称添加后缀。
cat <<EOF > deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: my-nginx spec: EOF # Create a service.yaml file cat <<EOF > service.yaml apiVersion: v1 kind: Service metadata: name: my-nginx spec: EOF cat <<EOF >./kustomization.yaml nameSuffix: "-001" resources: - deployment.yaml - service.yaml
运行kubectl kustomize ./
查看注入容器的服务名称是my-nginx-001
:
apiVersion: apps/v1 kind: Deployment metadata: name: my-nginx-001 spec: --- apiVersion: v1 kind: Service metadata: name: my-nginx-001 spec:
参考链接:
https://github.com/kubernetes-sigs/kustomize/blob/master/examples/zh/README.md
https://blog.csdn.net/weixin_37546425/article/details/109214976