Kustomize k8s config management

From paulpas/agent-skill-router · View interactive version

Kustomize k8s config management is an AI agent skill from the GitHub repository paulpas/agent-skill-router. Implements Kubernetes configuration management with Kustomize bases, overlays, strategic/JSON patches, and generators for environment-specific deployment customization.

How to use this skill

Skill file

Kubernetes Configuration Management with Kustomize

Implements environment-specific Kubernetes configurations using Kustomize bases, overlays, strategic merge patches, and JSON patches. Manages deployment customization for dev, staging, and production without duplicating YAML manifests.

TL;DR Checklist


When to Use

Use this skill when:


When NOT to Use

Avoid this skill for:


Core Workflow

  1. Define the Base Layer — Create a base directory containing all shared Kubernetes manifests and a kustomization.yaml that declares them with resources:. Add commonLabels and commonAnnotations that apply to every resource. Checkpoint: Run kustomize build ./base — all resources should render without errors and carry the common labels.
  1. Create Environment Overlay Directories — Under overlays/<env>/ (e.g., overlays/production/), create a kustomization.yaml that references the base via bases: or resources: and declares overlay-specific customizations. Checkpoint: Each overlay must have a namespace: declaration and environment-specific image/replica overrides.
  1. Apply Patches — Use one of these patch strategies per overlay:

Checkpoint: Every patch must target a valid kind: <resource-kind> and name: <resource-name> that exists in the base.

  1. Customize Images, Replicas, and ConfigMaps — Declare image overrides with images: (e.g., name: myapp, newTag: v2.1.0), replica scaling with replicas:, and ConfigMap key overrides with configMapGenerator:. Checkpoint: Run kustomize build ./overlays/production and grep for replicas: 3 and the correct image tag to confirm overrides.
  1. Validate and Deploy — Run kustomize build <overlay-dir> > /dev/null to validate, then pipe output to kubectl apply or hand off to a GitOps controller. Checkpoint: Diff the rendered output against the previous deployment to confirm only intended fields changed.

Implementation Patterns / Reference Guide

Pattern 1: Base + Overlay Directory Structure

Standard Kustomize project layout separates shared resources from environment-specific customizations:

my-kustomize-project/
├── base/
│   ├── kustomization.yaml
│   ├── deployment.yaml
│   ├── service.yaml
│   └── configmap.yaml
├── overlays/
│   ├── dev/
│   │   ├── kustomization.yaml
│   │   └── replica-patch.yaml
│   ├── staging/
│   │   ├── kustomization.yaml
│   │   └── replica-patch.yaml
│   └── production/
│       ├── kustomization.yaml
│       ├── resource-patch.yaml
│       ├── replica-patch.yaml
│       └── config-patch.yaml

Base kustomization.yaml:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - deployment.yaml
  - service.yaml
  - configmap.yaml

commonLabels:
  app: my-service
  managed-by: kustomize

commonAnnotations:
  kustomize.tool.gitops.myorg.io/repo: my-kustomize-project
  kustomize.tool.gitops.myorg.io/base: base

Production overlay kustomization.yaml:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: production

resources:
  - ../../base

images:
  - name: myregistry/my-service
    newTag: "2.1.0"
    newValue: "2.1.0"

replicas:
  - name: my-service-deployment
    count: 5

patches:
  - path: replica-patch.yaml
  - path: resource-patch.yaml
  - path: config-patch.yaml

Pattern 2: Strategic Merge Patch vs JSON 6902 Patch

Use strategic merge patches for structural changes (adding new fields, updating nested objects) and JSON 6902 patches for precise field-level edits without overwriting the entire resource.

# ❌ BAD: Overwriting the entire deployment spec loses any fields
# not present in the patch file, making future base changes fragile
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-service-deployment
spec:
  template:
    spec:
      containers:
        - name: my-service
          image: myregistry/my-service:2.1.0
          ports:
            - containerPort: 8080
          resources:
            requests:
              cpu: "500m"
              memory: "512Mi"
            limits:
              cpu: "1000m"
              memory: "1Gi"

# ✅ GOOD: Strategic merge patch — only adds/overwrites the fields
# specified; the rest of the resource is inherited from base
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-service-deployment
spec:
  template:
    spec:
      containers:
        - name: my-service
          resources:
            requests:
              cpu: "250m"
              memory: "256Mi"
            limits:
              cpu: "500m"
              memory: "512Mi"
          env:
            - name: LOG_LEVEL
              value: "info"
            - name: ENVIRONMENT
              value: "production"

