By Jun Nguyen3 min read566 words

Kustomize for beginner

Technology
Technology
Productivity

Kustomize is a tool that helps you manage different versions of your Kubernetes configuration (YAML) files for different environments, such as development, staging, and production.

It lets you define a single set of common "base" files and then apply "overlays" (or patches) to customize them for each specific environment, without ever having to copy and paste.


The Problem Kustomize Solves

Imagine you have an application with a

deployment.yaml
and a
service.yaml
.

  • For development, you only need 1 copy (
    replica
    ) of your app.
  • For production, you need 10 copies for high availability, a different service type (
    LoadBalancer
    ), and more memory.

Without Kustomize, you might copy your entire set of YAML files into a

dev/
folder and a
prod/
folder. But what happens when you need to update the container image? You have to remember to change it in both places. This is a common source of errors.


How Kustomize Works: Base and Overlays

Kustomize solves this with a simple "Base" and "Overlay" concept.

Think of it like buying a plain suit:

  • Base: This is the plain suit itself. It's your complete, standard
    deployment.yaml
    and
    service.yaml
    files.
  • Overlays: These are your accessories for different events.
    • Your
      dev
      overlay is a note that says: "Just wear the suit with sneakers." (e.g.,
      replicas: 1
      )
    • Your
      prod
      overlay is a note that says: "Wear the suit with a dress shirt, tie, and nice shoes." (e.g.,
      replicas: 10
      ,
      serviceType: LoadBalancer
      )

You're not buying a whole new suit for each event. You're just customizing the base suit.


A Simple Example Structure

This is what a typical Kustomize project looks like in your code repository:

my-app/ ├── base/ │ ├── deployment.yaml # Your main app deployment (e.g., replicas: 1) │ ├── service.yaml # Your main app service (e.g., type: ClusterIP) │ └── kustomization.yaml # Lists the files in this base └── overlays/ ├── development/ │ ├── kustomization.yaml # Points to the base (../../base) │ └── patch-replicas.yaml # A *tiny* file: {spec: {replicas: 1}} └── production/ ├── kustomization.yaml # Points to the base (../../base) ├── patch-replicas.yaml # A *tiny* file: {spec: {replicas: 10}} └── patch-service.yaml # A *tiny* file: {spec: {type: LoadBalancer}}
  • The
    base
    folder has the full YAML for your app.
  • The
    overlays/production
    folder does not contain a full copy. It only has tiny "patch" files that describe the changes (like "set
    replicas
    to 10").
  • The
    kustomization.yaml
    file in each folder is the "glue" that tells Kustomize where to find the base and which patches to apply.

How You Use It

The best part is that Kustomize is built directly into

kubectl
. You don't need to install any extra tools. The
-k
flag tells
kubectl
to use Kustomize.

  1. To see what your

    prod
    YAML would look like:

    kubectl kustomize overlays/production

    (This will print the full YAML to your screen, with all the prod patches applied).

  2. To apply your

    prod
    configuration to your cluster:

    kubectl apply -k overlays/production
  3. To apply your

    dev
    configuration:

    kubectl apply -k overlays/development

Why It's Great for Beginners

  • No New Language: It's just YAML. You don't have to learn a complex templating language (like with Helm). You are just "patching" YAML with more YAML.
  • Don't Repeat Yourself (DRY): You define your core app once in the
    base
    . You only manage the differences.
  • Built-in: It's already part of
    kubectl
    , making it easy to get started.