Config management using Kustomize
![20210525113423](https://raw.githubusercontent.com/eliteGoblin/images/master/blog/img/picgo/20210525113423.png)
WHY
我们通过一系列的yaml来Config application deployed on Kubernetes.
我们的每个application往往需要一组K8s resource, 即一组yaml
files.
问题, 对于Kubernetes:
- 我们如何高效管理不同application的manifest: “分治法”, 不同application不同的folder, 之下是其group of manifest.
- 不同环境的manifest, 大体相似,细微config差异,如何reuse manifest, 但区别不同的环境?
在Kustomize之前,已经有很多tool来管理不同环境的manifest, 可以看出对其的需求之强, 参见Config management design doc
在Kustomize之前,我们组用ktmpl来apply manifest到不同环境; 机制是 Parameterization Templates
: 所有的环境共享一致的template, template含有不同的parameters, 不同环境定制这些parameters.
Kustomize是Kunernetes官方的config management tool, 解决第二个问题. 机制是对将可复用的manifest定义在base文件中,不同环境对其做patch
: 即base file + patch(in different env)组成最终apply的manifest.
Config组织结构:
-
base
存放common manifest -
overlays
存放不同环境的patches
1 | ├── base |
可以看出:
-
overlays
下每个folder代表一个环境:staging
,prod
- 每个folder下都有
kustomization.yaml
: 为Kustomize的配置文件.
Kustomize feature
通过filekustomization.yaml
来配置:
- Name prefix: 不同overlays给k8s resource以不同的name prefix
- Common label, annotation
- ConfigMap, secret generator: 生成ConfigMap, Secret, with hash suffix; 并自动reference.
- Diff of overlays: 以
diff
输出的形式,查看overlays
对base
的patch. - Image tag: 配置image’s Tag
- Namespace: 配置resource的namespace
Hello world
代码见这里, 示例来自Demo: hello world with variants
helloword
folder存放demo app的manifest, tree base
1 | base |
kustomize build base
会将base
下所有manifest混合在一起输出, 一般这么用: kustomize build base | k apply -f -
加入staging
和prod
的patches, in overlays
folder, 变成:
1 | ├── base |
Base’s Kustomization
base/kustomization.yaml
config common labels for all resources:
1 | commonLabels: |
我们修改为app: my-hello
, 验证是否所有base
下resource都发生了改变:
1 | sed -i.bak 's/app: hello/app: my-hello/' \ |
通过kustomize build base
可以看到效果:
![20210525104303](https://raw.githubusercontent.com/eliteGoblin/images/master/blog/img/picgo/20210525104303.png)
resources
列出base
folder包含的resource.
Overlays intro
以staging
的kustomization.yaml为例:
1 | namePrefix: staging- |
-
namePrefix
表明以staging-
prefix各resource name -
commonLabels
和commonAnnotations
类似上述base
的case -
bases
: 指明base manifest -
patchesStrategicMerge
: 以merge的形式update相应的resource:
1 | apiVersion: v1 |
-
(1)
指明了update target, name必须match.
Diff
直接看下staging
和production
的不同(同理base
与具体环境):
1 | diff \ |
1 | 3,4c3,4 |
Patches总结
Kustomize支持两种patch:
-
patchesStrategicMerge
: 给出partial object
, 对原object做Upsert
, design doc: Strategic Merge Patch JSON patch
: 根据JSON patch 标准, 对JSON object施加标准operations, 达到update的效果. 比merge更为imperative, operation有:- add
- remove
- replace
- move
- copy
JSON patch
示例见这里
json_patch
为demo folder, 下面kustomization.yaml
:
1 | resources: |
-
resources
: 待patch的manifest patches
: JSON patch文件,格式也为JSON-
path
: file path -
target
: 用来filter待update的resource, 可以一次patch多个resources
-
Verify patch diff:
1 | diff \ |
1 | 7c7 |
Note:
- JSON patch并不要求object为JSON, 也可为yaml, 见
ingress_patch.yaml
为example. -
patches
之前版本也写为patchesJson6902
相关Kustomize文档:
Reference
简洁的Intro slide
JSON Patching
Patching multiple resources at once