Kubernetes 101: Kustomize

The Kubernetes ecosystem is vast, continuously evolving, and encompasses a wide range of tools that aim to simplify, streamline, and optimize application deployment. Amid this landscape, two names frequently emerge in the context of configuration and deployment: Helm and Kustomize.

Kubernetes 101: Kustomize

Introduction

The Kubernetes ecosystem is vast, continuously evolving, and encompasses a wide range of tools that aim to simplify, streamline, and optimize application deployment. Amid this landscape, two names frequently emerge in the context of configuration and deployment: Helm and Kustomize. While Helm has been around for a while and has established itself as a prominent package manager for Kubernetes, Kustomize has steadily gained traction by offering a unique approach to application configuration. This article delves deep into Kustomize, highlighting its benefits, best use cases, and contrasts it with Helm.

What is Kustomize?

Kubernetes, as a container orchestration platform, requires detailed configuration through manifest files to operate. As projects grow, managing these configurations across different environments—development, staging, production—becomes complex. Here’s where Kustomize comes in, offering a unique approach to configuration management tailored for Kubernetes.

Customization without Templating

Unlike many configuration management tools, Kustomize doesn’t rely on templating. Templating typically requires placeholders in the original files which get replaced with actual values. Instead, Kustomize operates by overlaying changes onto original manifests, ensuring they remain unchanged.

Layering Configurations

A core philosophy behind Kustomize is layering configurations. You start with a base – your standard configuration. Over this base, you can apply layers (or overlays) of changes for specific scenarios or environments.

For example, Imagine a scenario where you have a standard deployment for a microservice. In development, you might want it to connect to a mock database. In production, it connects to your primary database. With Kustomize, the base describes the standard deployment, and overlays specify changes for development and production environments.

Key Components of Kustomize

  • Bases: These are foundational configurations. For instance, a base might define a Kubernetes Deployment with two replicas of a specific container image.
  • Overlays: Built on top of bases, overlays apply modifications. Continuing our example, an overlay might modify the base to specify ten replicas instead of two for a production environment.
  • kustomization.yaml: An integral part of Kustomize, this file dictates how different pieces fit together. It references resources, describes patches, and can even handle variables.

Benefits of Using Kustomize

Source: https://devopscube.com/kustomize-tutorial/

Kustomize isn’t the only configuration management tool in the Kubernetes world, but it brings distinct advantages to the table:

Streamlined Declarative Management

With Kustomize, the focus shifts from how configurations change to what the desired end state is. This declarative approach aligns with Kubernetes' philosophy, making Kustomize a natural fit.

Example: Rather than writing scripts that dictate replace X with Y (imperative), you declare I want it to be Y (declarative).

Simplified Configuration Handling

As there's no need for a separate templating engine, Kustomize reduces the learning curve for new users. Raw Kubernetes manifests remain untouched, and patches or changes are explicitly defined in overlays.

Preservation of Original Manifests

One of Kustomize's strongest points is its non-intrusive nature. Original manifests remain untouched, ensuring that configurations are consistent and traceable. Overlays ensure that changes are applied transparently, without obscuring the original intent of the manifests.

Enhanced Reusability and Consistency

With Kustomize's base and overlay model, common configurations can be defined once and reused across various environments. This ensures consistency while minimizing redundancy and potential errors.

Kustomize vs. Helm

While both Helm and Kustomize are aimed at simplifying the deployment process in Kubernetes, they have different philosophies and methodologies.

Kustomize's Approach

Kustomize focuses on customizing raw Kubernetes YAML manifests using a base-and-overlay methodology. It doesn't introduce a new templating format; instead, it works directly with native Kubernetes manifests. Understanding when to leverage Kustomize can be as crucial as understanding its functionality:

Distinct Environment Configurations: Projects often span multiple environments, each with subtle configuration differences. Kustomize’s layered approach—using bases and overlays—makes managing these differences efficient and clear.
For example, A company might have separate configurations for local development, CI/CD testing, staging, and production. Using Kustomize, they can maintain a common base with overlays for each specific environment, ensuring clear differentiation and reducing configuration drift.

