Sub topic 2
The kustomization.yaml file and its resources
This chapter assumes you can read Kubernetes YAML for a Deployment and a Service, and that you have kubectl installed. It also assumes you work from a terminal inside a Git repository.
What the file does
A kustomization.yaml file tells Kustomize which Kubernetes objects to build and which changes to apply to them. Kustomize reads it, loads the listed YAML, applies the changes, and prints the result. It never edits your source files.
The simplest useful file only lists resources. A resource is a path to a YAML file that holds one or more Kubernetes objects.
A minimal base
Start with a directory named base that holds two plain manifests. Neither file contains templating, so each is valid on its own.
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: 8080Save that as base/deployment.yaml. Then save the Service as base/service.yaml:
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 8080Now add the file that ties them together, base/kustomization.yaml:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yamlThe fields you will see first
apiVersionandkindidentify the file as a Kustomization. Kustomize accepts files without them, but declaring both avoids ambiguity.resourcesis a list of things to include. Later chapters add fields such asnamespace,patchesandconfigMapGeneratorbeside it.
Each entry in resources can be one of three things:
| Entry | Example | Meaning |
|---|---|---|
| A file | `deployment.yaml` | One or more objects in that file |
| A directory | `../../base` | Another kustomization, built first and then included |
| A remote URL | a Git repository address | A kustomization fetched over the network |
Paths are relative to the directory that holds the kustomization.yaml. The directory entry is the feature that makes overlays work, which the chapter on bases and overlays covers.
Checking that it works
Render the base to see the result. The next chapter covers this command in detail.
kubectl kustomize baseThe output is both objects as one YAML stream, separated by ---. Kustomize also orders the objects itself, so the order in your resources list does not control the output order.
Common errors
- The file name is wrong. Kustomize looks for
kustomization.yaml,kustomization.ymlorKustomizationin the directory you pass. Any other name gives a "missing kustomization file" error. - A listed path does not exist. A typo such as
deploy.yamlfails with an "accumulating resources" error that names the path. - A directory entry has no kustomization.yaml. Directories in
resourcesmust contain their own kustomization file. - The file lists itself or a non-Kubernetes file. Every resource file must contain objects with
apiVersion,kindandmetadata.name.
Exercise
You add ingress.yaml to the base directory, but kubectl kustomize base does not show the Ingress. What is the likely cause, and how do you fix it?
Show answer
The file is not listed under resources in base/kustomization.yaml. Kustomize only includes files that the kustomization names, not every file in the directory. Add - ingress.yaml to the resources list and render again.