Kustomize for beginner
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
- 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
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.yamlandservice.yamlfiles.
- Overlays: These are your accessories for different events.
- Your devoverlay is a note that says: "Just wear the suit with sneakers." (e.g.,replicas: 1)
- Your prodoverlay is a note that says: "Wear the suit with a dress shirt, tie, and nice shoes." (e.g.,replicas: 10,serviceType: LoadBalancer)
- Your
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 basefolder has the full YAML for your app.
- The overlays/productionfolder does not contain a full copy. It only has tiny "patch" files that describe the changes (like "setreplicasto 10").
- The kustomization.yamlfile 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
-
To see what your
prodYAML would look like:kubectl kustomize overlays/production
(This will print the full YAML to your screen, with all the prod patches applied).
-
To apply your
prodconfiguration to your cluster:kubectl apply -k overlays/production
-
To apply your
devconfiguration: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.