Basic Directory Structure Without Kustomize
Initially, you might store all your Kubernetes YAML files in a single directory (e.g., a directory named “k8s”). In this simple setup, you could have files such as:- API deployment YAML file
- API service YAML file
- Database deployment YAML file
- Database service YAML file
Organizing YAML Files into Subdirectories
A more structured approach is to organize your manifests into subdirectories. For instance, you can place API-related configurations in an “api” subdirectory and database-related configurations in a “db” subdirectory. Deployment commands for each subdirectory would look like this:Simplifying Deployment with Kustomize
Kustomize simplifies this process by letting you define a singlekustomization.yaml file that aggregates resources from multiple directories. Create a kustomization.yaml file in the root of your “k8s” directory with a list of resources and their relative paths. For example:
kustomization.yaml and deploy them simultaneously, eliminating the need to apply individual directories manually.
Scaling with Multiple Directories
As your Kubernetes project grows, you might add more subdirectories for additional services like caches or messaging systems (e.g., Redis or Kafka). For instance, your rootkustomization.yaml file might expand to include these new resources:
Consider breaking down the configurations into subdirectories with their own
kustomization.yaml files. This not only simplifies maintenance but also enhances scalability.Using Individual kustomization.yaml Files in Subdirectories
For improved maintainability, create a separatekustomization.yaml file within each subdirectory (such as “api”, “db”, “cache”, and “kafka”). For example, in the “db” directory, create a file with the following content:
kustomization.yaml file to reference each subdirectory:
kustomization.yaml, aggregating all the resources for deployment. This modular approach makes your configuration more manageable over time.
Deploying the Configurations
After setting up both the root and subdirectory-specifickustomization.yaml files, deployment becomes effortless. Simply run one of the following commands to deploy all configurations:
Conclusion
By organizing your Kubernetes YAML files into dedicated subdirectories and leveraging Kustomize with individualkustomization.yaml files, you can manage complex deployments more effectively. This approach not only cleans up your configuration hierarchy but also streamlines CI/CD pipelines by reducing repetitive commands.
For more insights on Kubernetes configuration management, consider exploring the following resources:
Always test your Kustomize configurations in a staging environment before deploying to production. This practice helps ensure that your aggregated manifests work as expected across your Kubernetes clusters.