Sub topic 1
Why per-environment manifests get out of hand
Prerequisites
This chapter assumes you already know the following:
- Basic Kubernetes objects: Deployment, Service, ConfigMap and Namespace.
- How to read and write YAML.
- How to apply manifests with
kubectl apply -f. - Basic use of a terminal and a Git repository.
You do not need to install Kustomize yet. This chapter explains the problem that Kustomize solves, and later chapters cover the tool itself.
The same application in several places
Most teams run one application in more than one place. A typical set is dev, staging and production. The Deployment is almost the same in each. A few fields differ:
- The namespace.
- The replica count.
- The image tag.
- Resource requests and limits.
- Configuration values such as the log level.
The first approach most teams try is to copy the manifests into one directory per environment and edit the copies.
manifests/
dev/deployment.yaml
staging/deployment.yaml
production/deployment.yamlThis works on day one. Over time the copies stop matching each other.
How drift happens
Drift means the environments differ in ways nobody intended. It usually comes from small, reasonable changes:
- Someone adds a readiness probe to
production/deployment.yamland forgetsstaging. - A port changes in two files but not the third.
- A reviewer sees a diff in one file and cannot tell which lines are meant to differ between environments.
- Staging no longer predicts production, because it runs a different manifest.
With three environments and ten resources you maintain thirty files. Each shared change must be made in every copy, by hand.
Why templating is a partial fix
The common alternative is a template. You replace the values that change with placeholders and fill them in per environment.
spec:
replicas: {{ .Values.replicas }}
template:
spec:
containers:
- name: web
image: registry.example.com/web:{{ .Values.imageTag }}This removes the copying, but it has costs:
- The file is no longer valid YAML until it is rendered, so editors, linters and schema validators cannot check it directly.
- You decide in advance which fields can vary. A new difference needs a new placeholder.
- Logic such as conditionals and loops makes templates harder to read than the manifests they produce.
The Kustomize approach
Kustomize takes a different route. You keep one plain, valid manifest and describe each environment as a set of changes to it. Nothing in the base file is a placeholder.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: registry.example.com/web:1.0.0
ports:
- containerPort: 8080You can apply this file with kubectl apply -f as it is. An environment holds only its differences, such as "5 replicas, image tag 1.2.3, namespace production". Kustomize combines the base and those differences to produce the final manifests.
This gives you three properties:
- A shared change is made once, in the base.
- The differences between environments are small and easy to review.
- The base stays valid Kubernetes YAML that standard tools can read.
What the rest of the tutorial covers
The next chapters build this up step by step. You will write a kustomization.yaml file, render it, split a project into a base and overlays, and then apply transformers and patches to change specific fields.
Exercise
A team keeps three copies of deployment.yaml, one per environment. Name two problems with this approach, and one difference between how templating and Kustomize treat the shared file.
Show answer
Any shared change must be repeated in every copy, and the copies drift apart so staging stops matching production. Reviewers also cannot tell which differences are intentional. A template contains placeholders, so the file is not valid YAML until rendered. Kustomize keeps the base as plain valid YAML and applies per-environment changes on top of it.