Kubernetes Customization with Kustomize Components

In this article we are going to cover Simplifying Kubernetes Customization with Kustomize Components.

Kustomize, a Kubernetes-native configuration management tool, empowers DevOps teams to manage and customize application deployments efficiently. Components, an advanced feature of Kustomize, take customization to the next level by promoting reusability. This article explores components in depth, showcasing their practical applications and benefits.

Prerequisites

Before diving into components, ensure you have the following:

  • Kustomize Installed: Version 5.0.0 or later to support components.
  • A Basic Understanding of Kubernetes: Familiarity with YAML manifests, Deployments, Services, and ConfigMaps.
  • A Kubernetes Cluster: For testing configurations (e.g., Minikube, Kind, or a managed Kubernetes service).
  • kubectl Installed: To apply and manage Kubernetes manifests.
  • A Project Directory Structure: Set up with base, components, and overlay directories as described in the examples below.

What are Components?

Components in Kustomize are modular and reusable pieces of configuration logic. They allow you to define optional functionality that can be enabled or disabled across different overlays, making them ideal for managing shared or environment-specific configurations.

When to Use Components

Components are particularly useful when:

  • Multiple overlays share common configuration logic, such as debugging or monitoring settings.
  • You want to avoid duplicating configuration code.
  • Optional features need to be toggled on or off for specific environments.
Directory Structure:
project/
├── base/
│   ├── deployment.yaml
│   └── kustomization.yaml
├── components/
│   └── debug/
│       ├── configmap-debug.yaml
│       └── kustomization.yaml
└── overlays/
    ├── dev/
    │   └── kustomization.yaml
    └── prod/
        └── kustomization.yaml
1. Base Configuration

base/deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: nginx

base/kustomization.yaml:

resources:
  - deployment.yaml
2. Debug Component

components/debug/configmap-debug.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: debug-config
  labels:
    debug: enabled

components/debug/kustomization.yaml:

apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
resources:
  - configmap-debug.yaml
3. Overlay Usage

overlays/dev/kustomization.yaml:

resources:
  - ../../base
components:
  - ../../components/debug

overlays/prod/kustomization.yaml:

resources:
  - ../../base
Result of Applying Overlays

To see the result, run the following commands:

1. For the dev overlay:

kustomize build overlays/dev

Output:

apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    debug: enabled
  name: debug-config
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - image: nginx
        name: my-app

2. For the prod overlay:

kustomize build overlays/prod

Output:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - image: nginx
        name: my-app

The dev overlay includes the debug-config ConfigMap, while the prod overlay contains only the base deployment.

Benefits of Components in Kustomize

  • Reusability: Shared configurations can be managed centrally.
  • Flexibility: Enable or disable functionality for specific environments.
  • Efficiency: Reduces duplication and simplifies complex setups.

Conclusion:

Kustomize’s components are a powerful feature that enhance Kubernetes configuration management. Components improve reusability and modularity, allowing you to simplify complex workflows and promote best practices for managing multi-environment setups. By leveraging these features, you can build scalable and maintainable Kubernetes deployments with ease.

Related Articles:

Multi Environment Workflows in Kustomize

Harish Reddy

Leave a Comment

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

Share via
Copy link
Powered by Social Snap