Kustomize is a tool for customizing Kubernetes configurations. It allows you to manage and deploy Kubernetes resources in a more flexible and reusable way. Unlike Helm, which uses templating, Kustomize works by overlaying patches on top of base configurations, making it a more declarative approach to configuration management.

Key Concepts

  1. Base and Overlays

  • Base: The base configuration is the original set of Kubernetes manifests that define your application.
  • Overlay: Overlays are modifications or customizations applied to the base configuration. They allow you to create different environments (e.g., development, staging, production) without duplicating the base configuration.

  1. Resources

  • Resources: These are the Kubernetes manifests (YAML files) that define your application components, such as Deployments, Services, ConfigMaps, etc.

  1. Patches

  • Patches: Patches are used to modify the base resources. They can be strategic merge patches, JSON patches, or replacement patches.

  1. Kustomization File

  • kustomization.yaml: This file defines the resources, patches, and other configurations for Kustomize. It is the entry point for Kustomize to understand how to build the final set of manifests.

Setting Up Kustomize

Installation

Kustomize can be installed as a standalone tool or as part of kubectl.

Standalone Installation

# Download the latest release
curl -s "https://api.github.com/repos/kubernetes-sigs/kustomize/releases/latest" | grep browser_download_url | grep kustomize | cut -d '"' -f 4 | xargs curl -O -L

# Make the binary executable
chmod +x kustomize

# Move the binary to your PATH
sudo mv kustomize /usr/local/bin/

Using kubectl

Kustomize is integrated into kubectl starting from version 1.14.

kubectl kustomize <directory>

Practical Example

Directory Structure

Let's create a simple example to demonstrate how Kustomize works. We'll deploy a web application with different configurations for development and production environments.

my-app/
├── base
│   ├── deployment.yaml
│   ├── service.yaml
│   └── kustomization.yaml
├── overlays
    ├── development
    │   ├── kustomization.yaml
    │   └── patch.yaml
    └── production
        ├── kustomization.yaml
        └── patch.yaml

Base Configuration

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-app:latest
        ports:
        - containerPort: 80

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: my-app
spec:
  selector:
    app: my-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

kustomization.yaml

resources:
- deployment.yaml
- service.yaml

Overlay Configuration

Development Overlay

patch.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 2
  template:
    spec:
      containers:
      - name: my-app
        image: my-app:dev
kustomization.yaml
resources:
- ../../base
patchesStrategicMerge:
- patch.yaml

Production Overlay

patch.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  template:
    spec:
      containers:
      - name: my-app
        image: my-app:prod
kustomization.yaml
resources:
- ../../base
patchesStrategicMerge:
- patch.yaml

Applying the Configuration

To apply the development configuration:

kubectl apply -k overlays/development

To apply the production configuration:

kubectl apply -k overlays/production

Exercises

Exercise 1: Create a New Overlay

  1. Create a new overlay for a staging environment.
  2. Modify the replicas to 4 and use the image my-app:staging.

Solution

  1. Create the directory structure:
    my-app/
    ├── overlays
        ├── staging
            ├── kustomization.yaml
            └── patch.yaml
    
  2. Create patch.yaml:
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-app
    spec:
      replicas: 4
      template:
        spec:
          containers:
          - name: my-app
            image: my-app:staging
    
  3. Create kustomization.yaml:
    resources:
    - ../../base
    patchesStrategicMerge:
    - patch.yaml
    

Exercise 2: Add a ConfigMap

  1. Add a ConfigMap to the base configuration.
  2. Modify the overlays to include environment-specific configurations.

Solution

  1. Create configmap.yaml in the base directory:
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-app-config
    data:
      key: value
    
  2. Update kustomization.yaml in the base directory:
    resources:
    - deployment.yaml
    - service.yaml
    - configmap.yaml
    
  3. Modify the overlays to include environment-specific configurations by creating patches for the ConfigMap.

Conclusion

Kustomize provides a powerful and flexible way to manage Kubernetes configurations. By using bases and overlays, you can easily customize and manage different environments without duplicating your configuration files. This approach promotes reusability and maintainability, making it easier to manage complex Kubernetes deployments.

Kubernetes Course

Module 1: Introduction to Kubernetes

Module 2: Core Kubernetes Components

Module 3: Configuration and Secrets Management

Module 4: Networking in Kubernetes

Module 5: Storage in Kubernetes

Module 6: Advanced Kubernetes Concepts

Module 7: Monitoring and Logging

Module 8: Security in Kubernetes

Module 9: Scaling and Performance

Module 10: Kubernetes Ecosystem and Tools

Module 11: Case Studies and Real-World Applications

Module 12: Preparing for Kubernetes Certification

© Copyright 2024. All rights reserved