0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

3層にわたってoverlayするKustomize実装

Last updated at Posted at 2022-08-26

実現したいこと

例えば本番環境(prd)かつバージョン2のアプリケーションの設定値をKustomizeでoverlayして、3層にわたって設定値を適用したい
image.png

環境

Kustomize Version: v4.5.4

実装

以下ユースケースで考える
nginx DeploymentのCPU requestを本番環境では1000mにする
また本番環境にデプロイするバージョン2のアプリケーションはカナリアリリースをしたいため、replicas 1でスタートしたい

ディレクトリ構成

開発環境(dev)とversion1は空ディレクトリのみ用意してる

tree
.
├── base
│   ├── deployment.yaml..........A
│   └── kustomization.yaml.......B
├── components
│   ├── v1
│   └── v2
│       └── kustomization.yaml...C
├── dev
│   ├── base
│   ├── v1
│   └── v2
└── prd
    ├── base
    │   └── kustomization.yaml...D
    ├── v1
    └── v2
        └── kustomization.yaml...E

ファイル内容

A.

base/deployment.yaml
# 通常のDeployment
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx

B.

base/kustomization.yaml
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - deployment.yaml

C.

components/v2/kustomization.yaml
# 本番環境と開発環境共通のversion2 label付与
---
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
nameSuffix: -v2
labels:
  - pairs:
      version: v2
    includeSelectors: true

D.

prd/base/kustomization.yaml
# 本番環境ではCPU request 1000mにする
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
bases:
  - ../../base/
patches:
  - patch: |-
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: nginx
      spec:
        template:
          spec:
            containers:
              - name: nginx
                resources:
                  requests:
                    cpu: "1000m"

E.

prd/v2/kustomization.yaml
# version2はreplicas 1にする
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
bases:
  - ../base/
components:
  - ../../components/v2
replicas:
  - name: nginx
    count: 1

Kustomize実行結果

shell
kubectl kustomize prd/v2
実行結果
# CPU request 1000mとreplicas 3がどちらも適用されている
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    version: v2
  name: nginx-v2
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
      version: v2
  template:
    metadata:
      labels:
        app: nginx
        version: v2
    spec:
      containers:
      - image: nginx
        name: nginx
        resources:
          requests:
            cpu: 1000m
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?