Published on

Kustomize

Authors
  • avatar
    Name
    Galuh Pradipta
    Twitter

How to Use Kustomize

Kustomize is a tool that allows you to customize Kubernetes manifests without modifying the original YAML files. This makes it easy to manage and version your Kubernetes deployments. In this tutorial, we will go through the steps to use Kustomize to deploy a sample application on a Kubernetes cluster.

Prerequisites

  • A Kubernetes cluster
  • Kustomize installed on your local machine

Step 1: Create the Base Manifests

Create a directory named base in your local machine. This directory will contain the base manifests for your application. In this example, we will create a deployment and a service manifest for a sample application.

Create a file named deployment.yaml inside the base directory with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sample-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sample-app
  template:
    metadata:
      labels:
        app: sample-app
    spec:
      containers:
        - name: sample-app
          image: nginx
          ports:
            - containerPort: 80

Create a file named service.yaml inside the base directory with the following content:

apiVersion: v1
kind: Service
metadata:
  name: sample-app
spec:
  ports:
    - port: 80
      targetPort: 80
  selector:
    app: sample-app

Step 2: Create the Overlays

Create a directory named overlays in your local machine. This directory will contain the overlays for your application. In this example, we will create two overlays, one for the development environment and one for the production environment.

Create a directory named dev inside the overlays directory. This directory will contain the overlays for the development environment.

Create a file named deployment.yaml inside the dev directory with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sample-app
spec:
  replicas: 3
  template:
    spec:
      containers:
        - name: sample-app
          image: nginx:1.19.10-alpine

Create a file named service.yaml inside the dev directory with the following content:

apiVersion: v1
kind: Service
metadata:
  name: sample-app
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
  selector:
    app: sample-app

Create a directory named prod inside the overlays directory. This directory will contain the overlays for the production environment.

Create a file named deployment.yaml inside the prod directory with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sample-app
spec:
  replicas: 5
  template:
    spec:
      containers:
      - name: sample-app
        image: nginx:1.19.10-alpine

Create a file named service.yaml inside the prod directory with the following content:

apiVersion: v1
kind: Service
metadata:
  name: sample-app
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: sample-app

Step 3: Use Kustomize to Deploy the Application

Create a file named kustomization.yaml in the root directory of your project with the following content:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- base

namespace: sample-app

commonLabels:
  app: sample-app

namePrefix: dev-

patchesStrategicMerge:
- overlays/dev/deployment.yaml
- overlays/dev/service.yaml

---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- base

namespace: sample-app

commonLabels:
  app: sample-app

namePrefix: prod-

patchesStrategicMerge:
- overlays/prod/deployment.yaml
- overlays/prod/service.yaml

The kustomization.yaml file includes the following:

  • resources: specifies the base manifests directory
  • namespace: specifies the namespace for the manifests
  • commonLabels: specifies the common labels for all the manifests
  • namePrefix: specifies the prefix for the deployment and service names
  • patchesStrategicMerge: specifies the overlays for the development and production environments

Now, you can deploy the application to the development environment by running the following command:

$ kubectl apply -k .

To deploy the application to the production environment, run the following command:

$ kubectl apply -k . -f overlays/prod/

Congratulations! You have successfully used Kustomize to deploy an application on a Kubernetes cluster.

Conclusion

In this tutorial, we have gone through the steps to use Kustomize to deploy a sample application on a Kubernetes cluster. Kustomize is a powerful tool that simplifies the management and versioning of Kubernetes manifests. It allows you to customize your deployments without modifying the original YAML files, making it easy to maintain and update your Kubernetes applications.