はじめに
Kustomizeのbaseディレクトリにあるリソースを特定のoverlaysの環境からのみ削除する方法を紹介します。
背景
Kustomizeを使っていて、基本的にはどの環境にもデプロイするリソースはbase配下にマニフェストを配置しますが、ある特定の環境のみbaseで用意したリソースをデプロイしたくない状況がありました。
例えば、下記のようなディレクトリ構成でbase配下にDeploymentとHPAのマニフェストを配置していますが、overlays/devの環境にはHPAをデプロイしたくないような場合です。
1.
2├── base/
3│ ├── deployment.yaml
4│ ├── hpa.yaml
5│ └── kustomization.yaml
6└── overlays/
7 ├── dev/ # dev環境はHPAをデプロイしたくない
8 ├── stg/
9 └── prod/こういった状況でどうやってbase配下のリソースをデプロイしないようにするかの方法を紹介します。
特定のoverlayだけbaseのリソースを削除する
まず、削除したい環境のoverlays配下に下記のような$patch: deleteを含めたパッチファイルを用意します。
1$patch: delete
2apiVersion: v1
3kind: Pod
4metadata:
5 name: pod-name用意したパッチファイルを対象の環境のoverlays配下のkustomization.yamlで下記のようにpatchesStrategicMergeで指定します。
1...
2
3patchesStrategicMerge:
4 - delete-pod-base.yaml
5
6...試してみる
実際に試してみます。
用意するファイルとディレクトリ構成は下記の通りです。
1.
2├── base
3│ ├── deployment.yaml
4│ ├── hpa.yaml
5│ └── kustomization.yaml
6└── overlays
7 ├── prod
8 │ ├── deployment.yaml
9 │ ├── hpa.yaml
10 │ └── kustomization.yaml
11 └── stg
12 ├── delete-hpa-base.yaml
13 ├── deployment.yaml
14 └── kustomization.yamlbaseにあるHPAをstg環境にデプロイしないようにします。
base
base配下のファイルはそれぞれ下記の通りです。
kustomization.yaml
1apiVersion: kustomize.config.k8s.io/v1beta1
2kind: Kustomization
3
4resources:
5- deployment.yaml
6- hpa.yamldeployment.yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: nginx
5spec:
6 selector:
7 matchLabels:
8 run: nginx
9 template:
10 metadata:
11 labels:
12 run: nginx
13 spec:
14 containers:
15 - name: nginx
16 image: nginxhpa.yaml
1apiVersion: autoscaling/v2
2kind: HorizontalPodAutoscaler
3metadata:
4 name: nginx-hpa
5spec:
6 scaleTargetRef:
7 apiVersion: apps/v1
8 kind: Deployment
9 name: nginx
10 minReplicas: 1
11 maxReplicas: 3
12 metrics:
13 - type: Resource
14 resource:
15 name: cpu
16 target:
17 type: Utilization
18 averageUtilization: 50stg
overlays/stg配下のファイルはそれぞれ下記の通りです。
overlays/stg/kustomization.yaml
1apiVersion: kustomize.config.k8s.io/v1beta1
2kind: Kustomization
3namespace: app-stg
4
5resources:
6- ./../../base
7
8patchesStrategicMerge:
9 - deployment.yaml
10 - delete-hpa-base.yamloverlays/stg/deployment.yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: nginx
5spec:
6 template:
7 spec:
8 containers:
9 - name: nginx
10 resources:
11 limits:
12 cpu: 200m
13 requests:
14 cpu: 200moverlays/stg/delete-hpa-base.yaml
1$patch: delete # 削除
2apiVersion: autoscaling/v2
3kind: HorizontalPodAutoscaler
4metadata:
5 name: nginx-hpaprod
overlays/prod配下のファイルはそれぞれ下記の通りです。
overlays/prod/kustomization.yaml
1apiVersion: kustomize.config.k8s.io/v1beta1
2kind: Kustomization
3namespace: app-prod
4
5resources:
6- ./../../base
7
8patchesStrategicMerge:
9 - deployment.yaml
10 - hpa.yamloverlays/prod/deployment.yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: nginx
5spec:
6 template:
7 spec:
8 containers:
9 - name: nginx
10 resources:
11 limits:
12 cpu: 500m
13 requests:
14 cpu: 500moverlays/prod/hpa.yaml
1apiVersion: autoscaling/v2
2kind: HorizontalPodAutoscaler
3metadata:
4 name: nginx-hpa
5spec:
6 minReplicas: 1
7 maxReplicas: 10ビルドしてみる
まずは特にbaseのリソースを削除していないoverlays/prodからビルドしてみます。
1kustomize build overlays/prod --output resource-prod.yamlビルドされたファイルを見ると、DeploymentとHPAのマニフェストが含まれていることが確認できます。
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: nginx
5 namespace: app-prod
6spec:
7 selector:
8 matchLabels:
9 run: nginx
10 template:
11 metadata:
12 labels:
13 run: nginx
14 spec:
15 containers:
16 - image: nginx
17 name: nginx
18 resources:
19 limits:
20 cpu: 500m
21 requests:
22 cpu: 500m
23---
24apiVersion: autoscaling/v2
25kind: HorizontalPodAutoscaler
26metadata:
27 name: nginx-hpa
28 namespace: app-prod
29spec:
30 maxReplicas: 10
31 metrics:
32 - resource:
33 name: cpu
34 target:
35 averageUtilization: 50
36 type: Utilization
37 type: Resource
38 minReplicas: 1
39 scaleTargetRef:
40 apiVersion: apps/v1
41 kind: Deployment
42 name: nginx次に、overlays/stg/delete-hpa-base.yamlで$patch: deleteを追記したパッチファイルを含んだoverlays/stgをビルドしてみます。
1kustomize build overlays/stg --output resource-stg.yaml$patch: deleteを含んでいたリソースはビルドされたファイルには含まれていないことが確認できます。
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: nginx
5 namespace: app-stg
6spec:
7 selector:
8 matchLabels:
9 run: nginx
10 template:
11 metadata:
12 labels:
13 run: nginx
14 spec:
15 containers:
16 - image: nginx
17 name: nginx
18 resources:
19 limits:
20 cpu: 200m
21 requests:
22 cpu: 200m参考
- Is it possible to remove a whole resource from a manifest? · Issue #1593 · kubernetes-sigs/kustomize
- kubernetes - Exclude Resource in kustomization.yaml - Stack Overflow
- kubernetes - Remove resource from Kustomize base? - DevOps Stack Exchange




