Skip to main content
In this demo, we review how to efficiently manage your Kubernetes configuration directories. The example uses a structured “k8s” directory containing three subdirectories, each dedicated to a distinct application component:
  • api/ – Contains Kubernetes configurations for your API.
  • cache/ – Hosts configurations for caching mechanisms, such as Redis.
  • db/ – Stores configurations for your MongoDB database.
Within each subdirectory, YAML files define deployments, services, config maps, and other Kubernetes resources. This guide describes the setup, key commands, and benefits of using Kustomize to streamline deployments.

MongoDB Deployment Example

Inside the db/ folder, one YAML file defines a standard Kubernetes Deployment for a MongoDB container. The configuration sets one replica and sources environment variables (username and password) from a config map named “db-credentials.”

Service and ConfigMap Example

In the cache/ folder, you will find YAML files for Redis. Below is an example of a ClusterIP service definition for a Redis deployment:
Additionally, before introducing Kustomize, the demo deploys resources using the standard method. Here is a sample ConfigMap for Redis credentials:
For additional information on configuring Kubernetes services and ConfigMaps, refer to the official Kubernetes Documentation.

Deploying the Configurations Without Kustomize

To deploy these configurations without Kustomize, open your terminal and use the kubectl apply command to apply manifests from the “k8s” directory. For example:
This command deploys all configurations within the “k8s” folder. Alternatively, you can apply each subdirectory individually:
To remove these resources from your cluster, run:

Introducing Kustomize

Kustomize simplifies resource management by consolidating multiple YAML files into a single manifest. Begin by creating a kustomization.yaml file at the root of your k8s directory. This file should list all the resource files to be customized. Start with these basic declarations:
Build the complete manifest with:
This command outputs the consolidated Kubernetes manifest. To apply the configuration, pipe the output to kubectl apply:
Alternatively, use the built-in Kustomize feature in kubectl:
After applying, verify that all resources have been deployed by listing the pods:
Example output:
Explore more Kubernetes best practices in our Kubernetes Basics guide.

Organizing Kustomize Configurations per Directory

For a scalable approach, create individual kustomization.yaml files within each subdirectory (api, cache, db). Then, update the root kustomization.yaml file to reference these folders.

API Directory

Inside the api/ folder, create a kustomization.yaml to include the API deployment and service definitions:

Cache Directory

Within the cache/ folder, create a kustomization.yaml for Redis configurations:

Database Directory

In the db/ folder, create a kustomization.yaml for MongoDB resources:
Finally, update the root k8s/ directory’s kustomization.yaml to reference each subdirectory:
Now, when you run:
Kustomize will traverse each subdirectory, read the respective kustomization.yaml files, and deploy all the resources accordingly.

Final Deployment and Verification

After removing any previously deployed resources, apply the new configuration structure:
The output should confirm the creation of each resource, as shown below:
Verify that all pods are running with:
Example output:
Remember to clean up any stale resources in your cluster before applying new configurations to avoid conflicts.

Kubernetes Resource Overview

This guide has walked you through managing multiple Kubernetes configuration directories and transitioning from standard kubectl apply methods to using Kustomize for a more scalable solution. For further details, check out the Kubernetes Documentation.

Watch Video

Practice Lab