It can be difficult to navigate the complicated terrain of Kubernetes deployment across numerous contexts. However, with the powerful Kustomize tool at your disposal, you have the ability to easily and precisely manage Kubernetes manifests. In this investigation, we'll look at how to use Kustomize to simplify configuration management, ensure smooth deployments, and preserve consistency across several environments. Join me as we explore the possibility of simpler Kubernetes manifest management. Let's get started.

What is Kustomize

kustomize is a command-line tool that enables template-free, structured declarative configuration customisation for k8s-style objects. It allows you to deploy your kubernetes configuration file across multiple environments easily. You may utilize Kustomize to reuse one of the base files across all environments (development, staging, production, and so on) and overlay requirements for each of those environments. Kustomize is also compatible with helm and CD solutions such as argo CD.

kustomization.yaml

Each directory includes a kustomization.yaml file, which is effectively a collection of resources or manifests that specify how to produce or alter Kubernetes objects.

Kustomize allows you to customize raw, template-free YAML files, allowing you to easily change settings/annotations between deployment and production.

Kustomize supports two methods for applying patches.

  • patchesStrategicMerge (most common and easy to use merge strategy)
  • patchesJson6902
  • base folder

    The base folder contains shared resources including deployment.yaml, service.yaml, and configmap.yaml. It contains the initial manifest as well as the resources' namespace and label.

    1
    2
    3
    4
    5
    kustomize
    ├── base
    | ├── deployment.yaml
    | ├── service.yaml
    │ ├── kustomization.yaml

    overlays

    The overlays folder contains environment-specific overlays that employ patches to specify and overlaid YAML files on top of the base for any modifications.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    kustomize
    ├── base
    │ ├── deployment.yaml
    │ ├── kustomization.yaml
    └── overlays
    ├── dev
    │ ├── deployment-patch.yaml
    │ ├── kustomization.yaml
    └── prod
    ├── deployment-patch.yaml
    ├── kustomization.yaml

    Preview and apply manifests

    We can preview the kustomize output using kustomize build command.

    1
    2
    3
    4
    5
    # preview output
    kustomize build overlays/dev

    # apply output to kubernetes
    kustomize build overlays/dev | kubectl apply -f -

    Conclusion

    In this article, we learned how to manage Kubernetes manifests for multiple environments using Kustomize. You can find the all the related scripts and code for this tutorial from here. If you have any issue regarding this tutorial, mention your issue in the comment section or reach me through my E-mail.

    Happy Coding