In Kubernetes, deploying applications across multiple environments—such as development, staging, and production—often requires variations in configuration. These variations can lead to repetitive and hard-to-maintain YAML files. Enter Kustomize, a tool designed to simplify this process by enabling customizations without altering the original configurations.
What is Kustomize?
Kustomize is a native Kubernetes configuration management tool that uses a declarative approach to apply environment-specific customizations. Instead of duplicating your YAML files for every environment, Kustomize allows you to define a base configuration and apply targeted overlays for specific needs.
This ensures that your configurations remain DRY (Don't Repeat Yourself) and maintainable.
Why Kustomize?
Managing environment-specific variations often requires changes like:
- Adjusting replica counts for resource optimization.
- Modifying environment variables for debugging or production.
- Switching resource limits or service types.
Kustomize makes these changes easy and scalable while reducing errors caused by manual updates.
Key Concepts in Kustomize
Base Configuration:
The core, reusable Kubernetes resources, applicable across environments (e.g., Deployment, Service).Overlay:
Environment-specific customizations or patches (e.g., changing replicas, service types).Kustomization File (
kustomization.yaml
):
The heart of Kustomize that declares resources and patches.
Hands-On with Kustomize
Step 1: Set Up a Base Configuration
Start by defining the shared resources for all environments.
Folder Structure:
kustomize/
└── base/
├── deployment.yaml
├── service.yaml
└── kustomization.yaml
-
deployment.yaml
: Defines an Nginx Deployment with 2 replicas.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
-
service.yaml
: Creates a Service to expose the Nginx app.
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
-
kustomization.yaml
: Lists resources in the base configuration.
resources:
- deployment.yaml
- service.yaml
Deploy the base configuration:
kubectl apply -k base/
Step 2: Add Environment-Specific Overlays
Next, create environment-specific overlays (e.g., dev and prod). Each overlay modifies the base configuration using patches.
Folder Structure:
kustomize/
├── base/
└── overlays/
├── dev/
│ ├── kustomization.yaml
│ └── replica-count.yaml
└── prod/
├── kustomization.yaml
└── replica-count.yaml
-
replica-count.yaml
(dev overlay): Reduces replicas for development.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
-
kustomization.yaml
(dev overlay): Refers to the base and applies the patch.
resources:
- ../../base
patches:
- replica-count.yaml
Deploy the dev overlay:
kubectl apply -k overlays/dev/
Step 3: Testing the Overlays
To confirm the changes:
- Check the Deployment:
kubectl get deployment nginx -o yaml
Observe that the replicas have been updated to the overlay's configuration.
- Scale further or apply additional patches (e.g., resource limits, environment variables).
Going Beyond Basics
While this guide introduces the core capabilities of Kustomize, you can extend its use in more complex scenarios:
- Combine Kustomize with Helm for advanced templating and package management.
- Use
ConfigMapGenerator
orSecretGenerator
for dynamic ConfigMaps and Secrets. - Integrate with CI/CD pipelines for seamless deployments.
Top comments (0)