Skip to content

Commit 3026953

Browse files
feat: Add examples for kustomize
Signed-off-by: Abhishek Veeramalla <abhishek.veeramalla@gmail.com>
1 parent d37cf6e commit 3026953

File tree

25 files changed

+626
-2
lines changed

25 files changed

+626
-2
lines changed

01-basics/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Kustomize Basics
2+
3+
## What You'll Learn
4+
In this section, you'll learn the fundamental concepts of Kustomize:
5+
- How to organize your Kubernetes configurations
6+
- How to make simple changes without touching the original files
7+
- Understanding base and overlay structure
8+
9+
## Directory Structure
10+
```
11+
.
12+
├── base/ # Your original configuration
13+
│ ├── deployment.yaml # A basic nginx deployment
14+
│ └── kustomization.yaml # Tells Kustomize what to include
15+
└── overlay/ # Your customizations
16+
└── kustomization.yaml # Defines how to modify the base
17+
```
18+
19+
## Understanding the Files
20+
21+
### Base Configuration
22+
The `base/` directory contains your original, unchanged configuration:
23+
- `deployment.yaml`: A simple nginx web server deployment
24+
- `kustomization.yaml`: Lists which files to include
25+
26+
### Overlay Configuration
27+
The `overlay/` directory shows how to customize the base:
28+
- Adds a prefix to all resources (`dev-`)
29+
- Updates the nginx image version to 1.20
30+
- Adds environment labels
31+
32+
## Try It Yourself
33+
34+
1. View the base deployment:
35+
```bash
36+
kubectl kustomize base/
37+
```
38+
39+
2. View the customized version:
40+
```bash
41+
kubectl kustomize overlay/
42+
```
43+
44+
3. Apply to your cluster:
45+
```bash
46+
kubectl apply -k overlay/
47+
```
48+
49+
## What's Happening?
50+
1. Kustomize reads the base configuration
51+
2. It applies the changes specified in the overlay:
52+
- Adds "dev-" prefix to names
53+
- Updates the nginx image
54+
- Adds environment labels
55+
3. Generates the final configuration
56+
57+
## Key Concepts
58+
- **Base**: Your original configuration
59+
- **Overlay**: Your customizations
60+
- **kustomization.yaml**: Tells Kustomize what to do
61+
- **Resources**: The Kubernetes files you want to modify

01-basics/base/deployment.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: web-app
5+
spec:
6+
replicas: 1
7+
selector:
8+
matchLabels:
9+
app: web-app
10+
template:
11+
metadata:
12+
labels:
13+
app: web-app
14+
spec:
15+
containers:
16+
- name: web-app
17+
image: nginx:1.19
18+
ports:
19+
- containerPort: 80

01-basics/base/kustomization.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
4+
resources:
5+
- deployment.yaml
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
4+
namePrefix: dev-
5+
resources:
6+
- ../base
7+
8+
images:
9+
- name: nginx
10+
newName: nginx
11+
newTag: "1.20"
12+
13+
commonLabels:
14+
environment: development

02-patches/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Working with Patches in Kustomize
2+
3+
## What You'll Learn
4+
This section teaches you how to make specific changes to your Kubernetes resources using patches:
5+
- Strategic Merge Patches: Easy way to modify configurations
6+
- JSON 6902 Patches: More precise modifications
7+
- When to use each type of patch
8+
9+
## Directory Structure
10+
```
11+
.
12+
├── base/ # Original configuration
13+
│ └── deployment.yaml # Basic deployment with resource limits
14+
└── overlay/ # Your modifications
15+
├── increase-memory.yaml # Strategic merge patch
16+
├── add-label.yaml # JSON patch
17+
└── kustomization.yaml # Combines both patch types
18+
```
19+
20+
## Understanding Patches
21+
22+
### Strategic Merge Patch
23+
- File: `increase-memory.yaml`
24+
- Purpose: Updates memory limit from 128Mi to 256Mi
25+
- How it works: Overlays matching fields in the original
26+
- When to use: For simple modifications to existing fields
27+
28+
### JSON 6902 Patch
29+
- File: `add-label.yaml`
30+
- Purpose: Adds a new label to the deployment
31+
- How it works: Specifies exact operations (add, remove, replace)
32+
- When to use: For precise changes or when working with arrays
33+
34+
## Try It Yourself
35+
36+
1. View the original deployment:
37+
```bash
38+
kubectl kustomize base/
39+
```
40+
41+
2. See the patched version:
42+
```bash
43+
kubectl kustomize overlay/
44+
```
45+
46+
## Key Concepts
47+
- **Strategic Merge Patch**:
48+
- Easier to write
49+
- Works well with Kubernetes resources
50+
- Good for simple changes
51+
52+
- **JSON 6902 Patch**:
53+
- More precise control
54+
- Can add/remove/replace specific values
55+
- Better for complex changes
56+
57+
## Common Use Cases
58+
1. Updating resource limits
59+
2. Adding labels or annotations
60+
3. Modifying container arguments
61+
4. Updating environment variables

