Kustomize

4 minutes read
  1. Create a directory structure for a Kustomize project with:

    • a base layer
    • a development and a production overlays
  2. Create resources in the base layer:

    • A Deployment named webapp using nginx:1.26 image with 2 replicas
    • A Service named webapp-service exposing port 80
  3. Create a kustomization.yaml file in the base directory that references both resources

  4. Apply the base configuration

  5. Configure the development overlay:

    • Changes the image to nginx:1.28
    • Adds a env: development label to all resources
    • Sets the replica count to 1
  6. Preview the development overlay using kubectl kustomize without applying

  7. Apply the development overlay

  8. Create a production overlay that:

    • Changes the image to nginx:1.28-alpine
    • Adds a env: production label to all resources
    • Sets the replica count to 5
    • Changes the service type to LoadBalancer
  9. Preview the production overlay

  10. Apply the production overlay

  11. Verify the production changes

  12. Delete the resources

https://kubectl.docs.kubernetes.io/guides/introduction/kustomize/

  1. Create a directory structure for the Kustomize project
mkdir -p base
mkdir -p overlays/development
mkdir -p overlays/production
  1. Create resources in the base layer:

Create the following specifications in the base folder:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: webapp
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: nginx:1.26
        ports:
        - containerPort: 80
apiVersion: v1
kind: Service
metadata:
  name: webapp-service
spec:
  selector:
    app: webapp
  ports:
  - port: 80
    targetPort: 80
  type: ClusterIP
  1. Create a kustomization.yaml file in the base directory that references both resources
resources:
- deployment.yaml
- service.yaml
  1. Apply the base configuration
$ kubectl apply -k base
service/webapp-service created
deployment.apps/webapp created
  1. Configure the development overlay:

In the overlays/development folder, create the following kustomization.yaml file:

resources:
- ../../base

labels:
- pairs:
    env: development

patches:
- patch: |-
    - op: replace
      path: /spec/template/spec/containers/0/image
      value: nginx:1.28
    - op: replace
      path: /spec/replicas
      value: 1
  target:
    kind: Deployment
    name: webapp

This file specifies:

  • the base resources to use
  • the labels to add
  • the patches that need to be applied to the base resources to change the image and the number of replicas
  1. Preview the development overlay:

kubectl has the kustomize built-in command for that purpose:

kubectl kustomize overlays/development

You should see the resulting Deployment’s specification is now based on nginx:1.28 and has 1 replica.

  1. Apply the development overlay:
$ k apply -k overlays/development           
service/webapp-service configured
deployment.apps/webapp configured
  1. Configure the production overlay

In the overlays/production folder, create the following kustomization.yaml file:

resources:
- ../../base

labels:
- pairs:
    env: production

patches:
- patch: |-
    - op: replace
      path: /spec/template/spec/containers/0/image
      value: nginx:1.28-alpine
    - op: replace
      path: /spec/replicas
      value: 5
  target:
    kind: Deployment
    name: webapp
- patch: |-
    - op: replace
      path: /spec/type
      value: LoadBalancer
  target:
    kind: Service
    name: webapp-service
  1. Preview the production overlay:
kubectl kustomize overlays/production
  1. Apply the production overlay:
kubectl apply -k overlays/production
  1. Verify the production changes:

Checking deployment:

kubectl get deployment webapp -o wide
kubectl get pods -l app=webapp

You should see 5 replicas running nginx:1.28-alpine.

Checking service:

kubectl get service webapp-service

You should see the Service type is now LoadBalancer.

Checking labels:

kubectl get deployment webapp --show-labels
kubectl get service webapp-service --show-labels

You should see the env: production label applied to both the Deployment and the Service.

  1. Clean up:
kubectl delete -k overlays/production