Skip to main content
In this lesson, we explore how to effectively manage directories containing Kubernetes manifests. The demonstration uses a structured “K8s” directory that holds all Kubernetes configurations organized into three subdirectories: one for the API, one for the cache (acting as a readers’ database), and one for the MongoDB database. When you open the K8s directory, you’ll see three distinct folders. Each folder includes configuration files (YAML manifests) tailored for a specific component. For instance, the database folder contains the deployment YAML files for MongoDB, while the API and cache directories contain configurations for services such as ClusterIP or LoadBalancer services along with associated ConfigMaps.
The image shows the Visual Studio Code interface with a project open, displaying a folder structure on the left and a welcome screen on the right. The project includes YAML files related to Kubernetes configurations.
Below is an excerpt showcasing a typical service configuration for the cache component:
Before we introduce Kustomize, let’s deploy these resources using the conventional approach without customization. Typically, you navigate into each directory and run the kubectl apply command as shown below:
You can also deploy all configurations in one command by appending multiple -f flags:
To delete all resources simultaneously, you can run:
Using multiple -f flags simplifies bulk deployment and deletion. However, as your infrastructure grows, managing these commands can become cumbersome.

Simplifying Resource Management with Kustomize

Kustomize makes it easier to manage and customize your application configurations. Begin by creating a kustomization.yaml file in the root of your K8s directory. This file specifies the API version and kind required by Kustomize:
Next, define the resources you wish to manage by listing the file paths relative to the kustomization.yaml file’s location. For example:
You can then build the complete set of manifests using the Kustomize CLI:
This command outputs the final Kubernetes manifests, combining configurations from the API, cache, and database folders. While kustomize build displays the resulting configuration, it does not apply it to your cluster. To deploy these resources, pipe the output to kubectl apply:
Alternatively, leverage the built-in support for Kustomize in kubectl with the -k flag:
After applying the configurations, verify that the resources have been successfully created by checking the pods:
Expected output:
For quick troubleshooting, always verify your pods’ status with kubectl get pods after deploying configurations.

Advanced Directory Structuring with Kustomize

While maintaining a single kustomization.yaml in the root directory works for simple projects, a more scalable practice is to include a kustomization.yaml file in each subdirectory. In this method, each directory imports only the YAML files specific to its component, while a root kustomization.yaml aggregates these directories.

API Directory Configuration

In the API folder, create a kustomization.yaml that lists the API deployment and service manifests:

Cache Directory Configuration

In the cache directory, set up the following kustomization.yaml:

Database Directory Configuration

Similarly, for the database directory, create a kustomization.yaml:

Root Directory Aggregation

Finally, update the root kustomization.yaml to reference these subdirectories. When a directory is specified as a resource, Kustomize automatically searches for a kustomization.yaml file inside:
Before re-deploying, delete any previously applied resources:
Then, build the final configuration with Kustomize:
Review the output to ensure it meets your expectations, then apply the aggregated configuration:
The expected output should be similar to:
Verify the pods again:
Expected output:
This structured approach using Kustomize not only centralizes the management of your Kubernetes configurations but also offers a scalable solution for handling an expanding set of resources within your infrastructure.

Watch Video

Practice Lab