Kustomize is a tool for customizing Kubernetes configurations. It allows you to manage and deploy Kubernetes resources in a more flexible and reusable way. Unlike Helm, which uses templating, Kustomize works by overlaying patches on top of base configurations, making it a more declarative approach to configuration management.
Key Concepts
- Base and Overlays
- Base: The base configuration is the original set of Kubernetes manifests that define your application.
- Overlay: Overlays are modifications or customizations applied to the base configuration. They allow you to create different environments (e.g., development, staging, production) without duplicating the base configuration.
- Resources
- Resources: These are the Kubernetes manifests (YAML files) that define your application components, such as Deployments, Services, ConfigMaps, etc.
- Patches
- Patches: Patches are used to modify the base resources. They can be strategic merge patches, JSON patches, or replacement patches.
- Kustomization File
- kustomization.yaml: This file defines the resources, patches, and other configurations for Kustomize. It is the entry point for Kustomize to understand how to build the final set of manifests.
Setting Up Kustomize
Installation
Kustomize can be installed as a standalone tool or as part of kubectl.
Standalone Installation
# Download the latest release curl -s "https://api.github.com/repos/kubernetes-sigs/kustomize/releases/latest" | grep browser_download_url | grep kustomize | cut -d '"' -f 4 | xargs curl -O -L # Make the binary executable chmod +x kustomize # Move the binary to your PATH sudo mv kustomize /usr/local/bin/
Using kubectl
Kustomize is integrated into kubectl starting from version 1.14.
Practical Example
Directory Structure
Let's create a simple example to demonstrate how Kustomize works. We'll deploy a web application with different configurations for development and production environments.
my-app/
├── base
│ ├── deployment.yaml
│ ├── service.yaml
│ └── kustomization.yaml
├── overlays
├── development
│ ├── kustomization.yaml
│ └── patch.yaml
└── production
├── kustomization.yaml
└── patch.yamlBase Configuration
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 1
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app:latest
ports:
- containerPort: 80service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-app
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80kustomization.yaml
Overlay Configuration
Development Overlay
patch.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 2
template:
spec:
containers:
- name: my-app
image: my-app:devkustomization.yaml
Production Overlay
patch.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
template:
spec:
containers:
- name: my-app
image: my-app:prodkustomization.yaml
Applying the Configuration
To apply the development configuration:
To apply the production configuration:
Exercises
Exercise 1: Create a New Overlay
- Create a new overlay for a staging environment.
- Modify the
replicasto 4 and use the imagemy-app:staging.
Solution
- Create the directory structure:
my-app/ ├── overlays ├── staging ├── kustomization.yaml └── patch.yaml - Create
patch.yaml:apiVersion: apps/v1 kind: Deployment metadata: name: my-app spec: replicas: 4 template: spec: containers: - name: my-app image: my-app:staging - Create
kustomization.yaml:resources: - ../../base patchesStrategicMerge: - patch.yaml
Exercise 2: Add a ConfigMap
- Add a ConfigMap to the base configuration.
- Modify the overlays to include environment-specific configurations.
Solution
- Create
configmap.yamlin the base directory:apiVersion: v1 kind: ConfigMap metadata: name: my-app-config data: key: value - Update
kustomization.yamlin the base directory:resources: - deployment.yaml - service.yaml - configmap.yaml - Modify the overlays to include environment-specific configurations by creating patches for the ConfigMap.
Conclusion
Kustomize provides a powerful and flexible way to manage Kubernetes configurations. By using bases and overlays, you can easily customize and manage different environments without duplicating your configuration files. This approach promotes reusability and maintainability, making it easier to manage complex Kubernetes deployments.
Kubernetes Course
Module 1: Introduction to Kubernetes
- What is Kubernetes?
- Kubernetes Architecture
- Key Concepts and Terminology
- Setting Up a Kubernetes Cluster
- Kubernetes CLI (kubectl)
Module 2: Core Kubernetes Components
Module 3: Configuration and Secrets Management
Module 4: Networking in Kubernetes
Module 5: Storage in Kubernetes
Module 6: Advanced Kubernetes Concepts
Module 7: Monitoring and Logging
- Monitoring with Prometheus
- Logging with Elasticsearch, Fluentd, and Kibana (EFK)
- Health Checks and Probes
- Metrics Server
Module 8: Security in Kubernetes
Module 9: Scaling and Performance
Module 10: Kubernetes Ecosystem and Tools
Module 11: Case Studies and Real-World Applications
- Deploying a Web Application
- CI/CD with Kubernetes
- Running Stateful Applications
- Multi-Cluster Management