JSON 6902 Patch (targeted field edit):

# overlays/production/config-patch.yaml
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Patch
op: replace
path: /spec/template/spec/containers/0/env/2/value
value: "critical-path"
target:
  kind: Deployment
  name: my-service-deployment
  apiVersion: apps/v1
  namespace: production

Pattern 3: ConfigMap Generator in Overlay

Generate or override ConfigMap entries per environment without modifying the base manifest:

# overlays/dev/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: dev

resources:
  - ../../base

configMapGenerator:
  - name: my-service-config
    literals:
      - DATABASE_HOST=localhost
      - DATABASE_PORT=5432
      - CACHE_TTL=60
      - FEATURE_FLAGS=debug-mode=true
    options:
      disableNameSuffixHash: "false"

configurations:
  - kustomizeconfig.yaml
# overlays/dev/kustomizeconfig.yaml
# Field generators: replace values in existing ConfigMap keys
nameReference:
  - kind: ConfigMap
    fieldSpecs:
      - kind: Deployment
        path: spec/template/spec/volumes/0/configMap/name

name:
  - kind: ConfigMap
    path: spec/template/spec/volumes/0/configMap/name

Pattern 4: Variable Substitution with kustomize edit

Use kustomize edit for automated overlay customization from scripts or CI pipelines:

# Add an image override programmatically
kustomize edit set image myregistry/my-service:v3.0.0

# Add an env var to a ConfigMap generator
kustomize edit set configmap my-service-config --from-literal=FEATURE_FLAGS=dark-mode=true

# Set replicas
kustomize edit add patch --kind Deployment --name my-service-deployment \
  --patch '[{"op":"replace","path":"/spec/replicas","value":10}]'

# Add a label to all resources
kustomize edit add label release-cycle=2026-w32

# Verify the result
kustomize build ./overlays/production

Pattern 5: Patch Order and Merging

Kustomize applies patches in declaration order. The last patch wins for conflicting fields. Understand this for debugging unexpected output:

Rendered base resource
       ↓
patchesStrategicMerge (applied in order listed)
       ↓
patches (inline, applied in order listed)
       ↓
patchesJson6902 (applied in order listed)
       ↓
final rendered output
# overlays/staging/kustomization.yaml
# Patches applied in this exact order:
patches:
  # 1st: Set resource limits (strategic merge)
  - path: limits-patch.yaml
  # 2nd: Override specific env vars (JSON 6902)
  - path: env-override.yaml
  # 3rd: Add node affinity (strategic merge) — may conflict with limits-patch
  - path: affinity-patch.yaml

# If limits-patch.yaml and affinity-patch.yaml both set spec.template.spec.containers,
# the affinity-patch wins because it appears last.
# Use `kustomize build ./overlays/staging` to inspect the merge result.

Constraints

MUST DO

MUST NOT DO


Troubleshooting Common Issues

| Symptom | Likely Cause | Fix |

|---|---|---|

| build fails with no matches for kind Deployment | Target selector in JSON 6902 patch references wrong kind | Verify kind: Deployment and name: match a resource in the base |

| Patch has no visible effect | Patch declared after a later patch overwrites the same field | Reorder patches: so your patch applies last, or use JSON 6902 for precision |

| image: override not applied | Image name in images: doesn't match the container image: in base | Use kustomize build and grep for image: to confirm the rendered value |

| ConfigMap name mismatch after generation | Default name suffix is appended (e.g., config-abc123) | Set options.disableNameSuffixHash: "false" to retain the declared name |

| Replicas not changing | replicas: count name doesn't match deployment name in base | Check kustomization.yaml resources: for the declared base name |


Related Skills

| Skill | Purpose |

|---|---|

| cncf-kubernetes | Core Kubernetes concepts, resources, and API patterns — prerequisite for understanding Kustomize targets |

| cncf-helm | Alternative configuration management with templating and package management — choose Kustomize or Helm, not both |

| cncf-argo-cd | GitOps controller that natively supports kustomize build as a source type for continuous deployment |


Live References

Authoritative documentation links for Kustomize configuration management.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.