What is Kustomize? Different Tools to Practice Kustomize

In this article we are going to cover What is Kustomize? Different Tools to Practice Kustomize.

Kustomize is a powerful tool for managing Kubernetes configurations, enabling developers to customize YAML manifests without resorting to templating. Whether you’re a beginner or an experienced Kubernetes user, practicing Kustomize can improve your workflow for managing configurations across multiple environments. This article explores how to practice Kustomize effectively and the tools that can aid your journey.

What is Kustomize?

Kustomize is a native Kubernetes tool for customizing application configurations. Unlike templating solutions like Helm, Kustomize works with declarative YAML files, maintaining readability and reducing complexity.

Key Benefits of using Kubernetes configuration using Kustomize

  • Declarative and template-free configuration management.
  • Environment-specific customization without duplicating base manifests.
  • Integrated directly into kubectl, ensuring seamless usage.

Setting Up Your Environment for Kustomize

Before practicing Kustomize, ensure you have the following:

  • A Kubernetes cluster: Use tools like Minikube or kind to create a local cluster.
  • Installed tools: Ensure kubectl and Kustomize CLI are installed on your system.

How to Install Kustomize?

  1. Install kubectl using package managers like apt, yum, or brew.
  2. Download and install the Kustomize CLI from the official Github Repository.

Getting Started with Kustomize

Core Concepts

  • Base: Core YAML configuration applicable to all environments.
  • Overlays: Environment-specific modifications layered on top of the base.
  • Resources: The actual Kubernetes objects managed by Kustomize.

Example Workflow:

Let’s assume you’re deploying a web application with the following files:

Kustomize directory structure:

.
├── base
│   ├── deployment.yaml
│   ├── kustomization.yaml
│   └── service.yaml
└── overlays
    └── dev
        ├── deployment-patch.yaml
        └── kustomization.yaml

Base Configuration (base/deployment.yaml):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web-app
  template:
    metadata:
      labels:
        app: web-app
    spec:
      containers:
      - name: web-app
        image: nginx:1.21
        ports:
        - containerPort: 80

Base Configuration (base/service.yaml):

apiVersion: v1
kind: Service
metadata:
name: web-app-service
spec:
selector:
app: web-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP

Customization File (base/kustomization.yaml):

resources:
- deployment.yaml
- service.yaml

Creating Overlays:

For a development environment, you might modify the replicas and add a custom tag.

Overlay (overlays/dev/kustomization.yaml):

resources:
- ../../base

patchesStrategicMerge:
- deployment-patch.yaml

Patch File (overlays/dev/deployment-patch.yaml):

apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 1
template:
spec:
containers:
- name: web-app
image: nginx:1.21-dev

To apply the development configuration:

kubectl apply -k overlays/dev

How to Practice Kustomize?

ConfigMap and Secret Generators using Kustomize

Use Kustomize to dynamically create ConfigMaps and Secrets.

Example (base/kustomization.yaml):

resources:
- deployment.yaml
- service.yaml

# ConfigMap generator for app configuration
configMapGenerator:
- name: app-config
literals:
- APP_ENV=production
- APP_DEBUG=false

# Secret generator for database credentials
secretGenerator:
- name: db-credentials
literals:
- DB_USER=admin
- DB_PASSWORD=securepassword

Updated Configuration (base/deployment.yaml):

apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 2
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
spec:
containers:
- name: web-app
image: nginx:1.21
envFrom:
- configMapRef:
name: app-config
- secretRef:
name: db-credentials
ports:
- containerPort: 80

Reapply the changes with:

kubectl apply -k .

Result

ConfigMaps and Secrets will be generated and applied without maintaining separate YAML files.

Use kustomize build to Preview the Final Resources:

You can run kustomize build in the base directory to see how your resources (ConfigMap, Secret, and Deployment) will be applied by Kustomize.

kustomize build .

Example Result:

apiVersion: v1
data:
  APP_DEBUG: "false"
  APP_ENV: production
kind: ConfigMap
metadata:
  name: app-config-4m6tkk8md8
---
apiVersion: v1
data:
  DB_PASSWORD: c2VjdXJlcGFzc3dvcmQ=
  DB_USER: YWRtaW4=
kind: Secret
metadata:
  name: db-credentials-fmd58tg5tc
type: Opaque
---
apiVersion: v1
kind: Service
metadata:
  name: web-app-service
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: web-app
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web-app
  template:
    metadata:
      labels:
        app: web-app
    spec:
      containers:
      - envFrom:
        - configMapRef:
            name: app-config-4m6tkk8md8
        - secretRef:
            name: db-credentials-fmd58tg5tc
        image: nginx:1.21
        name: web-app
        ports:
        - containerPort: 80

Different Tools to Practice Kustomize

Local Clusters

  • Minikube: A simple way to run a Kubernetes cluster locally.
  • kind: Lightweight and ideal for testing Kustomize configurations.

Cluster Management

  • Lens: Visualizes and manages clusters while allowing you to test Kustomize overlays.

Integration Tools

  • kubectl kustomize: Built into kubectl, enabling direct use of Kustomize configurations.

Online Platforms

  • Katacoda: Interactive Kubernetes and Kustomize learning environments.
  • Play with Kubernetes: Cloud-hosted clusters for practicing YAML configurations.

Conclusion:

Kustomize simplifies managing Kubernetes configurations, and practicing its features can help you deploy applications efficiently. By leveraging tools like Minikube, Lens, and CI/CD pipelines, you can explore and master Kustomize for real-world applications.

Related Articles:

Kubernetes Kustomize Tutorial with Examples

Understanding Helm Chart Structure

About Hint DevOps

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link