DEV Community

Cover image for Day 37: Using Kustomize to Manage Kubernetes Configurations
Arbythecoder
Arbythecoder

Posted on

Day 37: Using Kustomize to Manage Kubernetes Configurations

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

  1. Base Configuration:

    The core, reusable Kubernetes resources, applicable across environments (e.g., Deployment, Service).

  2. Overlay:

    Environment-specific customizations or patches (e.g., changing replicas, service types).

  3. 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • kustomization.yaml: Lists resources in the base configuration.
resources:
- deployment.yaml
- service.yaml
Enter fullscreen mode Exit fullscreen mode

Deploy the base configuration:

kubectl apply -k base/
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • replica-count.yaml (dev overlay): Reduces replicas for development.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 1
Enter fullscreen mode Exit fullscreen mode
  • kustomization.yaml (dev overlay): Refers to the base and applies the patch.
resources:
- ../../base
patches:
- replica-count.yaml
Enter fullscreen mode Exit fullscreen mode

Deploy the dev overlay:

kubectl apply -k overlays/dev/
Enter fullscreen mode Exit fullscreen mode

Step 3: Testing the Overlays

To confirm the changes:

  1. Check the Deployment:
   kubectl get deployment nginx -o yaml
Enter fullscreen mode Exit fullscreen mode

Observe that the replicas have been updated to the overlay's configuration.

  1. 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 or SecretGenerator for dynamic ConfigMaps and Secrets.
  • Integrate with CI/CD pipelines for seamless deployments.

Top comments (0)