Wener Notes

kustomize

约 2 分钟阅读
bash
# macOS
brew install kustomize

kubectl kustomize

目录结构

bash
# 应用基础目录
mkdir -p app/base app/overlays/{staging,production}

# kustomize build 生成 yaml
# 部署不同环境
kustomize build ~/ldap/overlays/staging | kubectl apply -f - --context staging
kustomize build ~/ldap/overlays/production | kubectl apply -f - --context production

# -k 可直接 apply
kubectl apply -k ~/ldap/overlays/staging --context staging
kubectl apply -k ~/ldap/overlays/production --context production
text
~/someApp - 基础应用目录
├── base - 基础层
│   ├── deployment.yaml
│   ├── kustomization.yaml
│   └── service.yaml
└── overlays - 叠加层
    ├── development - base+开发环境自定义内容
    │   ├── cpu_count.yaml
    │   ├── kustomization.yaml
    │   └── replica_count.yaml
    └── production - base+生产环境自定义内容
        ├── cpu_count.yaml
        ├── kustomization.yaml
        └── replica_count.yaml

示例

批量合并修改

kustomization.yaml

yaml
patches:
  - target:
      kind: Application
    path: patch.yaml

patch.yaml

yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  # 匹配所有
  name: '*'
spec:
  project: dev-cluster

vars/replacements

yaml
resources:
- admin.cm.yaml

replacements:
- source:
   kind: ConfigMap
   name: admin-conf
   fieldPath: data.version
 targets:
 - select:
     kind: Deployment
   fieldPaths:
   # 替换镜像版本号
   - spec.template.spec.containers.[name=admin].image
   options:
     delimiter: ":"
     index: 1

FAQ

accumulating resources: accumulation err='merging resources from 'res.yaml': may not add resource with an already registered id

检查是否已经定义过资源

index out of range [4] with length 4

YAML 语法可能没有问题,但是有 CJK 可能会导致这个异常

关联信息

反向链接、本文链接的其他页面和外部资料。

反向链接

  • K8S Awesome
    笔记 · Kustomize

    cncf landscape · awesome-cloud-native · Service Mesh / Cloud Native · GoogleCloudPlatform/scion · Google Cloud Platform 相关的 Go 项目。

References

GitHub8 条
其他外链3 条
最近更新commit 7c3df6dEdit

On this page