Kustomize and overlay in Kubernetes Open SHift - unix1998/technical_notes GitHub Wiki

Kustomize and overlays are related concepts in Kubernetes that help manage and customize Kubernetes resources declaratively. Here's an overview of each:

Kustomize

Kustomize is a configuration management tool built into kubectl that allows you to customize Kubernetes resources without modifying the original YAML files. It provides a way to manage configuration variants using a kustomization.yaml file.

Key Features of Kustomize:

  1. No Template Language:

    • Unlike Helm, Kustomize does not use a template language. Instead, it uses plain YAML and layering to customize configurations.
  2. Declarative Management:

    • Kustomize allows you to manage Kubernetes configurations declaratively, meaning you specify the desired state rather than how to achieve it.
  3. Layering:

    • You can define a base configuration and overlay customizations on top of it for different environments (e.g., development, staging, production).
  4. Built-in Support in kubectl:

    • Kustomize is integrated into kubectl, so you can use it directly with commands like kubectl apply -k.

Basic Kustomize Example:

Suppose you have a base configuration for a Deployment and a Service.

  • base/deployment.yaml:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: myapp
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: myapp
      template:
        metadata:
          labels:
            app: myapp
        spec:
          containers:
          - name: myapp
            image: myapp:latest
            ports:
            - containerPort: 80
    
  • base/service.yaml:

    apiVersion: v1
    kind: Service
    metadata:
      name: myapp
    spec:
      selector:
        app: myapp
      ports:
      - protocol: TCP
        port: 80
        targetPort: 80
    
  • base/kustomization.yaml:

    resources:
    - deployment.yaml
    - service.yaml
    

Overlays

Overlays in Kustomize are used to customize or extend the base configurations for different environments or purposes without duplicating the entire configuration.

Overlays Example:

You might have different configurations for development and production environments. You can create overlays to modify the base configuration accordingly.

  • overlays/dev/kustomization.yaml:

    resources:
    - ../../base
    
    patchesStrategicMerge:
    - deployment-patch.yaml
    
  • overlays/dev/deployment-patch.yaml:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: myapp
    spec:
      replicas: 2
      template:
        spec:
          containers:
          - name: myapp
            image: myapp:dev
    
  • overlays/prod/kustomization.yaml:

    resources:
    - ../../base
    
    patchesStrategicMerge:
    - deployment-patch.yaml
    
  • overlays/prod/deployment-patch.yaml:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: myapp
    spec:
      replicas: 5
      template:
        spec:
          containers:
          - name: myapp
            image: myapp:prod
    

Applying Kustomize Configurations

To apply the configuration for a specific environment, you use the -k flag with kubectl:

  • Apply the development configuration:

    kubectl apply -k overlays/dev
    
  • Apply the production configuration:

    kubectl apply -k overlays/prod
    

Summary

  • Kustomize is a tool for customizing Kubernetes configurations using plain YAML files and a kustomization.yaml file to manage configuration variants.
  • Overlays are used in Kustomize to customize base configurations for different environments or purposes without duplicating the entire configuration.
  • Kustomize is integrated into kubectl, making it easy to use for declarative configuration management and environment-specific customization.