Traditional Organization and Its Limitations
Imagine a directory named “k8s” that contains four YAML files:- An API deployment manifest
- An API service manifest
- A database deployment manifest
- A database service manifest
- Move the API deployment and service YAML files into an
apisubdirectory. - Move the database deployment and service YAML files into a
dbsubdirectory.
Simplifying with Kustomize
Kustomize can significantly streamline the process by allowing you to manage multiple directories from a single kustomization.yaml file.With Kustomize, you can maintain a single command to deploy resources, reducing manual steps and potential errors.
Setting Up Your Root kustomization.yaml
- In the root of your “k8s” directory, create a
kustomization.yamlfile. - List all individual resource files using their relative paths.
-
Build the complete configuration and pipe it to kubectl:
-
Apply directly using kubectl’s built-in Kustomize support:
kustomization.yaml file and deploy the defined resources automatically.
Scaling with Additional Directories
Over time, your application may bring in new components—such as a cache for Redis or a Kafka service. In that case, your root kustomization.yaml might expand to list many individual resources:A Cleaner Approach with Subdirectory kustomization.yaml Files
A more elegant solution delegates resource management to the individual subdirectories. In each subdirectory (e.g.,api, db, cache, and kafka), create a separate kustomization.yaml file that lists only the YAML files contained within that directory.
For example, the db directory might have the following kustomization.yaml file:
By organizing your configurations with subdirectory kustomization.yaml files, you not only simplify the management of Kubernetes manifests but also streamline your deployment processes—ideal for continuous integration and continuous delivery (CI/CD) pipelines. For more details on deploying and managing Kubernetes applications, visit the Kubernetes Documentation. Happy Kustomizing!