- An API deployment YAML file
- An API service YAML file
- A kustomization.yaml file that imports these resources
- A Database deployment YAML file
- A Database service YAML file
- A configuration file (
config.yaml) - A dedicated kustomization.yaml file that imports the database-related Kubernetes configurations
Adding a Common Label
To streamline management, you can add a common label to every resource in your environment. By updating the root kustomization.yaml file, you can apply a global label. For example, add the labeldepartment: engineering as shown below:
department: engineering label.
Applying Labels in Subdirectory kustomization.yaml Files
In some scenarios, you might prefer to apply a label only to resources defined within a specific folder. For example, to add a labelfeature: api specifically for the API components, update the API folder’s kustomization.yaml as follows:
department: engineering (from the root configuration) and feature: api (from the subdirectory). In contrast, resources in the database folder will not show the feature: api label.
Similarly, to add a label for the database folder, include the following in its kustomization.yaml:
feature: db label.
Adding a Namespace
Applying a namespace to all resources is as simple as adding thenamespace field to the root kustomization.yaml file. For example, to assign the namespace debugging, update the file accordingly:
kustomize build, every resource—including those from the API and database folders—will have the namespace debugging.
Setting Name Prefixes and Suffixes
You may also want to add a name prefix and folder-specific suffix to your resources. Suppose you want to prefix every resource’s name withKodeKloud- and apply a suffix of -web to API objects and -storage to database objects.
To add a global name prefix, include it in the root kustomization.yaml:
KodeKloud-api-deployment-web and the database deployment KodeKloud-db-deployment-storage.
Adding a Global Annotation
To attach an annotation to every resource, include thecommonAnnotations field in the root kustomization.yaml file. For instance, to add an annotation logging: verbose, update the file as follows:
logging: verbose in its metadata.
Using an Image Transformer
The image transformer is a powerful feature that enables you to modify container image references directly in your configuration files. Suppose the current database deployment uses the Mongo image and you want to replace it with Postgres. Open the kustomization.yaml file in the database folder and add the following image transformer section:If you encounter an error like:ensure that the tag value is quoted so that it is processed as a string.
Conclusion
In this lesson, we demonstrated various techniques to modify Kubernetes configurations using Kustomize transformations. The key topics covered include:- Importing resources using kustomization.yaml files
- Applying common labels and annotations globally and at the folder level
- Setting namespaces for resource segmentation
- Modifying resource names with prefixes and folder-specific suffixes
- Transforming container images using the image transformer