kustomize
在kustomize出现之前,一直使用helm来管理kubernetes应用管理,这需要学会helm的特定配置语言(go template),而kustomize的出现就是为了改进这一现象。在kubernetes v1.14之后,kustomize成为kubectl内置子命令。
kustomize成为了kubernetes原生的配置管理,以无模板方式来定制应用的配置,解决了多环境配置差异的问题。
kustomize通过以下几种方式解决:
- kustomize通过Base & overlays方式维护不同环境的应用配置
- kustomize使用patch复用base胚子,并在overlay描述与base应用配置的差异部分实现资源复用
- kustomize管理kubernetes原生yaml文件,不需要额外dsl的学习成本
kustomize术语
kustomization
术语kustomization指的是kustomization.yaml文件,或者指的是包含kustomization.yaml文件的目录以及引用的相关文件路径
base
base指的是一个kustomization,任何kustomization包括overlay都可以作为另一个 kustomization 的 base,base定义了共享内容。
overlay
overlay 声明了与 base 之间的差异。通过 overlay 来维护基于 base 的不同 variants(变体),例如开发、QA 和生产环境的不同 variants。
variant
variant 是含有同一组 base 的不同 kustomization
resource
指向一个声明了 kubernetes API 对象的 YAML 文件
patch
修改文件的一般说明。文件路径,指向一个声明了 kubernetes API patch 的 YAML 文件
示例
1 | . |
2 | ├── base |
3 | │ ├── deployment.yml |
4 | │ ├── kustomization.yaml |
5 | │ └── service.yml |
6 | └── overlays |
7 | ├── dev |
8 | │ ├── env_patch.yaml |
9 | │ ├── kustomization.yaml |
10 | │ └── nodeport_patch.yaml |
11 | ├── prod |
12 | │ ├── config.yaml |
13 | │ ├── env_patch.yaml |
14 | │ ├── kustomization.yaml |
15 | │ ├── nodeport_patch.yaml |
16 | │ └── resource_patch.yaml |
17 | └── test |
18 | ├── env_patch.yaml |
19 | ├── kustomization.yaml |
20 | ├── nodeport_patch.yaml |
21 | └── resource_patch.yaml |
base
1 | commonLabels: |
2 | app: app-test |
3 | resources: |
4 | - deployment.yml |
5 | - service.yml |
1 | apiVersion: apps/v1 |
2 | kind: Deployment |
3 | metadata: |
4 | labels: |
5 | run: app-test |
6 | name: app-test |
7 | spec: |
8 | replicas: 1 |
9 | selector: |
10 | matchLabels: |
11 | run: app-test |
12 | template: |
13 | metadata: |
14 | labels: |
15 | run: app-test |
16 | spec: |
17 | containers: |
18 | - image: app-test |
19 | name: app-test |
1 | apiVersion: v1 |
2 | kind: Service |
3 | metadata: |
4 | labels: |
5 | run: app-test |
6 | name: app-test |
7 | spec: |
8 | ports: |
9 | - port: 80 |
10 | protocol: TCP |
11 | targetPort: 80 |
12 | selector: |
13 | run: app-test |
1 | ~]# kubectl kustomize base |
2 |
|
3 | apiVersion: v1 |
4 | kind: Service |
5 | metadata: |
6 | labels: |
7 | app: app-test |
8 | run: app-test |
9 | name: app-test |
10 | spec: |
11 | ports: |
12 | - port: 80 |
13 | protocol: TCP |
14 | targetPort: 80 |
15 | selector: |
16 | app: app-test |
17 | run: app-test |
18 | --- |
19 | apiVersion: apps/v1 |
20 | kind: Deployment |
21 | metadata: |
22 | labels: |
23 | app: app-test |
24 | run: app-test |
25 | name: app-test |
26 | spec: |
27 | replicas: 1 |
28 | selector: |
29 | matchLabels: |
30 | app: app-test |
31 | run: app-test |
32 | template: |
33 | metadata: |
34 | labels: |
35 | app: app-test |
36 | run: app-test |
37 | spec: |
38 | containers: |
39 | - image: app-test |
40 | name: app-test |
dev
1 | bases: |
2 | - ../../base |
3 | patchesStrategicMerge: |
4 | - nodeport_patch.yaml |
5 | - env_patch.yaml |
1 | apiVersion: apps/v1 |
2 | kind: Deployment |
3 | metadata: |
4 | name: app-test |
5 | spec: |
6 | template: |
7 | spec: |
8 | containers: |
9 | - name: app-test |
10 | env: |
11 | - name: DEBUG |
12 | value: "1" |
1 | apiVersion: v1 |
2 | kind: Service |
3 | metadata: |
4 | name: app-test |
5 | spec: |
6 | ports: |
7 | - port: 80 |
8 | protocol: TCP |
9 | targetPort: 80 |
10 | type: NodePort |
1 | kubectl kustomize dev |
2 |
|
3 | apiVersion: v1 |
4 | kind: Service |
5 | metadata: |
6 | labels: |
7 | app: app-test |
8 | run: app-test |
9 | name: app-test |
10 | spec: |
11 | ports: |
12 | - port: 80 |
13 | protocol: TCP |
14 | targetPort: 80 |
15 | selector: |
16 | app: app-test |
17 | run: app-test |
18 | type: NodePort |
19 | --- |
20 | apiVersion: apps/v1 |
21 | kind: Deployment |
22 | metadata: |
23 | labels: |
24 | app: app-test |
25 | run: app-test |
26 | name: app-test |
27 | spec: |
28 | replicas: 1 |
29 | selector: |
30 | matchLabels: |
31 | app: app-test |
32 | run: app-test |
33 | template: |
34 | metadata: |
35 | labels: |
36 | app: app-test |
37 | run: app-test |
38 | spec: |
39 | containers: |
40 | - env: |
41 | - name: DEBUG |
42 | value: "1" |
43 | image: app-test |
44 | name: app-test |
test
1 | bases: |
2 | - ../../base |
3 | patchesStrategicMerge: |
4 | - nodeport_patch.yaml |
5 | - env_patch.yaml |
6 | - resource_patch.yaml |
7 | namespace: app-test |
1 | apiVersion: apps/v1 |
2 | kind: Deployment |
3 | metadata: |
4 | name: app-test |
5 | spec: |
6 | template: |
7 | spec: |
8 | containers: |
9 | - name: app-test |
10 | env: |
11 | - name: TEST |
12 | value: "1" |
1 | apiVersion: v1 |
2 | kind: Service |
3 | metadata: |
4 | name: app-test |
5 | spec: |
6 | ports: |
7 | - port: 80 |
8 | protocol: TCP |
9 | targetPort: 80 |
10 | type: NodePort |
1 | apiVersion: apps/v1 |
2 | kind: Deployment |
3 | metadata: |
4 | name: app-test |
5 | spec: |
6 | template: |
7 | spec: |
8 | containers: |
9 | - name: app-test |
10 | resources: |
11 | limits: |
12 | cpu: 100m |
13 | memory: 100Mi |
14 | requests: |
15 | cpu: 100m |
16 | memory: 100Mi |
1 | kubctl kustomize test |
2 |
|
3 | apiVersion: v1 |
4 | kind: Service |
5 | metadata: |
6 | labels: |
7 | app: app-test |
8 | run: app-test |
9 | name: app-test |
10 | namespace: app-test |
11 | spec: |
12 | ports: |
13 | - port: 80 |
14 | protocol: TCP |
15 | targetPort: 80 |
16 | selector: |
17 | app: app-test |
18 | run: app-test |
19 | type: NodePort |
20 | --- |
21 | apiVersion: apps/v1 |
22 | kind: Deployment |
23 | metadata: |
24 | labels: |
25 | app: app-test |
26 | run: app-test |
27 | name: app-test |
28 | namespace: app-test |
29 | spec: |
30 | replicas: 1 |
31 | selector: |
32 | matchLabels: |
33 | app: app-test |
34 | run: app-test |
35 | template: |
36 | metadata: |
37 | labels: |
38 | app: app-test |
39 | run: app-test |
40 | spec: |
41 | containers: |
42 | - env: |
43 | - name: TEST |
44 | value: "1" |
45 | image: app-test |
46 | name: app-test |
47 | resources: |
48 | limits: |
49 | cpu: 100m |
50 | memory: 100Mi |
51 | requests: |
52 | cpu: 100m |
53 | memory: 100Mi |