Managing multiple environments (development, staging, production) in Kubernetes can be complex. Different environments require different configurations, such as replica counts, image versions, and resource limits. Kustomize provides a clean, native Kubernetes solution to manage these variations while keeping a single source of truth.
Why Use Kustomize?
- Declarative approach – No need for external templating.
- Layered configuration – Maintain a base config with environment-specific overlays.
- Native Kubernetes integration – Directly used with kubectl apply -k.
Setting Up Kustomize Directory Structure
Kustomize uses a base-and-overlay pattern. We will create a base configuration that applies to all environments and overlays for dev, staging, and prod to customize them as needed.
Run the following to set up the directory structure:
mkdir -p kustomize/base kustomize/overlays/dev kustomize/overlays/staging kustomize/overlays/prod
Creating the Base Configuration
The base configuration includes the core Deployment and Service YAMLs.
Deployment (kustomize/base/deployment.yaml)
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
ports:
- containerPort: 80
Service (kustomize/base/service.yaml)
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
Base kustomization.yaml (kustomize/base/kustomization.yaml)
resources:
- deployment.yaml
- service.yaml
Creating Environment-Specific Overlays
Now, let’s define overlays for dev, staging, and prod.
Dev Patch (kustomize/overlays/dev/deployment-patch.yaml)
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
template:
spec:
containers:
- name: nginx
image: nginx:1.19
Staging Patch (kustomize/overlays/staging/deployment-patch.yaml)
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 3
template:
spec:
containers:
- name: nginx
image: nginx:1.21
Prod Patch (kustomize/overlays/prod/deployment-patch.yaml)
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 5
template:
spec:
containers:
- name: nginx
image: nginx:1.25
Defining Overlays
Each overlay references the base and applies environment-specific patches.
Dev kustomization.yaml (kustomize/overlays/dev/kustomization.yaml)
resources:
- ../../base
patches:
- path: deployment-patch.yaml
Staging kustomization.yaml (kustomize/overlays/staging/kustomization.yaml)
resources:
- ../../base
patches:
- path: deployment-patch.yaml
Prod kustomization.yaml (kustomize/overlays/prod/kustomization.yaml)
resources:
- ../../base
patches:
- path: deployment-patch.yaml
Applying the Configurations
To deploy the environment-specific configurations, use:
kubectl apply -k kustomize/overlays/dev/
kubectl apply -k kustomize/overlays/staging/
kubectl apply -k kustomize/overlays/prod/
Verify the deployments:
kubectl get deployments
kubectl get services
Conclusion
Kustomize simplifies Kubernetes configuration management by allowing environment-specific modifications while maintaining a single source of truth. With its patch-based approach, it avoids duplication and makes configurations easier to manage.
Have you used Kustomize before? How do you manage multiple Kubernetes environments? Let’s discuss in the comments!![]()