- A top-level “k8s” directory containing two subdirectories:
- An “API” folder with:
- An API deployment YAML file (e.g., api-depl.yaml)
- An API service YAML file (e.g., api-service.yaml)
- A kustomization.yaml file that imports these resources
- A “database” folder with:
- A database deployment YAML file (e.g., db-depl.yaml)
- A database service YAML file (e.g., db-service.yaml)
- A config YAML file (e.g., db-config.yaml)
- Its own kustomization.yaml file that imports these resources
- An “API” folder with:
Adding a Common Label to All Resources
To ensure every Kubernetes resource is marked with a common label such asdepartment: engineering, update the root kustomization.yaml. This change propagates the label to both the API and database configurations.
After adding the label, run the following command in your terminal (from the correct directory):
Review the configuration output to verify that resources from both the API and database directories now include the
department: engineering label.Applying Labels in Subdirectories Versus the Root
If you need to add an additional label only for a specific group of resources, add the extra label in the corresponding subdirectory’s kustomization.yaml. For instance, to assign the labelfeature: api only to API resources, update the API folder’s kustomization.yaml as follows:
department: engineering label and the subdirectory-specific feature: api label. In contrast, the database folder resources will retain only the global labels.
Adding a Namespace to All Configurations
To assign a uniform namespace (such asdebugging) for all resources, the root kustomization.yaml must be updated. For example:
kustomize build k8s/ will produce YAML with every resource set to the debugging namespace.
Configuring Name Prefixes and Suffixes
To modify resource names for easier identification and organization, you can define a common name prefix as well as folder-specific name suffixes.Global Name Prefix
Apply a common prefix (e.g., “KodeKloud-”) in the root kustomization.yaml:Folder-Specific Name Suffixes
For the API folder, include a suffix (e.g.,-web) by updating its kustomization.yaml:
-storage):
KodeKloud-api-deployment-web for API resources and KodeKloud-db-deployment-storage for database resources.
Using both global name prefixes and folder-specific suffixes helps maintain a consistent naming convention across your deployments, making management and identification much easier.
Adding a Common Annotation
To add the same annotation to every resource, include thecommonAnnotations field in the root kustomization.yaml. For example, to add logging: verbose:
logging: verbose annotation in its metadata.
Using the Image Transformer
The image transformer allows you to update container images in your deployment configuration. Suppose your database deployment currently uses MongoDB, and you need to change it to PostgreSQL. In the database folder’s kustomization.yaml, define animages block:
- The transformer only updates the image in the deployment spec, leaving the container name unchanged.
- Always enclose the image tag in quotes (e.g.,
"4.2") to prevent type conversion issues. - Apply the transformer specifically to a subdirectory if you want to limit the scope of the change.
kustomize build k8s/, the database deployment will reference the image postgres:4.2 while retaining the original container name (“mongo”). An excerpt from the output might look like this:
If you encounter an error indicating a type conversion issue, ensure that the image tag value is provided as a string using quotes.
Summary of Kustomize Transformations
Below is a summary table of the transformations we covered in this lesson:| Transformation Type | Purpose | Example Configuration |
|---|---|---|
| Common Labels | Apply a label across all resources | department: engineering |
| Subdirectory Labels | Apply a label to specific resource groups | feature: api or feature: db |
| Namespace Assignment | Set a common namespace for all resources | namespace: debugging |
| Name Prefix/Suffix | Standardize resource names | KodeKloud- / -web or -storage |
| Common Annotations | Add a common annotation to all resources | logging: verbose |
| Image Transformation | Update container images in deployments | Change mongo to postgres:4.2 |
Conclusion
In this lesson, we demonstrated how to:- Organize your Kubernetes configurations using multiple kustomization.yaml files.
- Apply both global and folder-specific labels, namespaces, name prefixes/suffixes, and annotations.
- Use the image transformer to update container images seamlessly.