CKA Certification Course - Certified Kubernetes Administrator
2025 Updates Kustomize Basics
Common Transformers
In this article, we explore how to efficiently modify your Kubernetes configurations using Kustomize's built-in transformers. Kustomize allows you to apply common configuration changes across multiple YAML files—whether by adding labels, modifying resource names, setting namespaces, or applying annotations—without editing each file manually.
Below, we outline the challenges these transformers solve and provide detailed examples of several common transformations.
The Problem
Imagine working with multiple Kubernetes resource files, such as a Deployment and a Service. Consider the following initial configuration files:
# db-depl.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 1
selector:
matchLabels:
component: api
template:
metadata:
labels:
component: api
spec:
containers:
- name: nginx
image: nginx
# db-service.yaml
apiVersion: v1
kind: Service
metadata:
name: db-service
spec:
selector:
component: db-depl
ports:
- protocol: "TCP"
port: 27017
targetPort: 27017
type: LoadBalancer
Suppose you want to apply a common configuration change—like adding a label (for example, org: KodeKloud
) or appending a suffix (such as -dev
) to resource names—across all these files. Manually updating each file in a production environment with numerous YAML files can be time-consuming and error-prone. This is where Kustomize's transformers become extremely useful.
Transformations with Kustomize
1. Common Label Transformation
Instead of manually updating each resource, you can add a common label to all Kubernetes resources via your kustomization file. For example, if you want to add the label org: KodeKloud
, you could modify your YAML files as follows:
# db-depl.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 1
selector:
matchLabels:
component: api
org: KodeKloud
template:
metadata:
labels:
component: api
org: KodeKloud
spec:
containers:
- name: nginx
image: nginx
# db-service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
org: KodeKloud
name: api-service
spec:
selector:
component: api
ports:
- protocol: "TCP"
port: 80
targetPort: 3000
type: LoadBalancer
Rather than updating every file individually, add the following to your kustomization file:
# Kustomization.yaml
commonLabels:
org: KodeKloud
This change automatically appends the label to all imported resources.
2. Name Prefix/Suffix Transformation
If you need to append a suffix (like -dev
) to the names of all your resources, you can adjust your resource file names manually or delegate the change to Kustomize. For instance:
# db-depl.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment-dev
spec:
replicas: 1
selector:
matchLabels:
component: api
org: KodeKloud
template:
metadata:
labels:
component: api
org: KodeKloud
spec:
containers:
- name: nginx
image: nginx
Instead of manually updating each resource name, you can let Kustomize handle it with the following configuration:
# Kustomization.yaml
namePrefix: KodeKloud-
nameSuffix: -dev
Kustomize will automatically modify the names of all imported resources to include the specified prefix and suffix.
3. Namespace Transformation
The namespace transformer enables you to group all your Kubernetes resources under a designated namespace. Instead of individually editing each file, simply modify the metadata within your YAML. For example:
# db-service.yaml
apiVersion: v1
kind: Service
metadata:
annotations:
branch: master
labels:
org: KodeKloud
name: api-service
namespace: lab
spec:
ports:
- port: 80
protocol: TCP
targetPort: 3000
selector:
component: api
org: KodeKloud
type: LoadBalancer
This method ensures that the Service is deployed in the specified namespace (lab
in this case).
4. Common Annotations Transformation
When you need to apply common annotations across all your Kubernetes resources, Kustomize makes it easy. For example, to add an annotation like branch: master
, update your resource file:
# db-service.yaml
apiVersion: v1
kind: Service
metadata:
annotations:
branch: master
labels:
org: KodeKloud
name: api-service
namespace: auth
spec:
ports:
- port: 80
protocol: TCP
targetPort: 3000
selector:
component: api
org: KodeKloud
type: LoadBalancer
Then declare it in your kustomization file:
# Kustomization.yaml
commonAnnotations:
branch: master
With this setup, every resource managed by this kustomization automatically receives the annotation.
Conclusion
Kustomize's common transformers significantly streamline the process of updating and maintaining your Kubernetes configurations. By centrally managing transformations—such as adding labels, modifying resource names with prefixes or suffixes, setting namespaces, or applying annotations—you not only reduce manual effort but also minimize the possibility of errors in your production deployments.
Note
Leveraging Kustomize transformers ensures consistent and scalable configuration management, which is essential in dynamic environments with multiple resources.
For more detailed information on Kubernetes and configuration management tools, make sure to explore the following resources:
Watch Video
Watch video content