๐Ÿ”ง DevOps

Kustomize

Kustomize

A Kubernetes configuration management tool. Per-environment configuration through YAML patches.

Detailed Explanation

Kustomize is a Kubernetes-native configuration management tool that manages environment-specific configurations through YAML patching, without templates. Because it is built into kubectl, it can be used right away with the `kubectl apply -k` command, without separate installation. It was developed by Google and is maintained by Kubernetes SIG-CLI.

The core philosophy of Kustomize is "Don't Repeat Yourself." Common resources are defined in the base directory, and only the differences for each environment (dev, staging, prod) are patched in the overlays directory. Because the original YAML is not modified directly, tracking changes is easy, and drift between environments is prevented.

Its main features include bulk renaming of resources with namePrefix/nameSuffix, automatic generation of ConfigMaps/Secrets with configMapGenerator/secretGenerator, overriding container image tags with images, and fine-grained YAML modification with patchesStrategicMerge/patchesJson6902.

It is often compared with Helm: Helm injects values based on templates, while Kustomize applies patches based on layers. Helm is advantageous for deploying complex charts, while Kustomize is more intuitive for applying environment-specific variations to existing YAML. GitOps tools such as ArgoCD and Flux support Kustomize natively.

Code Example

# ๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ:
# app/
# โ”œโ”€โ”€ base/
# โ”‚   โ”œโ”€โ”€ kustomization.yaml
# โ”‚   โ”œโ”€โ”€ deployment.yaml
# โ”‚   โ””โ”€โ”€ service.yaml
# โ””โ”€โ”€ overlays/
#     โ”œโ”€โ”€ dev/
#     โ”‚   โ””โ”€โ”€ kustomization.yaml
#     โ””โ”€โ”€ prod/
#         โ”œโ”€โ”€ kustomization.yaml
#         โ””โ”€โ”€ replica-patch.yaml

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

# base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: api-server
  template:
    spec:
      containers:
        - name: api
          image: api-server:latest
          resources:
            requests:
              memory: "256Mi"
              cpu: "100m"

---
# overlays/prod/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - ../../base
namespace: production
namePrefix: prod-
images:
  - name: api-server
    newName: registry.company.com/api-server
    newTag: v1.2.3
configMapGenerator:
  - name: app-config
    literals:
      - LOG_LEVEL=warn
      - DB_POOL_SIZE=20
patchesStrategicMerge:
  - replica-patch.yaml

# overlays/prod/replica-patch.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-server
spec:
  replicas: 5
  template:
    spec:
      containers:
        - name: api
          resources:
            requests:
              memory: "1Gi"
              cpu: "500m"
            limits:
              memory: "2Gi"
              cpu: "1000m"

# ์ ์šฉ: kubectl apply -k overlays/prod/

How It's Said in Practice

DevOps: "We kept copy-pasting YAML for each environment, and the staging and prod configurations ended up diverging and caused an outage. Let's restructure with Kustomize."

Developer: "We can put the common parts in base and patch only things like replicas and resource limits per environment in overlays."

DevOps: "Yes, and it would be good to preview the final YAML with kustomize build and show the diff during PR review."

Interviewer: "When do you choose Helm, and when do you choose Kustomize?"

Candidate: "Helm is suitable for charts that need complex conditional logic or loops, while Kustomize is simpler when you only apply environment-specific variations to existing YAML manifests. We also use an approach of installing a third-party Helm chart and then customizing it with Kustomize."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

Reviewer: "You're aware that using configMapGenerator appends a hash to the ConfigMap name, so a rolling update is triggered automatically when it changes, right?"

Developer: "Ah, so that's why a new ConfigMap is created every time. I'll set disableNameSuffixHash in generatorOptions."

Cautions

Learn More