> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Image Transformers

> Learn to modify container images in Kubernetes deployments using Kustomize's image transformer feature for consistent updates across manifests.

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.

<Callout icon="lightbulb" color="#1CB2FE">
  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.
</Callout>

Below is an example deployment that initially uses the default NGINX image:

```yaml theme={null}
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.

```yaml theme={null}
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:

```yaml theme={null}
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:

```yaml theme={null}
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:

```yaml theme={null}
images:
  - name: nginx
    newTag: 2.4
```

After applying this transformation, your final deployment configuration will reference the image as `nginx:2.4`:

```yaml theme={null}
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:

```yaml theme={null}
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:

```yaml theme={null}
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:

* [Kubernetes Documentation](https://kubernetes.io/docs/)
* [Kubernetes Basics](https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/)
* [Docker Hub](https://hub.docker.com/)

<Callout icon="lightbulb" color="#1CB2FE">
  Always test your kustomization changes in a development environment before applying them to production. This helps prevent accidental misconfigurations.
</Callout>

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/cka-certification-course-certified-kubernetes-administrator/module/031e84b8-bcbc-4f39-94d6-66d93b05bddc/lesson/632a781d-9494-4120-80d3-e7e86b4e88cf" />
</CardGroup>
