CKA Certification Course - Certified Kubernetes Administrator

2025 Updates Kustomize Basics

Managing Directories

In this article, we explore how to efficiently manage Kubernetes manifests spread across multiple directories using Kustomize. Up to now, you have seen only a basic example with a simple kustomization.yaml file. However, even with limited knowledge, you can leverage powerful features in Kustomize to better organize your configurations.

Consider a scenario where you have a directory named "k8s" containing four YAML files:

  • API deployment
  • API service
  • Database deployment
  • Database service

Initially, you might deploy these configurations with the standard Kubernetes command:

kubectl apply -f k8s/

As your project grows and the number of YAML files increases to 20, 30, or even 50, you might decide to organize them into subdirectories. For example, you could move the API deployment and service YAML files into an "api" subdirectory and the database configurations into a "db" subdirectory. After reorganizing, you would deploy each set of configurations separately:

kubectl apply -f k8s/api/
kubectl apply -f k8s/db/

Note

With multiple subdirectories, managing deployments can become cumbersome. Every change might require separate commands for each subdirectory and even adjustments to your CI/CD pipeline.

This is where Kustomize proves its worth. You can create a root kustomization.yaml file in your "k8s" directory that lists every resource by its relative path:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

# Kubernetes resources to be managed by Kustomize
resources:
  - api/api-depl.yaml
  - api/api-service.yaml
  - db/db-depl.yaml
  - db/db-service.yaml

With this configuration, you can deploy all resources with a single command:

kustomize build k8s/ | kubectl apply -f -

Alternatively, you can leverage kubectl's native support for Kustomize:

kubectl apply -k k8s/

The output will indicate that your API and database services and deployments have been successfully created:

$ kustomize build k8s/ | kubectl apply -f -
service/api-service created
service/db-service created
deployment.apps/api-deployment created
deployment.apps/db-deployment created

$ kubectl apply -k k8s/
service/api-service created
service/db-service created
deployment.apps/api-deployment created
deployment.apps/db-deployment created

Modular Deployment

While the above approach works, it can get unwieldy if the number of directories increases. A modular strategy with separate kustomization.yaml files in each subdirectory helps to keep the configuration clean and maintainable.

Imagine expanding your project to include additional components, such as a cache and Kafka. Your root kustomization.yaml file might then list resources from API, database, cache, and Kafka directories as follows:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

# Kubernetes resources to be managed by Kustomize
resources:
  - api/api-depl.yaml
  - api/api-service.yaml
  - db/db-depl.yaml
  - db/db-service.yaml
  - cache/redis-depl.yaml
  - cache/redis-service.yaml
  - cache/redis-config.yaml
  - kafka/kafka-depl.yaml
  - kafka/kafka-service.yaml
  - kafka/kafka-config.yaml

This file can quickly become cluttered and hard to maintain. A better solution is to add a separate kustomization.yaml file within each subdirectory. For example, in the "db" directory, the kustomization.yaml might look like this:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - db-depl.yaml
  - db-service.yaml

Similarly, you should create kustomization.yaml files in the "api", "cache", and "kafka" directories. Then, update your root kustomization.yaml file to only reference these subdirectories:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - api/
  - db/
  - cache/
  - kafka/

When you run either of the following commands, Kustomize will automatically locate each subdirectory's own kustomization.yaml file and build a combined configuration:

kustomize build k8s/ | kubectl apply -f -

or

kubectl apply -k k8s/

This modular approach keeps your root kustomization.yaml file clean and simplifies the deployment process across multiple directories.

Summary

  • Organize your YAML files into directories as your project scales.
  • Use a root kustomization.yaml file to combine configurations.
  • For enhanced maintainability, create separate kustomization.yaml files in each subdirectory and reference them in the root file.

By following these practices, you optimize the organization of your Kubernetes manifests, making deployments easier to manage while keeping your configuration files neat and modular.

Watch Video

Watch video content

Previous
Kustomize ApiVersion Kind