Skip to main content
In this lesson, you’ll learn how to organize Kubernetes manifests into directories and apply them using both standard kubectl commands and Kustomize. This approach helps maintain a clean, modular structure for your Kubernetes configurations, improving manageability and scalability. The directory structure is organized as follows:
  • A main “K8s” directory containing:
    • An “api” folder that holds Kubernetes configurations for your API.
    • A “cache” folder that contains configurations for your cache service (for example, a Redis instance).
    • A “db” folder that stores configurations for your database service (for example, a MongoDB instance).
Within each folder, YAML files define deployments, services, and config maps. For example, the database folder includes a deployment YAML for a MongoDB container, while the other folders typically feature either a ClusterIP or LoadBalancer service and, optionally, a config map.
Below is an example Kubernetes Service configuration for Redis:
Before using Kustomize, resources are deployed using the standard methodologies. For instance, here is a ConfigMap definition for Redis credentials:

Deploying Without Kustomize

To deploy resources without Kustomize, open your terminal, navigate into each directory, and run the appropriate commands. For example:
You can apply the resources sequentially for clarity:
Alternatively, you can deploy all resources in one command. Note that reapplying resources will not change their state if nothing has been updated:
To clean up the applied resources, execute:
Deploying resources sequentially can help troubleshoot issues more effectively, as you can pinpoint where a problem occurs.

Introducing Kustomize

Kustomize simplifies resource management by allowing you to create a single, consolidated kustomization file at the root of your “K8s” directory. Begin by creating a kustomization.yaml file in the K8s directory with the content below:
Before proceeding, delete the previously applied resources to avoid conflicts:
The kustomization file begins by defining the API version and the kind. Next, specify the paths of the individual resource directories (or individual YAML files if desired). For example, to include resources from the API folder:
A complete kustomization file that imports configurations from all directories might look like this:
Run the following command to perform a Kustomize build, which outputs the consolidated Kubernetes manifests:
The output will include configurations from the cache, database, and API folders. Keep in mind that kustomize build only displays the final output without applying the configurations. To apply the built manifests, pipe the output into kubectl apply:
Alternatively, use the built-in Kustomize functionality built into kubectl:
A successful output should indicate that all resources have been created:
Verify that your pods are running with:
Using Kustomize can significantly reduce the complexity of managing resources in your cluster, especially as your application scales.

Refining the Directory Structure with Nested Kustomization Files

For enhanced scalability, you can place a kustomization.yaml file within each subdirectory. This modular approach lets each folder manage its own resources independently. The root kustomization.yaml then simply references these subdirectories.

For the API Folder

Create a kustomization.yaml file inside the “api” folder with the following content:

For the Cache Folder

Inside the “cache” folder, create a kustomization.yaml file:

For the DB Folder

Similarly, in the “db” folder, create a kustomization.yaml file:
Finally, update the root kustomization.yaml to reference the subdirectories, each containing its own kustomization.yaml:
After deleting any old resources, apply the new configuration with:
The output should confirm that all resources have been created:
Verify the status of your pods with:

This modular approach, which utilizes nested kustomization.yaml files, makes managing and scaling your Kubernetes configurations easier as your application grows. For more in-depth information on Kubernetes resources and configuration best practices, explore the following resources:

Watch Video

Practice Lab