Skip to content

Kustomize Lab📜

Lab Overview📜

In this lab we use kustomize CLI tool for templating kubernetes .yaml files, but first we will build out the directory structure and populate the necessary files.

Finally, we will compare the files that the kustomize tool generated to understand how kustomize is implemented

Kustomize📜

Lab📜

  1. Verify kustomize is installed

    kustomize version
    which kustomize
    

Warning

For Ubuntu users (if kustomize was installed via snap) you’ll see errors, so let’s verify

The which command tells you the path where the kustomize cli tool is installed

If you see snap in the path, uninstall it and reinstall using the directions on this page

  1. Create the file structure

    cd ~/Desktop/configlabs
    mkdir -p sample-app/base
    mkdir -p sample-app/overlays/development
    mkdir -p sample-app/overlays/production
    
  2. Create the base deployment yaml and open to edit

    vi sample-app/base/deployment.yaml
    
  3. Add base pod to the file and save

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: hello-world
    spec:
      replicas: 1
      selector:
        matchLabels:
         app: hello-world
      template:
        metadata:
          labels:
            app: hello-world
        spec:
          containers:
          - name: hello-world
            image: nginx
            resources:
              limits:
                memory: "128Mi"
                cpu: "500m"
            ports:
            - containerPort: 80
    
  4. Create the base kustomization.yaml and open to edit

    vi sample-app/base/kustomization.yaml
    
  5. Add the following contents and save

    # Specifies the files to include when kustomize builds the application
    resources:
    - deployment.yaml
    

    Create an overlay file and open to edit

    vi sample-app/overlays/development/replica_count.yaml
    
  6. Add the following contents and save

    # Overlay to increase the replicaCount
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: hello-world
    spec:
      replicas: 3
    
  7. Create an overlay kustomization file and open to edit

    vi sample-app/overlays/development/kustomization.yaml
    
  8. Add the following contents

    # Specifies the base files
    bases:
    - ../../base
    
    # Specifies overrides to the base files
    patches:
    - replica_count.yaml
    
  9. Generate Kustomize output

    kustomize build sample-app/overlays/development 
    cat sample-app/base/deployment.yaml
    

    Compare the 2 outputs and notice the replicaCount of 3 was overlaid onto the base file

  10. Generate a production deployment with kustomize

    Create the kustomization.yaml and replica_count.yaml for production environment

    vi sample-app/overlays/production/kustomization.yaml
    
    Add the following contents
    bases:
    - ../../base
    
    patches:
    - replica_count.yaml
    

    vi sample-app/overlays/production/replica_count.yaml
    
    Add the following contents
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: hello-world
    spec:
      replicas: 200
    

Now create the deployment file for our production environment📜

  kustomize build sample-app/overlays/production

This should output the following to the terminal:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: hello-world
    spec:
      replicas: 200
      selector:
        matchLabels:
          app: hello-world
      template:
        metadata:
          labels:
            app: hello-world
        spec:
          containers:
          - image: nginx
            name: hello-world
            ports:
            - containerPort: 80
            resources:
              limits:
                cpu: 500m
                memory: 128Mi