kustomize
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseKustomize
Kustomize
Customize Kubernetes resources declaratively without templating.
无需模板即可声明式自定义Kubernetes资源。
When to Use This Skill
使用场景
Use this skill when:
- Managing Kubernetes configs across environments
- Patching existing manifests without modification
- Creating configuration variants from bases
- Customizing third-party manifests
- Preferring declarative over templating approach
在以下场景使用该技能:
- 跨环境管理Kubernetes配置
- 无需修改现有清单即可修补资源
- 基于基础配置创建配置变体
- 自定义第三方清单
- 偏好声明式方法而非模板化方法
Prerequisites
前置条件
- kubectl 1.14+ (includes kustomize)
- Or standalone kustomize CLI
- Basic Kubernetes manifest knowledge
- kubectl 1.14+(内置kustomize)
- 或独立的kustomize CLI
- 具备基础的Kubernetes清单知识
Directory Structure
目录结构
myapp/
├── base/
│ ├── kustomization.yaml
│ ├── deployment.yaml
│ ├── service.yaml
│ └── configmap.yaml
└── overlays/
├── development/
│ ├── kustomization.yaml
│ └── replica-patch.yaml
├── staging/
│ ├── kustomization.yaml
│ └── namespace.yaml
└── production/
├── kustomization.yaml
├── replica-patch.yaml
└── resource-patch.yamlmyapp/
├── base/
│ ├── kustomization.yaml
│ ├── deployment.yaml
│ ├── service.yaml
│ └── configmap.yaml
└── overlays/
├── development/
│ ├── kustomization.yaml
│ └── replica-patch.yaml
├── staging/
│ ├── kustomization.yaml
│ └── namespace.yaml
└── production/
├── kustomization.yaml
├── replica-patch.yaml
└── resource-patch.yamlBase Configuration
基础配置
kustomization.yaml
kustomization.yaml
yaml
undefinedyaml
undefinedbase/kustomization.yaml
base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
- configmap.yaml
commonLabels:
app: myapp
commonAnnotations:
managed-by: kustomize
undefinedapiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
- configmap.yaml
commonLabels:
app: myapp
commonAnnotations:
managed-by: kustomize
undefinedBase Resources
基础资源
yaml
undefinedyaml
undefinedbase/deployment.yaml
base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:latest
ports:
- containerPort: 8080
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "128Mi"
cpu: "200m"
undefinedapiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:latest
ports:
- containerPort: 8080
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "128Mi"
cpu: "200m"
undefinedOverlays
覆盖层
Development Overlay
开发环境覆盖层
yaml
undefinedyaml
undefinedoverlays/development/kustomization.yaml
overlays/development/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namespace: myapp-dev
namePrefix: dev-
commonLabels:
environment: development
images:
- name: myapp newTag: dev-latest
undefinedapiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namespace: myapp-dev
namePrefix: dev-
commonLabels:
environment: development
images:
- name: myapp newTag: dev-latest
undefinedProduction Overlay
生产环境覆盖层
yaml
undefinedyaml
undefinedoverlays/production/kustomization.yaml
overlays/production/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namespace: myapp-prod
namePrefix: prod-
commonLabels:
environment: production
replicas:
- name: myapp count: 5
images:
- name: myapp newName: registry.example.com/myapp newTag: v2.0.0
patches:
- path: resource-patch.yaml
undefinedapiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namespace: myapp-prod
namePrefix: prod-
commonLabels:
environment: production
replicas:
- name: myapp count: 5
images:
- name: myapp newName: registry.example.com/myapp newTag: v2.0.0
patches:
- path: resource-patch.yaml
undefinedPatching
资源修补
Strategic Merge Patch
策略合并修补
yaml
undefinedyaml
undefinedoverlays/production/resource-patch.yaml
overlays/production/resource-patch.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
template:
spec:
containers:
- name: myapp
resources:
requests:
memory: "256Mi"
cpu: "500m"
limits:
memory: "512Mi"
cpu: "1000m"
undefinedapiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
template:
spec:
containers:
- name: myapp
resources:
requests:
memory: "256Mi"
cpu: "500m"
limits:
memory: "512Mi"
cpu: "1000m"
undefinedJSON Patch
JSON修补
yaml
undefinedyaml
undefinedkustomization.yaml
kustomization.yaml
patches:
- target:
kind: Deployment
name: myapp
patch: |-
- op: replace path: /spec/replicas value: 5
- op: add
path: /spec/template/spec/containers/0/env
value:
- name: LOG_LEVEL value: info
undefinedpatches:
- target:
kind: Deployment
name: myapp
patch: |-
- op: replace path: /spec/replicas value: 5
- op: add
path: /spec/template/spec/containers/0/env
value:
- name: LOG_LEVEL value: info
undefinedInline Patches
内联修补
yaml
undefinedyaml
undefinedkustomization.yaml
kustomization.yaml
patches:
- patch: |- apiVersion: apps/v1 kind: Deployment metadata: name: myapp spec: replicas: 3 target: kind: Deployment name: myapp
undefinedpatches:
- patch: |- apiVersion: apps/v1 kind: Deployment metadata: name: myapp spec: replicas: 3 target: kind: Deployment name: myapp
undefinedConfiguration Generation
配置生成
ConfigMap Generator
ConfigMap生成器
yaml
undefinedyaml
undefinedkustomization.yaml
kustomization.yaml
configMapGenerator:
- name: myapp-config
literals:
- APP_ENV=production
- LOG_LEVEL=info files:
- config.yaml envs:
- config.env options: disableNameSuffixHash: false
undefinedconfigMapGenerator:
- name: myapp-config
literals:
- APP_ENV=production
- LOG_LEVEL=info files:
- config.yaml envs:
- config.env options: disableNameSuffixHash: false
undefinedSecret Generator
Secret生成器
yaml
undefinedyaml
undefinedkustomization.yaml
kustomization.yaml
secretGenerator:
- name: myapp-secrets
literals:
- api-key=secret123 files:
- tls.crt
- tls.key type: kubernetes.io/tls
undefinedsecretGenerator:
- name: myapp-secrets
literals:
- api-key=secret123 files:
- tls.crt
- tls.key type: kubernetes.io/tls
undefinedImage Transformations
镜像转换
yaml
undefinedyaml
undefinedkustomization.yaml
kustomization.yaml
images:
Change tag
- name: myapp newTag: v2.0.0
Change registry
- name: myapp newName: registry.example.com/myapp newTag: v2.0.0
Use digest
- name: myapp digest: sha256:abc123...
undefinedimages:
修改标签
- name: myapp newTag: v2.0.0
修改镜像仓库
- name: myapp newName: registry.example.com/myapp newTag: v2.0.0
使用镜像摘要
- name: myapp digest: sha256:abc123...
undefinedResource Transformations
资源转换
Name Prefix/Suffix
名称前缀/后缀
yaml
undefinedyaml
undefinedkustomization.yaml
kustomization.yaml
namePrefix: prod-
nameSuffix: -v2
undefinednamePrefix: prod-
nameSuffix: -v2
undefinedNamespace
命名空间
yaml
undefinedyaml
undefinedkustomization.yaml
kustomization.yaml
namespace: production
undefinednamespace: production
undefinedLabels and Annotations
标签与注解
yaml
undefinedyaml
undefinedkustomization.yaml
kustomization.yaml
commonLabels:
app.kubernetes.io/name: myapp
app.kubernetes.io/environment: production
commonAnnotations:
example.com/owner: team-a
undefinedcommonLabels:
app.kubernetes.io/name: myapp
app.kubernetes.io/environment: production
commonAnnotations:
example.com/owner: team-a
undefinedReplicas
副本数
yaml
undefinedyaml
undefinedkustomization.yaml
kustomization.yaml
replicas:
- name: myapp count: 5
- name: worker count: 3
undefinedreplicas:
- name: myapp count: 5
- name: worker count: 3
undefinedComponents
组件
yaml
undefinedyaml
undefinedcomponents/monitoring/kustomization.yaml
components/monitoring/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
resources:
- servicemonitor.yaml
patches:
- patch: |- apiVersion: apps/v1 kind: Deployment metadata: name: myapp spec: template: metadata: annotations: prometheus.io/scrape: "true" prometheus.io/port: "8080"
```yamlapiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
resources:
- servicemonitor.yaml
patches:
- patch: |- apiVersion: apps/v1 kind: Deployment metadata: name: myapp spec: template: metadata: annotations: prometheus.io/scrape: "true" prometheus.io/port: "8080"
```yamloverlays/production/kustomization.yaml
overlays/production/kustomization.yaml
components:
- ../../components/monitoring
undefinedcomponents:
- ../../components/monitoring
undefinedRemote Resources
远程资源
yaml
undefinedyaml
undefinedkustomization.yaml
kustomization.yaml
resources:
Remote Git repository
Remote URL
undefinedresources:
远程Git仓库
远程URL
undefinedCommands
命令
bash
undefinedbash
undefinedBuild and view output
构建并查看输出
kubectl kustomize overlays/production
kubectl kustomize overlays/production
Apply to cluster
应用到集群
kubectl apply -k overlays/production
kubectl apply -k overlays/production
Delete resources
删除资源
kubectl delete -k overlays/production
kubectl delete -k overlays/production
View diff
查看差异
kubectl diff -k overlays/production
kubectl diff -k overlays/production
Build with standalone kustomize
使用独立kustomize构建
kustomize build overlays/production
kustomize build overlays/production
Build and apply
构建并应用
kustomize build overlays/production | kubectl apply -f -
undefinedkustomize build overlays/production | kubectl apply -f -
undefinedHelm Chart Integration
Helm Chart集成
yaml
undefinedyaml
undefinedkustomization.yaml
kustomization.yaml
helmCharts:
- name: prometheus repo: https://prometheus-community.github.io/helm-charts version: 25.0.0 releaseName: prometheus namespace: monitoring valuesFile: values.yaml includeCRDs: true
undefinedhelmCharts:
- name: prometheus repo: https://prometheus-community.github.io/helm-charts version: 25.0.0 releaseName: prometheus namespace: monitoring valuesFile: values.yaml includeCRDs: true
undefinedVariable Substitution
变量替换
yaml
undefinedyaml
undefinedkustomization.yaml
kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
replacements:
- source:
kind: ConfigMap
name: myapp-config
fieldPath: data.APP_VERSION
targets:
- select:
kind: Deployment
name: myapp
fieldPaths:
- spec.template.spec.containers.[name=myapp].image options: delimiter: ':' index: 1
- select:
kind: Deployment
name: myapp
fieldPaths:
undefinedapiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
replacements:
- source:
kind: ConfigMap
name: myapp-config
fieldPath: data.APP_VERSION
targets:
- select:
kind: Deployment
name: myapp
fieldPaths:
- spec.template.spec.containers.[name=myapp].image options: delimiter: ':' index: 1
- select:
kind: Deployment
name: myapp
fieldPaths:
undefinedCommon Issues
常见问题
Issue: Name Hash Conflicts
问题:名称哈希冲突
Problem: Resources not updating when ConfigMap changes
Solution: Enable name suffix hash (default) or use replacement
问题:ConfigMap变更后资源未更新
解决方案:启用名称后缀哈希(默认开启)或使用替换功能
Issue: Patch Not Applying
问题:修补未生效
Problem: Strategic merge patch doesn't work
Solution: Verify resource names match, use JSON patch for complex changes
问题:策略合并修补不起作用
解决方案:验证资源名称匹配,针对复杂变更使用JSON修补
Issue: Remote Resource Fails
问题:远程资源获取失败
Problem: Cannot fetch remote resources
Solution: Check URL, verify ref/tag exists, ensure network access
问题:无法获取远程资源
解决方案:检查URL,验证引用/标签存在,确保网络可访问
Issue: Label Selector Mismatch
问题:标签选择器不匹配
Problem: commonLabels breaks selectors
Solution: Use includeSelectors: false or exclude specific resources
yaml
commonLabels:
app: myapp
configurations:
- labelExclusions.yaml问题:commonLabels破坏选择器
解决方案:使用includeSelectors: false或排除特定资源
yaml
commonLabels:
app: myapp
configurations:
- labelExclusions.yamlBest Practices
最佳实践
- Keep base manifests environment-agnostic
- Use overlays for environment-specific config
- Prefer strategic merge patches for simple changes
- Use components for optional features
- Pin remote resource versions
- Enable ConfigMap/Secret hash suffixes
- Document overlay structure in README
- Test builds before applying
- 保持基础清单与环境无关
- 使用覆盖层处理环境特定配置
- 简单变更优先使用策略合并修补
- 使用组件实现可选功能
- 固定远程资源版本
- 启用ConfigMap/Secret哈希后缀
- 在README中记录覆盖层结构
- 应用前测试构建结果
Related Skills
相关技能
- kubernetes-ops - K8s fundamentals
- helm-charts - Helm alternative
- argocd-gitops - GitOps deployment
- kubernetes-ops - K8s基础操作
- helm-charts - Helm替代方案
- argocd-gitops - GitOps部署