In this article we are going to cover Kubernetes Kustomize Tutorial with Examples, How to Deploy Go App on minikube using kustomize.
Table of Contents
What is Kustomize?
Kustomize is a tool for managing and customizing configurations for applications deployed on Kubernetes. It offers a declarative approach, meaning you define what you want to achieve rather than how to achieve it.
Kustomize focuses on overlays and patches, allowing you to customize existing YAML manifests without directly editing them. This keeps your original files clean and reusable.
Kustomize is available as a standalone tool and is also integrated with the kubectl command
How Kustomize Works ?
Kustomize and Helm can work with Git repositories for managing Kubernetes configurations and Helm charts, respectively.
You can version control your configurations or charts in Git repositories and then use either tool to deploy or manage them. This enables collaboration, versioning, and tracking changes over time in a structured manner.
Features of Kustomize for Kubernetes Configuration Management
1.Layered Configuration:
- Separation of concerns: Base layer holds the default configuration, while overlays apply environment-specific changes without modifying the base.
- Reusability: Base configurations can be reused across different deployments, promoting consistency and reducing duplication.
- Environment-specific customization: Overlays allow for targeted changes for specific environments (e.g., dev, staging, prod) without modifying the base configuration.
- Maintainability: Clear separation of concerns between base configurations and environment-specific adjustments.
2.Declarative Management:
- Focuses on what needs to be changed rather than how to change it.
- Simplifies configuration management and reduces the risk of errors.
- Example: Specify desired image for a deployment in kustomization.yaml, and Kustomize automatically applies the change.
3.Template-free Customization:
- Uses plain YAML files for both base configurations and overlays, making them easier to read and maintain compared to templating languages.
- Reduces the complexity of managing configurations and avoids potential issues with templating syntax.
4.Additional Features:
- Resource Filtering: Include or exclude specific types of resources in the final configuration.
- Image Overrides: Override container image versions for different environments directly in kustomization.yaml.
- Name Prefixing: Prefix resource names to avoid conflicts across deployments.
- Kustomize Build: Command for generating the final merged YAML based on the base and overlays.
- Integration with kubectl: Use the -k flag with kubectl apply to directly apply kustomized configurations.
Benefits of Kustomize for Kubernetes Configuration Management
- Improved organization: Layered approach keeps configurations organized and maintainable.
- Reduced complexity: Declarative approach simplifies configuration management.
- Increased efficiency: Reusability and environment-specific customization streamline deployments.
- Enhanced collaboration: Easier to share and understand plain YAML files compared to templated configurations.
Kustomize Overlay Architecture
Kustomize relies on a layered architecture for managing Kubernetes configurations.
1. Base Layer:
- This layer forms the foundation and contains the default configuration for your application or component. These configurations are typically stored in plain YAML files.
- The base layer serves as a common starting point for all environments and deployments.
2. Patch Layers (Overlays):
- These layers sit on top of the base layer and contain specific changes for different environments or use cases.
- Patches use strategic merge patching to modify specific fields within the base configurations.
- Multiple patch layers can be applied sequentially to build up more specific configurations.
4. Output: (kustomize build)
- Kustomize processes the layered configuration and generates a final, merged YAML file ready for deployment to Kubernetes.
- This final output reflects the combined configuration from the base layer and all the applied patches.
Benefits of Kustomize Layered Architecture:
- Reusability: Base configurations can be reused across different deployments, promoting consistency and reducing duplication.
- Environment-specific customization: Patches allow for targeted changes for specific environments (e.g., development, staging, production) without modifying the base configuration.
- Maintainability: Clear separation of concerns between base configurations and environment-specific adjustments.
This layered architecture empowers Kustomize to manage Kubernetes configurations efficiently, promoting maintainability and customization across multiple environments.
Kustomize File Structure
Base:
- A directory containing a kustomization.yaml file.
- This file defines:
- Resources (YAML files) to be included.
- Base configurations applicable to all environments (e.g., namespace, labels).
Overlays:
- Directories extending the base configuration for specific environments (e.g., staging, production).
- Each overlay also has a kustomization.yaml file.
- This file:
- References the base directory (using bases) for shared resources.
- Specifies additional resources or patches specific to the environment.
Kustomize File Structure Key Points:
- The base directory contains the core configuration used across environments.
- Overlays introduce environment-specific adjustments without modifying the base resources.
- This layering approach promotes reusability and consistency in configuration management.
- To know more about, please visit Kustomize official page
Kustomize Installation Methods
Kustomize installation depends on whether you already have kubectl installed and its version:
Scenario 1: kubectl version >= 1.14 is installed
If you already have kubectl version 1.14 or later, Kustomize is already included and you don’t need to install it separately. You can verify your kubectl version by running:
kubectl version
If the version is 1.14 or above, you can use the kubectl apply -k flag to work with kustomize directly.
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl
Download the public signing key for the Kubernetes package repositories
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
Add the appropriate Kubernetes apt repository
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
Update apt package index, then install kubectl:
sudo apt-get update
sudo apt-get install -y kubectl
To check kubectl version
kubectl version
Client Version: v1.29.2
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Scenario 2: kubectl version < 1.14 or no kubectl installed
In this case, you need to install Kustomize, Below are options available from kustomize official page
How to Practice Kustomize
Here are some ways you can practice Kubernetes Kustomize:
- Set up a local development environment:
- Install a local Kubernetes cluster using tools like Minikube or Kind.
- Ensure you have kubectl installed and configured to interact with your cluster.
2. Killercoda
Killercoda is an interactive learning platform
that allows you to access pre-configured Linux and Kubernetes environments directly within your web browser, eliminating the need for local setup or resource-intensive configurations.
3. Kubernetes on Docker Desktop
Docker Desktop provides a convenient way to run a single-node Kubernetes cluster
for local development and testing purposes. Here’s a summary of using Kubernetes on Docker Desktop:
Setting up Kubernetes:
- Install Docker Desktop: Download and install Docker Desktop from the official website, ensuring compatibility with your operating system.
Enable Kubernetes: Within Docker Desktop settings, navigate to the “Kubernetes” tab and check the “Enable Kubernetes” box. Click “Apply & Restart” to activate the feature. Docker will download the necessary components and configure the cluster.
4. K3d.io
k3d.io refers to k3d, a lightweight tool used to run Kubernetes clusters locally within Docker containers. It simplifies the process of setting up and managing Kubernetes environments, making it particularly useful for development, testing, and experimentation with Kubernetes.
How to Deploy Go App on Minikube using Kustomize
Prerequisites
1) Minikube Installed on Ubuntu 22.04 EC2
2) kubectl Installed
Please follow below article to install minikube and kubectl on Ubuntu 22.04 LTS
How to Install Minikube and kubectl on Ubuntu 22.04 LTS
Below are directory structure to Deploy Go App on minikube using kustomize
Create a directory structure with a base folder containing the common manifests for your application.
Within the base directory, create a file named kustomization.yaml
. This file defines configurations for the base manifests.
Create subdirectories for different environments (e.g., dev, staging, production). Inside each environment directory, create a kustomization.yaml
file. This file can:
- Patch specific resources from the base using strategic merge patches.
- Add environment-specific resources.
You can create above kustomize file structure in Git Repo OR on your local system,
mkdir -p kustomize/base &&
touch kustomize/base/deployment.yaml \
kustomize/base/service.yaml \
kustomize/base/kustomization.yaml &&
mkdir -p kustomize/overlays/dev &&
touch kustomize/overlays/dev/deployment-dev.yaml \
kustomize/overlays/dev/service-dev.yaml \
kustomize/overlays/dev/kustomization.yaml
Create Base Folder Kustomization files
Create deployment.yaml in base folder.
base/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: go-app
spec:
replicas: 1
selector:
matchLabels:
app: go-app
template:
metadata:
labels:
app: go-app
spec:
containers:
- name: go-app
image: devopshint/go-app:latest
ports:
- containerPort: 8081
create service.yaml in base folder
base/service.yaml
apiVersion: v1
kind: Service
metadata:
name: go-app
spec:
selector:
app: go-app
ports:
- name: http
port: 3000
create kustomization.yaml in base folder
base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
Dev Env Overlay Folder
create deployment-dev.yaml file in overlay/dev folder
overlays/dev/deployment-dev.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: go-app
spec:
replicas: 2 # Update the replica count to 2 for dev env
template:
spec:
containers:
- name: go-app
create service-dev.yaml file in overlay/dev folder
overlays/dev/service-dev.yaml
apiVersion: v1
kind: Service
metadata:
name: go-app
spec:
type: NodePort
create kustomization.yaml file in overlay/dev folder
overlays/dev/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
patches:
- path: deployment-dev.yaml
- path: service-dev.yaml
To review the kustomize configuration and overlays.
kustomize build overlays/dev
To Apply kustomize overlays env configs on Minikube
kubectl apply -k overlays/dev
Kubectl commands to check pods, deployment and service on Minikube/kubernetes
kubectl get pods
kubectl get deploy
kubectl get svc
To expose service on internet on any IP to access on browser using kubectl port-forward
kubectl port-forward svc/go-app --address 0.0.0.0 8081:8081
Access Go App on browser using minikube EC2 instance public IP
http://IP:8081
Conclusion:
In this article we have covered Kubernetes Kustomize Tutorial with Examples, How to Deploy Go App on minikube using kustomize.
Reference: