This article explores efficient management of Kubernetes manifests across multiple directories using Kustomize for better organization and deployment.
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:
Copy
Ask AI
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:
Copy
Ask AI
kubectl apply -f k8s/api/kubectl apply -f k8s/db/
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:
Copy
Ask AI
apiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomization# Kubernetes resources to be managed by Kustomizeresources: - 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:
Copy
Ask AI
kustomize build k8s/ | kubectl apply -f -
Alternatively, you can leverage kubectl’s native support for Kustomize:
Copy
Ask AI
kubectl apply -k k8s/
The output will indicate that your API and database services and deployments have been successfully created:
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:
Copy
Ask AI
apiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomization# Kubernetes resources to be managed by Kustomizeresources: - 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:
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:
When you run either of the following commands, Kustomize will automatically locate each subdirectory’s own kustomization.yaml file and build a combined configuration:
Copy
Ask AI
kustomize build k8s/ | kubectl apply -f -
or
Copy
Ask AI
kubectl apply -k k8s/
This modular approach keeps your root kustomization.yaml file clean and simplifies the deployment process across multiple directories.
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.