CKA Certification Course - Certified Kubernetes Administrator

2025 Updates Kustomize Basics

kustomization

In this lesson, we explore the Kustomize tool and the essential kustomization.yaml file. Kustomize streamlines the customization of Kubernetes resource files without modifying the original YAML manifests. The kustomization.yaml file acts as the entry point, directing Kustomize to your Kubernetes configurations and specifying the required customizations.

Directory Structure and File Overview

Assume you have a directory named "K8s" that contains two YAML files: one for an nginx deployment and another for an nginx service. Kustomize does not process every file by default; it only considers files listed in the kustomization.yaml, which you must create manually.

Structure of kustomization.yaml

The kustomization.yaml file has two main sections:

  1. Kubernetes Resources: A list of resource files that Kustomize should manage.
  2. Customizations/Transformations: Definitions for transformations to apply to these resources.

Below is an example configuration for the kustomization.yaml file:

# Kubernetes resources to be managed by Kustomize
resources:
  - nginx-deployment.yaml
  - nginx-service.yaml

# Customizations to be applied to all resources
commonLabels:
  company: KodeKloud

Note

The "resources" section tells Kustomize which YAML files to include, while the "commonLabels" section adds a consistent label (key: company, value: KodeKloud) to all the resources. Kustomize supports various other complex transformations beyond adding labels.

Generating the Final Configuration

After creating your kustomization.yaml, you can generate the final Kubernetes configuration by navigating to your "K8s" directory and running:

$ kustomize build k8s/

This command processes the specified resources and applies the defined customizations, outputting the final configuration to your terminal. An example output may look like this:

apiVersion: v1
kind: Service
metadata:
  labels:
    company: KodeKloud
  name: nginx-loadbalancer-service
spec:
  ports:
    - port: 80
      protocol: TCP
      targetPort: 3000
  selector:
    company: KodeKloud
    component: nginx
  type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    company: KodeKloud
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      company: KodeKloud
      component: nginx
  template:
    metadata:
      labels:
        company: KodeKloud
        component: nginx
    spec:
      containers:
        - image: nginx
          name: nginx

The transformation process integrates the "company: KodeKloud" label into both the nginx service and deployment configurations.

Deployment Considerations

While Kustomize outputs the fully rendered configurations to the terminal, it does not deploy the resources directly. To deploy the rendered configuration to your Kubernetes cluster, you can pipe the output to the kubectl apply command. For example:

$ kustomize build k8s/ | kubectl apply -f -

Warning

Remember that this command only outputs and then applies the configurations to your Kubernetes cluster. Always review the generated YAML before deployment to ensure all customizations are correctly applied.

Summary

  • The kustomization.yaml file is critical for Kustomize as it lists the Kubernetes resource manifests and the customizations to apply.
  • Running kustomize build k8s/ processes the specified resources, applies the transformations, and outputs the final configuration.
  • To deploy the configurations, pipe the output to kubectl apply, ensuring that your cluster receives the fully rendered YAML.

The image explains Kustomize, detailing its use of kustomization files, the `kustomize build` command, and its non-deployment nature for Kubernetes resources.

For further guidance on deploying Kubernetes resources, refer to our subsequent sections on Kubernetes configuration and deployment practices.

Watch Video

Watch video content

Previous
InstallationSetup