CKA Certification Course - Certified Kubernetes Administrator
Image Transformers
In this lesson, you'll learn how to modify container images in Kubernetes deployments using Kustomize. The image transformer feature enables you to update an image's name or tag across your deployment manifests without manually editing each file.
Tip
Using Kustomize's image transformer simplifies the process of updating container images. This method ensures that all instances of the image in your manifests are consistently updated.
Below is an example deployment that initially uses the default NGINX image:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deployment
spec:
replicas: 1
selector:
matchLabels:
component: web
template:
metadata:
labels:
component: web
spec:
containers:
- name: web
image: nginx
Changing the Image Name
To change the image from NGINX to another image (for example, HAProxy), update your kustomization.yaml
file with the following configuration. In this file, the name
field specifies the target image to replace, while the newName
field defines the new image.
images:
- name: nginx
newName: haproxy
When you apply this kustomization, Kustomize scans all Kubernetes manifests for containers using the nginx
image and replaces them with haproxy
. The transformed deployment will look like this:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deployment
spec:
replicas: 1
selector:
matchLabels:
component: web
template:
metadata:
labels:
component: web
spec:
containers:
- name: web
image: haproxy
Note: The container name defined in the deployment (in this case,
web
) is independent of the image transformer operation. The transformer only targets the image name mentioned in the kustomization file.
Changing the Image Tag
If you wish to update the image tag rather than the image name—for instance, changing the tag from the default to 2.4
—you can specify the newTag
property in your kustomization.yaml
file.
Start with the same original deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deployment
spec:
replicas: 1
selector:
matchLabels:
component: web
template:
metadata:
labels:
component: web
spec:
containers:
- name: web
image: nginx
Update your kustomization.yaml
file to include the new tag:
images:
- name: nginx
newTag: 2.4
After applying this transformation, your final deployment configuration will reference the image as nginx:2.4
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deployment
spec:
replicas: 1
selector:
matchLabels:
component: web
template:
metadata:
labels:
component: web
spec:
containers:
- name: web
image: nginx:2.4
Combining Image Name and Tag Updates
You can combine both the image name and tag updates in a single configuration. For example, to change the image from NGINX to HAProxy and update its tag to 2.4
, configure your kustomization.yaml
as shown below:
images:
- name: nginx
newName: haproxy
newTag: 2.4
This setup instructs Kustomize to update all occurrences of the nginx
image to haproxy:2.4
, resulting in the following deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deployment
spec:
replicas: 1
selector:
matchLabels:
component: web
template:
metadata:
labels:
component: web
spec:
containers:
- name: web
image: haproxy:2.4
Conclusion
Leveraging Kustomize's image transformer provides a flexible and efficient method to manage container images in your Kubernetes deployments. Whether you need to update the image name, tag, or both, this approach eliminates the need for manual edits across multiple files, ensuring consistency and reducing potential errors in your configuration.
For more details on Kubernetes deployments and best practices, check out the following resources:
Remember
Always test your kustomization changes in a development environment before applying them to production. This helps prevent accidental misconfigurations.
Watch Video
Watch video content