02-patches/base/deployment.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: web-app
5+
spec:
6+
replicas: 1
7+
template:
8+
spec:
9+
containers:
10+
- name: web-app
11+
image: nginx:1.19
12+
resources:
13+
limits:
14+
memory: "128Mi"
15+
cpu: "100m"

02-patches/overlay/add-label.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- op: add
2+
path: /metadata/labels/app-type
3+
value: web-server
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: web-app
5+
spec:
6+
template:
7+
spec:
8+
containers:
9+
- name: web-app
10+
resources:
11+
limits:
12+
memory: "256Mi"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
4+
resources:
5+
- ../base
6+
7+
patchesStrategicMerge:
8+
- increase-memory.yaml
9+
10+
patchesJson6902:
11+
- target:
12+
group: apps
13+
version: v1
14+
kind: Deployment
15+
name: web-app
16+
path: add-label.yaml

03-configmaps-secrets/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# ConfigMaps and Secrets in Kustomize
2+
3+
## What You'll Learn
4+
This section covers how to manage application configuration using Kustomize:
5+
- Creating ConfigMaps from files
6+
- Mounting ConfigMaps in pods
7+
- Updating configurations for different environments
8+
9+
## Directory Structure
10+
```
11+
.
12+
├── base/ # Base configuration
13+
│ ├── config/ # Configuration files
14+
│ │ └── app.properties # Application properties
15+
│ ├── deployment.yaml # Deployment using ConfigMap
16+
│ └── kustomization.yaml # Generates ConfigMap
17+
└── overlay/ # Environment-specific configs
18+
├── config/
19+
│ └── app.properties # Modified properties
20+
└── kustomization.yaml # Updates ConfigMap
21+
```
22+
23+
## Understanding the Setup
24+
25+
### Base Configuration
26+
- `app.properties`: Default application settings
27+
- `deployment.yaml`: Mounts ConfigMap as a volume
28+
- `kustomization.yaml`: Generates ConfigMap from properties file
29+
30+
### Overlay Configuration
31+
- Modified `app.properties` with environment-specific values
32+
- Uses `replace` behavior to update the entire ConfigMap
33+
34+
## Try It Yourself
35+
36+
1. View the base ConfigMap:
37+
```bash
38+
kubectl kustomize base/
39+
```
40+
41+
2. See the modified version:
42+
```bash
43+
kubectl kustomize overlay/
44+
```
45+
46+
## Key Features
47+
1. **ConfigMap Generation**
48+
- Automatically creates ConfigMaps from files
49+
- Handles updates and changes
50+
- Generates unique names when content changes
51+
52+
2. **Volume Mounting**
53+
- Mounts ConfigMap as a volume
54+
- Makes configuration available to containers
55+
- Updates automatically when ConfigMap changes
56+
57+
3. **Environment Overrides**
58+
- Easy to maintain different configurations
59+
- No need to duplicate entire files
60+
- Clear separation of base and environment-specific settings
61+
62+
## Best Practices
63+
1. Keep sensitive data out of ConfigMaps
64+
2. Use meaningful file names
65+
3. Document your configuration structure
66+
4. Version control your configurations

0 commit comments

Comments
 (0)