Learn to organize Kubernetes manifests into directories and apply them using kubectl commands and Kustomize for improved manageability and scalability.
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:
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:
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:
Run the following command to perform a Kustomize build, which outputs the consolidated Kubernetes manifests:
Copy
Ask AI
kustomize build k8s/
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:
Copy
Ask AI
kustomize build k8s/ | kubectl apply -f -
Alternatively, use the built-in Kustomize functionality built into kubectl:
Copy
Ask AI
kubectl apply -k k8s/
A successful output should indicate that all resources have been created:
Copy
Ask AI
configmap/db-credentials createdconfigmap/redis-credentials createdservice/api-service createdservice/db-service createdservice/redis-cluster-ip-service createddeployment.apps/api-deployment createddeployment.apps/db-deployment createddeployment.apps/redis-deployment created
Verify that your pods are running with:
Copy
Ask AI
kubectl get podsNAME READY STATUS RESTARTS AGEapi-deployment-64dd567b46-1mw4c 1/1 Running 0 27sdb-deployment-657c8ffbd-vnjs7 1/1 Running 0 26sredis-deployment-587fd758cf-7pt57 1/1 Running 0 26s
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.
After deleting any old resources, apply the new configuration with:
Copy
Ask AI
kubectl apply -k k8s/
The output should confirm that all resources have been created:
Copy
Ask AI
configmap/redis-credentials createdservice/api-service createdservice/db-service createdservice/redis-cluster-ip-service createddeployment.apps/api-deployment createddeployment.apps/db-deployment createddeployment.apps/redis-deployment created
Verify the status of your pods with:
Copy
Ask AI
kubectl get podsNAME READY STATUS RESTARTS AGEapi-deployment-64dd567b46-lmw4c 1/1 Running 0 27sdb-deployment-657c8ffb8-vmjs7 1/1 Running 0 26sredis-deployment-587fd758cf-7pt57 1/1 Running 0 26s
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: