Skip to content

rara-tan/k8s-kustomization-demo

Repository files navigation

What is This Repo

To demonstrate how to use Kubernetes kusomization.

Pre Requirements

install kubectl & kind

https://kubernetes.io/docs/tasks/tools/

Create Kubernetes Cluster

I recommend a kind cluster.(any k8s cluster is OK)

$ kind create cluster -n kustomization-demo

Basic Usage

Basic usage of kustomization is deploying several Kubernetes resources at once.
At a test, Let's apply deployment and service files at onece.

$ kubectl apply -k ./basic

After applying, you can find a deployment(test-app) and a service(test-app) on your kubernetes cluster.

$ kubectl get deploy
NAME       READY   UP-TO-DATE   AVAILABLE   AGE
test-app   1/1     1            1           39s

$ kubectl get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP   55m
test-svc     ClusterIP   10.96.83.182   <none>        80/TCP    39s

If you want to check yaml file that will be created by kustomization, you run a dry-run command.

$ kubectl apply -k ./basic --dry-run=client -o yaml

Or if you installed kustomize command, you can use kustomize build commnad.

$ kustomize build ./basic

patch replicas

You can change the number of deployment replica by using kustomization.

patch/kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ./service.yaml
- ./deployment.yaml
replicas:
- name: patch-replica
  count: 3

By adding replicas to the kusomization file, you can change the number of replica count.

$ kubectl apply -k ./patch-replica
service/patch-replica-svc created
deployment.apps/patch-replica created

$ kubectl get deploy
NAME            READY   UP-TO-DATE   AVAILABLE   AGE
patch-replica   3/3     3            3           44s
test-app        1/1     1            1           9m38s

patch other parametes

To change other parameters, Kustomize supports patch to modify Kubernetes reosources.

For example, you can change a namespace and add a label by following codes.

patch-other-params/kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ./deployment.yaml
- ./namespace.yaml
patches:
  - target:
      version: v1
      group: apps
      kind: Deployment
      name: patch-other
    patch: |-
      - op: replace
        path: /metadata/namespace
        value: patch-other-parameters
      - op: add
        path: "/metadata/labels/patch"
        value: patched-value
$ kubectl apply -k ./patch-other-params
namespace/patch-other-parameters created
service/patch-other-svc created
deployment.apps/patch-other created

$ kubectl get deploy -n patch-other-parameters
NAME          READY   UP-TO-DATE   AVAILABLE   AGE
patch-other   1/1     1            1           23s

patch by files

Instead of embedding a json patch expression, you can use a yaml file using the path field.

patch-by-files/kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ./deployment.yaml
- ./namespace.yaml
patches:
  - target:
      version: v1
      group: apps
      kind: Deployment
      name: patch-by-file
    path: ./patch.yaml
$ kubectl apply -k ./patch-by-files
namespace/patch-by-files created
deployment.apps/patch-by-file created

$ kubectl get deploy -n patch-by-files
NAME            READY   UP-TO-DATE   AVAILABLE   AGE
patch-by-file   1/1     1            1           87s

Strategic Merge Patch

You can use an incomplete YAML file to patch.

For example, you can add resource requests and limits to the deployment by following expressions.

strategic-merge-patch/patch.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: strategic-merge-patch
spec:
  template:
    spec:
      containers:
      - name: nginx
        resources:
          limits:
            cpu: "300m"
            memory: "512Mi"
          requests:
            cpu: "300m"
            memory: "512Mi"

strategic-merge-patch/kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ./deployment.yaml
patchesStrategicMerge:
  - patch.yaml
$ kubectl apply -k ./strategic-merge-patch 
deployment.apps/strategic-merge-patch created

$ kubectl get deploy strategic-merge-patch -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: strategic-merge-patch
  ...
spec:
  ...
    spec:
      containers:
      - image: nginx
        imagePullPolicy: Always
        name: nginx
        resources:
          limits:
            cpu: 300m
            memory: 512Mi
          requests:
            cpu: 300m
            memory: 512Mi
...

Config Map Generator

By using configMapGenerator, you can manage several configmap values by kustomization files.

The follwing example is showing that test-cm ConfigMap is created by kustomization.

configmap-generator/kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ./deployment.yaml
configMapGenerator:
- name: test-cm
  literals:
  - env=prod

configmap-generator/deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: patch-other
  name: patch-other
spec:
  replicas: 1
  selector:
    matchLabels:
      app: patch-other
  template:
    metadata:
      labels:
        app: patch-other
    spec:
      containers:
      - image: nginx
        name: nginx
        env:   
        - name: ENV
          valueFrom:
            configMapKeyRef:
              name: test-cm
              key: env

Let's check the build result.

$ kustomize build ./configmap-generator
apiVersion: v1
data:
  env: prod
kind: ConfigMap
metadata:
  name: test-cm-b7k5b48kg7
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: patch-other
  name: patch-other
spec:
  replicas: 1
  selector:
    matchLabels:
      app: patch-other
  template:
    metadata:
      labels:
        app: patch-other
    spec:
      containers:
      - env:
        - name: ENV
          valueFrom:
            configMapKeyRef:
              key: env
              name: test-cm-b7k5b48kg7
        image: nginx
        name: nginx

Kustomization hashes configmap name. So after editing any config map values, the configmap name will change. It leads to trigger rolling update when this kustomization manifests are applied.

Of course you can use separate files to create configmaps as below.

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ./deployment.yaml
configMapGenerator:
- name: test-cm
  files:
  - ./cm1.yaml
  - ./cm2.yaml

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published