Skip to main content
In this lesson, we explore how to use several common transformations in Kustomize, including the image transformer. The demo utilizes a structured Kubernetes (K8s) directory setup containing two folders—one for API components and one for database components. At the API level, you will find:
  • An API deployment YAML file
  • An API service YAML file
  • A kustomization.yaml file that imports these resources
Similarly, the database folder contains:
  • 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
At the root level, the main kustomization.yaml brings together both the API and database directories:

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 label department: engineering as shown below:
After saving the file, execute the following command in your terminal to build the configurations:
The output confirms that every resource (such as ConfigMap, Service, and Deployment) now has the new label applied. Here is a snippet of the generated output:
Every resource within both the API and database folders now includes the 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 label feature: api specifically for the API components, update the API folder’s kustomization.yaml as follows:
When you run the build command, resources in the API directory will display both 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:
After building, the database deployment, service, and config map will reflect the feature: db label.

Adding a Namespace

Applying a namespace to all resources is as simple as adding the namespace field to the root kustomization.yaml file. For example, to assign the namespace debugging, update the file accordingly:
After running 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 with KodeKloud- 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:
Then, within each subdirectory’s kustomization.yaml, add a name suffix. For the API folder, update the file as follows:
For the database folder:
After building the configurations, the API deployment might be named KodeKloud-api-deployment-web and the database deployment KodeKloud-db-deployment-storage.

Adding a Global Annotation

To attach an annotation to every resource, include the commonAnnotations field in the root kustomization.yaml file. For instance, to add an annotation logging: verbose, update the file as follows:
Every resource will now feature the annotation 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:
This configuration replaces any reference to the Mongo image with the Postgres image tagged as “4.2”. Note that only the image reference is modified—the container name remains unchanged.
If you encounter an error like:
ensure that the tag value is quoted so that it is processed as a string.
After applying the image transformation, the final rendered output for the database deployment will display the updated container image:
Other image references remain unchanged.

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
In the next section, you will have the opportunity to participate in a lab exercise to gain hands-on experience with these Kustomize transformations and further refine your Kubernetes configurations. For more detailed information on Kubernetes configuration management, visit the Kubernetes Documentation.

Watch Video

Practice Lab