Learn to use Kustomize transformers for modifying Kubernetes configurations, focusing on common transformations for consistent resource management.
In this lesson, you will learn how to use Kustomize transformers to modify Kubernetes configurations. Kustomize supports several built-in transformers, and you can also create custom ones. Here, we focus on a subgroup known as Common Transformers.Imagine you have multiple YAML files such as deployment.yaml and service.yaml. You might want to apply a common configuration—for example, adding a label like “org: KodeKloud” or appending “-dev” to resource names—across all these files. Manually updating each file in a production environment isn’t scalable or efficient. Kustomize transformers offer a systematic way to make consistent changes across all resources.Below are the original Kubernetes resource examples:
This transformer automatically adds the specified labels to all Kubernetes resources. You can define the labels in your kustomization.yaml file as shown below:
Copy
Ask AI
commonLabels: org: KodeKloud
For example, a transformed Service resource would appear as:
The namespace transformer assigns all Kubernetes resources to a specified namespace. By specifying the namespace in your kustomization.yaml, all resources will be modified accordingly. For example:
Copy
Ask AI
namespace: lab
After this transformation, a Service resource might look like:
This transformer enables you to systematically add a prefix or suffix to resource names. For instance, to prepend “KodeKloud-” and append “-dev” to each resource name, include the following in your kustomization.yaml:
Copy
Ask AI
namePrefix: KodeKloud-nameSuffix: -dev
After applying this configuration, a Service resource would be renamed to “KodeKloud-api-service-dev”:
If you need to add specific annotations to all resources, use the common annotations transformer. By setting the annotations in your kustomization.yaml, each resource will automatically include them. For example:
Copy
Ask AI
commonAnnotations: branch: master
This transformation results in a Service resource similar to:
The common transformations available in Kustomize include:
Transformation Type
Purpose
Common Label Transformation
Adds specified labels (e.g., org: KodeKloud) to all resources
Namespace Transformation
Assigns a specific namespace to all resources
Name Prefix and Suffix
Adds predetermined prefixes and suffixes to resource names
Common Annotation Transformation
Appends specific annotations (e.g., branch: master) to resources
These methods provide a scalable and systematic approach to maintaining consistent configurations across your Kubernetes resources.
In summary, Kustomize transformers offer a robust and error-resistant way to apply common configurations—such as labels, namespaces, name modifications, and annotations—to your Kubernetes resources, ensuring that your deployments remain consistent and manageable across various environments.