Avoiding Templating Overhead: For teams or individuals who prefer to stay closer to raw Kubernetes manifests without the overhead of templating engines, Kustomize offers a more direct approach.

GitOps Workflows: GitOps, a term coined by Weaveworks, is a way to do Kubernetes cluster management and application delivery. It works by using Git as a single source of truth for declarative infrastructure and applications. Given Kustomize's declarative nature and its ability to layer configurations, it meshes well with GitOps principles. Tools like ArgoCD or Flux, which champion GitOps, often support or integrate directly with Kustomize.

Helm's Approach

Helm introduces the concept of "charts" – pre-packaged Kubernetes applications. A chart contains all the resource definitions necessary to run an application, tool, or service inside a Kubernetes cluster. Helm uses its templating engine, allowing for dynamic content generation based on values provided at installation time.

Comparison

Feature/Aspect Kustomize Helm
Core Concept Base and Overlay Charts and Releases
Configuration YAML patches Templated YAML with a values file
Integration Built into kubectl (from v1.14) Requires a separate CLI
State Management Stateless – no tracking of releases Maintains release history for rollbacks
Complexity Direct approach with native YAML Templating can introduce complexity
Reusability Common bases can be reused with different overlays Charts can be packaged and shared
Flexibility Patches existing manifests Can dynamically generate entire manifests
Tool Ecosystem Integrated into some GitOps tools Rich ecosystem with Helm Hub (chart repo)
Secret Management Can generate secrets Can template secrets, but needs caution

Best Cases for Helm and Kustomize

When to use Helm:

  1. Packaged Applications: If you want pre-packaged applications that can be deployed with a single command, Helm's charts are an excellent choice. Helm Hub, for instance, offers a plethora of community-driven charts for popular software.
  2. Release Management: Helm tracks each deployment as a 'release'. If you need a detailed history of releases, rollbacks, and version management, Helm shines.
  3. Dynamic Configuration: Helm's templating allows for dynamic generation of manifests. If you need to generate vastly different configurations based on a set of input values, Helm is more suitable.
  4. Dependency Management: Helm charts can define dependencies on other charts, allowing for the coordinated deployment of interdependent applications.

When to use Kustomize:

  1. Minimal Abstraction: If you prefer working closer to raw Kubernetes YAML without the overhead of another templating system, Kustomize is ideal.
  2. Distinct Environment Configurations: Kustomize's base-and-overlay methodology excels at maintaining common configurations and applying environment-specific patches. This ensures clear differentiation and reduces configuration drift.
  3. Stateless Configuration: Kustomize is stateless. If you want a tool that applies configurations without tracking the state, Kustomize is more straightforward.
  4. GitOps Workflows: Given its declarative nature and compatibility with raw Kubernetes manifests, Kustomize is often a preferred choice in GitOps workflows.

Kustomize In Action

To truly grasp the power and utility of Kustomize, let's delve into a practical example, showcasing its key features and use cases.

Base Configuration:
Suppose we have a simple application—a web server. Here's how a base configuration might look (base/deployment.yaml):

Creating an Overlay for Development:
Now, for our development environment, we want to:
1. Use a different image tagged with dev
2. Reduce the replicas to 1 for resource conservation

We create an overlay for development:

With the above configurations, invoking

~$ kubectl apply -k overlays/dev/ would result in deploying the dev tagged image of the web server with just one replica, perfectly tailored for our development environment.'

Conclusion

Kustomize, in essence, answers a pressing need in the Kubernetes ecosystem—a tool that allows configuration customization without drifting away from raw Kubernetes manifests. Through its approach of using bases and overlays, it ensures simplicity, transparency, and reusability. In modern cloud-native landscapes, where infrastructure as code is becoming a standard and GitOps is gaining traction, tools like Kustomize stand out. They not only facilitate configuration management but also ensure that the entire process is version-controlled, traceable, and replicable.

As with any tool, it's imperative to evaluate your specific needs, existing infrastructure, and team preferences. While Kustomize offers a plethora of features and advantages, its real strength lies in its ability to simplify complex configurations and streamline deployments across varied environments. As Kubernetes continues to dominate the container orchestration scene, understanding and leveraging tools like Kustomize will undeniably be a valuable asset for developers, operators, and organizations as a whole.