Kubernetes Configurations with Kustomize Transformers with Examples

In this article we are going to cover Kubernetes Configurations with Kustomize Transformers with Examples.

Managing Kubernetes configurations for multiple environments (such as development, staging, and production) can be complex and error-prone. The need to maintain consistency while introducing environment-specific customizations often leads to duplication and manual effort. Enter Kustomize, a configuration management tool built into kubectl, which enables declarative, reusable, and environment-specific resource management.

Among its most powerful features are Transformers, which allow dynamic modifications to Kubernetes resources. In this article, we’ll take a deep dive into transformers in Kustomize, exploring their purpose, types, and a hands-on example.

What Are Transformers in Kustomize?

In Kustomize, Transformers are functionalities or configurations that modify Kubernetes resource manifests during the build process. They provide a systematic way to adjust resource definitions, ensuring uniformity and adaptability across environments.

Transformers can be used for tasks such as:

  • Adding or updating labels and annotations.
  • Changing namespaces.
  • Applying patches to resource configurations.
  • Customizing resource configurations with plugins or scripts.

Transformers eliminate the need for maintaining duplicate YAML files for different environments, streamlining Kubernetes deployment workflows.

Key Types of Transformers

Kustomize supports several types of transformers:

1. Built-in Transformers

These are predefined in Kustomize and handle common tasks like:

  • Label Transformer: Adds or modifies labels in the metadata field.
  • Annotation Transformer: Adds or modifies annotations.
  • Namespace Transformer: Sets or updates the namespace for all resources.
  • Patch Transformer: Modifies specific resource fields using strategic merge or JSON patches.

2. Custom Transformers

Custom transformers can be defined to handle specific requirements, such as generating dynamic configurations based on external inputs or scripts.

3. Transformer Plugins

For advanced use cases, Kustomize supports plugins, allowing you to extend its capabilities. Plugins are external scripts or programs that perform transformations not natively supported by Kustomize.

Directory Structure Overview

To demonstrate how transformers work, let’s create a simple project with the following directory structure:

.
├── base
│   ├── deployment.yaml
│   ├── kustomization.yaml
│   └── service.yaml
└── overlays
└── production
├── kustomization.yaml
├── labels.yaml
├── output.yaml
└── patch.yaml

Here’s what each directory contains:

  • base/: Core Kubernetes resource definitions shared across environments.
  • overlays/production/: Customizations for the production environment, including transformers and patches.

Step #1:Base Configuration with Kustomize

The base/ directory contains the primary Kubernetes manifests that define the application.

deployment.yaml

Defines a deployment for the my-app application.

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

service.yaml

Defines a service to expose the application.

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

kustomization.yaml

Specifies the resources included in the base configuration.

resources:
  - deployment.yaml
  - service.yaml

Step #2:Adding Environment-Specific Overlays

The overlays/production/ directory contains customizations for the production environment.

kustomization.yaml

Defines the production overlay by referencing the base resources, applying a LabelTransformer, and using a patch.

resources:
  - ../../base

transformers:
  - labels.yaml

patchesStrategicMerge:
  - patch.yaml

labels.yaml

This LabelTransformer adds a environment: production label to all resources.

apiVersion: builtin
kind: LabelTransformer
metadata:
  name: label-transformer
labels:
  env: production
fieldSpecs:
  - kind: Deployment
    path: metadata/labels
    create: true
  - kind: Service
    path: metadata/labels
    create: true

patch.yaml

This patch updates the replicas count in the deployment to 5.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 5

Step #3:Building the Final Configuration

To generate the production-specific configuration, navigate to the overlays/production/ directory and run:

kustomize build .

The command outputs the merged configuration, including the base resources and the production-specific customizations:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  labels:
    app: my-app
    environment: production
spec:
  replicas: 5
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - image: nginx:latest
        name: my-app-container
---
apiVersion: v1
kind: Service
metadata:
  name: my-app-service
  labels:
    environment: production
spec:
  selector:
    app: my-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

Advantages of Transformers

Transformers in Kustomize provide several benefits:

  • Reusability: Keep the base configuration clean and reusable across different environments.
  • Consistency: Apply uniform labels, annotations, and patches dynamically.
  • Scalability: Effortlessly manage configurations for multiple environments (e.g., dev, staging, production).
  • Simplicity: Reduce duplication by modifying resource definitions programmatically.
  • Flexibility: Use built-in transformers or create custom ones to handle unique requirements.

Common Use Cases for Transformers in Kustomize

  • Adding environment-specific metadata (e.g., environment: production label).
  • Overriding resource limits, replicas, or image versions for staging and production.
  • Changing namespaces across multiple resources.
  • Applying dynamic customizations using plugins.

Conclusion

Kustomize Transformers are a powerful feature that simplifies Kubernetes configuration management. By separating base configurations from environment-specific overlays, transformers enable you to maintain clean, reusable, and adaptable YAML manifests. Whether you’re adding labels, annotations, or applying patches, Kustomize helps reduce complexity and improve productivity.

Try using Kustomize Transformers in your Kubernetes projects to unlock the full potential of declarative configuration management!

Related Articles:

Kubernetes Kustomize Tutorial with Examples

Reference:

Kustomize office page

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