Day95- Using Kustomize for Kubernetes Manifests

Sourabhh Kalal
3 min readApr 7, 2024

As Kubernetes continues to be the cornerstone of container orchestration, managing its manifests can become increasingly complex, especially with the deployment of applications across multiple environments. Enter Kustomize — an open-source tool integrated with kubectl since version 1.14, designed to simplify the customization of Kubernetes manifests without the need for template processing. This blog post explores the power of Kustomize for managing Kubernetes manifests, offering practical examples to streamline your deployments.

What is Kustomize?

Kustomize introduces a template-free approach to modifying Kubernetes manifests, using a series of YAML files to define the customizations that should be applied to a base configuration. This method promotes reusability, maintainability, and clear separation of concerns between configuration and customization.

Key Concepts

  • Base: The original Kubernetes manifests that describe the desired state of your application or service.
  • Overlay: Modifications applied to the base for specific environments (e.g., development, staging, production).
  • Resource: Any valid Kubernetes object (e.g., Deployment, Service) that can be described in a YAML file.
  • Patch: A piece of YAML or JSON used to update or modify resources defined in your base or overlays.

Getting Started with Kustomize

To begin, ensure you have Kustomize installed. While kubectl already includes Kustomize, you can also install it standalone for more advanced features.

1. Organizing Your Directory Structure

A typical Kustomize directory structure might look like this:

.
├── base
│ ├── deployment.yaml
│ ├── kustomization.yaml
│ └── service.yaml
└── overlays
├── development
│ ├── kustomization.yaml
│ └── replica_count.yaml
└── production
├── kustomization.yaml
└── replica_count.yaml

2. Defining Your Base

In the base directory, you define your application's Kubernetes resources. Here's a simple 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:tag

And a kustomization.yaml to declare these resources:

resources:
- deployment.yaml
- service.yaml

3. Creating Overlays

Overlays allow you to define changes specific to each environment. For instance, in the development overlay, you might want to increase the replica count and change the image tag.

Create a kustomization.yaml in overlays/development:

bases:
- ../../base
patchesStrategicMerge:
- replica_count.yaml
images:
- name: my-app
newTag: dev-latest

And a replica_count.yaml to adjust the replica count:

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

4. Applying Your Configuration

To apply your configuration with Kustomize, navigate to your overlay directory and run:

kubectl apply -k .

This command tells kubectl to use Kustomize to generate the final manifests from the overlay and apply them to your cluster.

Advantages of Using Kustomize

  • Simplicity: No need for complicated templating engines.
  • Clarity: Separation of base configurations and environment-specific customizations.
  • Flexibility: Easily patch or replace resources for different environments.
  • Integration: Native support in kubectl simplifies workflows.

Conclusion

Kustomize revolutionizes Kubernetes manifest management by providing a powerful yet straightforward way to customize applications for different environments. By leveraging Kustomize, you can maintain a single source of truth for your base configuration while easily applying variations for development, staging, and production environments. Embrace Kustomize, and simplify your Kubernetes application deployment process today.

--

--