Kustomize
Kustomize Basics
kustomization
In this guide, you’ll learn how to structure and configure the kustomization.yaml
file for managing Kubernetes manifests with Kustomize. We’ll cover directory setup, key fields, building overlays, and deploying your customized resources.
Prerequisites
Ensure you have Kustomize installed and available in your PATH
. You can verify with:
kustomize version
Directory Structure
Place your manifests and kustomization.yaml
in a folder (e.g., k8s/
). Only the kustomization.yaml
file is processed directly—other YAMLs are referenced as resources.
kustomization.yaml Fields
Here are the core sections you’ll use most often:
Field | Purpose | Example |
---|---|---|
resources | Lists input Kubernetes YAML manifests | - nginx-deployment.yaml |
commonLabels | Adds identical labels to every resource in build | company: KodeKloud |
Configuring Resources and Labels
Create or update kustomization.yaml
with your manifests and desired transformations:
# kustomization.yaml
resources:
- nginx-deployment.yaml
- nginx-service.yaml
commonLabels:
company: KodeKloud
- resources: Relative paths to each manifest file.
- commonLabels: A map of labels automatically injected into all output resources.
File Naming Best Practice
Keep your file names consistent (e.g., nginx-service.yaml
vs. nginx-service.yml
) to avoid missing resources during kustomize build
.
Generating Customized Manifests
Run the following command from the parent directory:
kustomize build k8s/
This command:
- Reads
k8s/kustomization.yaml
. - Imports listed resources.
- Applies
commonLabels
and other transformations. - Emits the merged YAML to stdout.
Sample Output
apiVersion: v1
kind: Service
metadata:
name: nginx-loadbalancer-service
labels:
company: KodeKloud
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 3000
protocol: TCP
selector:
company: KodeKloud
component: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
company: KodeKloud
spec:
replicas: 1
selector:
matchLabels:
company: KodeKloud
component: nginx
template:
metadata:
labels:
company: KodeKloud
component: nginx
spec:
containers:
- name: nginx
image: nginx
Notice each resource now carries company: KodeKloud
. You can apply this directly to your cluster:
kustomize build k8s/ | kubectl apply -f -
Watch Video
Watch video content