Kustomize introduces a template-free way to customize application configuration that simplifies the use of off-the-shelf applications. Now, built into kubectl as apply -k.
official doc:
https://kustomize.io/
kustomization will come automatically in bundle where kubectl version is above 1.14
C:\Users\shiva>kubectl version
Client Version: v1.29.2
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Server Version: v1.30.0
Lets start:
https://kubectl.docs.kubernetes.io/guides/introduction/kustomize/
Deploy a mongodb and mango application in kustomization
Source code;
https://github.com/shivscloud/rajesh_kubernetes_app/blob/main/k8/mongo.yml
Refer more:
https://github.com/kubernetes-sigs/kustomize/tree/master/examples
I have create the flow like below
$ cd kustomize/
shiva@BhagathSingh MINGW64 /d/SingamsHUB/app/k8/kustomize (main)
$ ls -l
total 7
-rw-r--r-- 1 shiva 197609 1320 Aug 17 22:02 deploy.yml
-rw-r--r-- 1 shiva 197609 72 Aug 17 22:01 namespace.yml
-rw-r--r-- 1 shiva 197609 242 Aug 17 22:01 secret.yml
-rw-r--r-- 1 shiva 197609 222 Aug 17 22:02 service.yml
Build the Kustomize
After hitting the below command kustomize will build the yaml files it will return the k8 manifest code like below
$ ls
deploy.yml kustomization.yaml namespace.yml secret.yml service.yml
shiva@BhagathSingh MINGW64 /d/SingamsHUB/app/k8/kz (main)
$ kubectl kustomize .
- env:
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef:
key: username
name: mongo-secret
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
key: password
name: mongo-secret
image: mongo:5.0
name: mongo
ports:
- containerPort: 27017
resources:
limits:
cpu: "1"
memory: 1Gi
requests:
cpu: 500m
memory: 512Mi
volumeMounts:
- mountPath: /data/db
name: mongo-data
volumes:
- name: mongo-data
persistentVolumeClaim:
claimName: mongo-pvc
kustomize apply
shiva@BhagathSingh MINGW64 /d/SingamsHUB/app/k8/kz (main)
$ kubectl kustomize . | kubectl apply -f -
namespace/mongodb created
secret/mongo-secret created
service/mongo-service created
persistentvolumeclaim/mongo-pvc created
deployment.apps/mongo-deployment created
Now you can see all the pods and services are running
Successfully mango app running
Transformer Configurations
Kustomize creates new resources by applying a series of transformations to an original set of resources. Kustomize provides the following default transformers:
- annotations
- images
- labels
- name reference
- namespace
- prefix/suffix
- variable reference
If you change anything in kustomize.yml file it will automatically applies inside all the files of kustomization
this are the item(commonLabels,namePrefix,namePrefix) added in k.yml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: kustomizenamespace
commonLabels:
environment: kustomizeprod
namePrefix: singamprod-
metadata:
name: singam-hub-customize
commonLabels:
app: singam-hub
resources:
- deploy.yml
this all values we passed in kustomize file so its overriding
Image Transformer
https://github.com/kubernetes-sigs/kustomize/blob/master/examples/transformerconfigs/images/README.md
The default images transformer updates the specified image key values found in paths that include containers
and initcontainers
sub-paths. If found, the image
key value is customized by the values set in the newName
, newTag
, and digest
fields. The name
field should match the image
key value in a resource.
=====The above process we used to append the common changes across all k8 manifest======
========================Pathes==============================
Useful to patch the specific k8 manifest where as above way it will applicable for all k8 manifest
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
# namespace: kustomizenamespace
# commonLabels:
# environment: kustomizeprod
# namePrefix: singamprod-
metadata:
name: singam-hub-customize
commonLabels:
app: singam-hub
resources:
- deploy.yml
patches:
- target:
kind: Deployment # i'm updating the specific deployment called the mongo-deployment
name: mongo-deployment
- patch: |-
- op: replace # it means operation
path: spec/replicas # specific path
value: 3 # changing the value from replicas 1 to 3
Proof: ensure run kubectl kustomize .
Strategic merging
In this patching it will check the origibal k8 manifest if there is any changes which we mention in kustimization yml file it will merge the code
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
# namespace: kustomizenamespace
# commonLabels:
# environment: kustomizeprod
# namePrefix: singamprod-
metadata:
name: singam-hub-customize
commonLabels:
app: singam-hub
resources:
- deploy.yml
patches:
- target:
kind: Deployment # i'm updating the specific deployment called the mongo-deployment
name: mongo-deployment
- patch: |-
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy
spec:
template:
spec:
containers:
- name: nginx
0 Comments