Video Thumbnail for Lesson
12.2: Kustomize

Kustomize

Kustomize is built into kubectl and lets us maintain a common set of manifests while tweaking only the fields that differ between environments. The example configuration lives in the kustomize directory.

Directory Layout

kustomize/
  base/
    api-golang/
      Deployment.yaml
      IngressRoute.yaml
      Service.yaml
    ...
  staging/
    api-golang/
      patches/
        Deployment.yaml
        IngressRoute.replace-host.yaml
    kustomization.yaml
  production/
    ...
Taskfile.yaml

The base folder contains complete manifests for each service. Staging and production overlays reference the base and apply small patches.

A production patch might update the replica count and image tag:

# production/api-golang/patches/Deployment.yaml
spec:
  replicas: 2
  template:
    spec:
      containers:
        - name: api-golang
          image: sidpalas/devops-directive-docker-course-api-golang:PRODUCTION_VERSION

Routing rules are patched as well. Because routes are defined in an array, we specify the index of the entry being replaced:

# production/api-golang/patches/IngressRoute.replace-host.yaml
- op: replace
  path: /spec/routes/0/match
  value: "Host(`kubernetes-course.devopsdirective.com`) && PathPrefix(`/api/golang`)"

Rendering the configuration

Use kubectl kustomize to build the manifests for an environment:

kubectl kustomize ./staging | yq
kubectl kustomize ./production | yq

The accompanying Taskfile.yaml provides task render-staging and task render-production shortcuts. Once verified, deploy with kubectl apply -k <overlay>.

Kustomize offers a lightweight approach to multi-environment deployments by sharing common configuration and patching only what needs to change. Arrays require a bit of manual work, but it's a great place to start.