Hello, readers. In this article, we will be focusing on Kustomize tool in Kubernetes, in detail.
So, let us begin!! 🙂
Recommended read: Kubernetes Pods
What is Kustomize in kubernetes?
Kubernetes offers us various resources to deal with the containers and manage them. That is, it orchestrates them easily through various resources to build them up.
But while building the resources such as namespaces, containers, etc you may want to add customized labels to differentiate it from the rest of the disciplines. For it to have this behavior, it needs to have different configuration files altogether. And, having different files for every different label does not make sense.
This is when Kustomization comes into the picture. With Kustomization, we can customize the configuration files in such a manner that the different labels can be added at runtime easily without having to code again.
This brings in reusability.
With Kustomize tool, which is a command-line tool introduced by Kubernetes, we can customize the files and execute them at a single click of a command. This also brings the concept of efficiency and automation.
Also read: How to configure Kubernetes configmaps?
Kustomize structure in k8
As discussed above, Kustomization is the reuse of kubernetes resources configurations across various stages and processes at ease.
Thus, to make it practically happen, Kustomize tool makes use of the base and overlays technique. With this, both of these stages represent kustomization files. But, the base stage represents the configuration that works in common such as namespace, labels, annotations, etc. While, on the other hand, overlays include the differences that are the different configuration that needs to be addressed separately.
someapp/ ├── specific/ │ ├── kustomization.yaml │ ├── deployment.yaml │ ├── resourcequota.yaml │ ├── kustomization.yaml ├── namespace.yaml
In the above representation, someapp/kustomization.yaml includes common resources such as namespaces, labels, etc. While someapp/specific/kustomization.yaml represents the specific configuration details such as RBAC, resourcequota, etc.
With this, we can easily patch and un-patch resources on a broader scale and also automate the process of resource creation and configuration.
Like, we can automate the namespace creation process and assign all necessary labels, quotas to it through the kustomization file and structure.
Implementation of Kustomize in Kubernetes k8
Following the above structure, at first let us create someapp/namespace.yaml file–
apiVersion: v1 kind: Namespace metadata: name: nss
Going ahead, we create someapp/kustomization.yaml file as shown below–
When the below file gets executed, it attaches the mentioned labels onto the namespace in a single shot.
commonLabels: environment: dev application: demo istio-injection: enabled resources: - namespace.yaml
Here, we define the resource information that would be bind once the namespace gets created.
apiVersion: v1 kind: ResourceQuota metadata: name: compute-quota spec: hard: requests.cpu: "2" requests.memory: "4Gi" limits.cpu: "4" limits.memory: "8Gi"
4. Finally we define someapp/specific/kustomization.yaml file that would execute the other resources and configure them to the namespace that is being created in the above steps.
namespace: nss commonLabels: environment: dev resources: #Include All quota policies - compute-quota.yaml
Now, having created all the files, we now need to have file that executes
#!/bin/sh read -p "Enter namespace: " ns #echo $ns kubectl kustomize . | sed -e "s/nss/$ns/g" | kubectl apply -f - kubectl kustomize base | sed -e "s/nss/$ns/g" | kubectl apply -f -
As a result, we just execute the sh file which prompts for the name of the namespace. After which, internally, it triggers the creation of namespace with the customized labels provided along with the RBAC and compute quota for the particular namespace.
--sh depoy.sh Enter namespace: INPUT->demo namespace/demo created role.rbac.authorization.k8s.io/edit created rolebinding.rbac.authorization.k8s.io/demosmulani2edit created resourcequota/compute-quota created
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.
For more such posts related Kubernetes, Stay tuned with us.
Till then, Happy Learning!! 🙂