Exploring Kubernetes-Native Configuration Management with Kustomize

August 11, 2023by Dhawal

In the realm of Kubernetes and application deployment, effective configuration management is a crucial aspect to ensure scalability, maintainability, and consistency. One tool that stands out in this arena is “Kustomize.” This article delves into the concept of Kustomize and how it empowers users to manage configurations seamlessly within the Kubernetes ecosystem.

Kustomize ?

Kustomize serves as a powerful configuration management solution, offering a unique approach to managing configurations. At its core, Kustomize utilizes a layering mechanism that preserves the fundamental settings of applications and components. It achieves this by employing declarative YAML artifacts known as “patches.” These patches are strategically overlaid onto existing configuration files, allowing selective overrides of default settings without altering the original files themselves.

Benefits of Embracing Kustomize for Configuration Management
  1. Reusability: Kustomize promotes the reuse of base configurations across different environments, allowing custom specifications to be overlaid. This ensures consistency and reduces duplication.
  2. Efficiency: With standard YAML syntax, Kustomize enables quick declaration of configurations without relying on complex templating languages.
  3. Simplified Debugging: Kustomize’s transparent YAML structure aids in easy identification of issues. Isolated patches streamline debugging by comparing base configurations with modifications.
  4. Granular Troubleshooting: The patching approach allows focused issue tracing, facilitating pinpointing of misconfigurations or errors.
  5. Maintainable Upgrades: Kustomize facilitates seamless integration of base file updates while retaining customizations, ensuring compatibility with new versions.

By leveraging Kustomize’s capabilities, organizations streamline configuration management, enhance deployment processes, and bolster collaboration among development teams.

A Step-by-Step Guide : Installing Kustomize

Kustomize, a versatile configuration management tool, is available for installation in different ways. Here, we’ll explore how to install Kustomize both as a part of kubectl and as a standalone CLI tool for broader usage.

Check kubectl Version (Version >= 1.14):
Kustomize is conveniently packaged with kubectl version 1.14 or higher. To verify your kubectl version, execute the command kubectl version. If your version is 1.14 or above, you’re all set to proceed without any additional steps.

Standalone Kustomize Installation (Kustomize CLI):

  1. Download Kustomize Script:Begin by running the following command to acquire the Kustomize installation script:
    curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash
  2. Move Kustomize to System Path:Once the script is executed successfully, you need to move the Kustomize executable to a location in your system’s PATH to make it accessible globally. Use the following command (you might need administrative privileges):
    sudo mv kustomize /usr/local/bin
  3. Verification:To confirm the successful installation, open a new terminal window and run the command:
    kustomize -h
Kustomize Kubernetes Example

Let’s walk through a practical scenario to understand how Kustomize functions across multiple environments

  • Dev
  • Staging
  • Production

Each with distinct requirements. We’ll utilize service, deployment, and horizontal pod autoscaler (HPA) resources, tailoring services to the environment’s needs. Here’s a glimpse of the directory structure to visualize this process:

root-directory/
├── base/
│ ├── deployment.yaml
│ ├── hpa.yaml
│ ├── kustomization.yaml
│ └── service.yaml
└── overlays/
├── dev/
│ ├── hpa.yaml
│ └── kustomization.yaml
├── staging/
│ ├── hpa.yaml
│ ├── kustomization.yaml
│ └── service-nodeport.yaml
└── production/
├── hpa.yaml
├── kustomization.yaml
├── rollout-replica.yaml
└── service-loadbalancer.yaml
  1. Reviewing Base Files: Common Resources ConfigurationIn the base folder, you’ll find fundamental resources like deployment.yaml, service.yaml, and hpa.yaml. These configurations provide the foundation for your application deployment. Let’s dive into the contents of each file:
    base/deployment.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: frontend-deployment-example
    spec:
      selector:
        matchLabels:
          app: frontend-deployment-example
      template:
        metadata:
          labels:
            app: frontend-deployment-example
        spec:
          containers:
          - name: app
            image: foo/bar:latest
            ports:
            - name: http
              containerPort: 8080
              protocol: TCP

    base/service.yaml

    apiVersion: v1
    kind: Service
    metadata:
    name: frontend-service-example
    spec:
    ports:
    - name: http
    port: 8080
    selector:
    app: frontend-deployment-example

    base/hpa.yaml

    apiVersion: autoscaling/v2beta2
    kind: HorizontalPodAutoscaler
    metadata:
    name: frontend-deployment-hpa
    spec:
    scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: frontend-deployment
    minReplicas: 1
    maxReplicas: 5
    metrics:
    - type: Resource
    resource:
    name: cpu
    target:
    type: Utilization
    averageUtilization: 50

    These base configuration files establish the core resources needed for your application. With Kustomize, you can further customize these configurations for different environments while retaining this foundational setup.

    In the base folder, the kustomization.yaml file holds significant importance as it outlines the resources utilized in your configuration. Let’s take a closer look at its content:

    base/kustomization.yaml

    apiVersion: kustomize.config.k8s.io/v1beta1
    kind: Kustomization
    
    resources:
    - service.yaml
    - deployment.yaml
    - hpa.yaml
  2. Define Dev Overlay Files:Inside the dev folder:dev/kustomization.yaml

    apiVersion: kustomize.config.k8s.io/v1beta1
    kind: Kustomization
    bases:
      - ../../base
    patchesStrategicMerge:
      - hpa.yaml
    

    dev/hpa.yaml

    apiVersion: autoscaling/v2beta2
    kind: HorizontalPodAutoscaler
    metadata:
      name: frontend-deployment-hpa
    spec:
      minReplicas: 1
      maxReplicas: 2
      metrics:
      - type: Resource
        resource:
          name: cpu
          target:
            type: Utilization
            averageUtilization: 90
    
  3. Review Patches:Run the below command kustomize build to confirm that your patch configuration is correct. This will show you the merged configuration that will be applied to the cluster.
    kustomize build overlays/dev
  4. Apply Patches:Use the below command  to apply the settings to your cluster.
    kubectl apply -k overlays/dev

By meticulously defining and applying overlays for different environments, Kustomize empowers you to manage configuration customizations effectively, ensuring seamless deployment across diverse scenarios.