Learn to modify and manage Kubernetes manifests efficiently using Kustomize’s Common Transformers for consistent changes across multiple YAML files.
In this lesson, you’ll learn how to efficiently modify and manage your Kubernetes manifests using Kustomize. Kustomize streamlines the process of updating common configurations across multiple YAML files by leveraging built-in transformers. This article focuses on a subgroup known as Common Transformers, which help you apply consistent changes such as labels, namespaces, prefixes/suffixes, and annotations without manually updating each resource.Consider an example where you have a basic Deployment and Service defined in separate YAML files:
Suppose you want to add a common configuration—such as a label identifying your organization—to all your Kubernetes resources. Manually editing each YAML file is not scalable, particularly in environments with dozens of resources. Instead, Kustomize applies these common changes automatically using transformers.For example, to add the label “org: KodeKloud” to every resource, you might update your files as follows:
The common label transformer automatically adds a specified label to every resource included in your kustomization.yaml file. For instance, to ensure every resource includes the label “org: KodeKloud”, add the following to your configuration:
Copy
Ask AI
commonLabels: org: KodeKloud
When applied, this transformer injects the label into the metadata of every resource, streamlining updates and reducing human error.
Assigning resources to a specific namespace is simplified with the namespace transformer. By specifying the namespace in your kustomization.yaml, you ensure that all resources are deployed into that namespace. For example, consider the following Service definition:
With the prefix/suffix transformer, you can automatically alter the names of your resources without manually editing every file. For example, to add the prefix “KodeKLOUD-” and suffix “-dev” to a Service name, use the following configuration:
Copy
Ask AI
namePrefix: KodeKLOUD-nameSuffix: -dev
After transformation, the Service’s name will be updated to “KodeKLOUD-api-service-dev”. Here’s an example of the transformed Service resource:
If you need to add common annotations to all Kubernetes objects, you can define these in your kustomization.yaml file. For example, to add the annotation “branch: master” everywhere, your configuration might look like this:
Below is a table summarizing the common transformers available in Kustomize and their use cases:
Transformer
Use Case
Example Configuration
Common Label
Add a label to all resources
commonLabels: { org: KodeKloud }
Namespace
Assign resources to a specific namespace
namespace: lab
Prefix/Suffix
Modify resource names with a prefix or suffix
namePrefix: KodeKLOUD- and nameSuffix: -dev
Common Annotations
Apply consistent annotations to resources
commonAnnotations: { branch: master }
Using these transformers can greatly simplify deployment and maintenance processes, especially when managing a large suite of Kubernetes manifests.For more details on Kubernetes and managing configurations, check out the following resources: