|
| 1 | + |
| 2 | +# Deployment in Kubernetes - Kustomize |
| 3 | + |
| 4 | +### Topics Covered: |
| 5 | +- **Kustomize**: Overview and introduction to its purpose in Kubernetes. |
| 6 | +- **Practical Demonstration**: Hands-on demo showcasing how to use Kustomize effectively. |
| 7 | +- **Real-life Scenarios**: Practical examples of how Kustomize is used in production settings. |
| 8 | +- **Interview Questions**: Key questions that might arise in interviews related to Kustomize. |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## Kustomize |
| 13 | + |
| 14 | +### What is Kustomize? |
| 15 | +Kustomize is a **Kubernetes-native configuration management tool** that allows you to customize Kubernetes resource YAML files without modifying the original files. It dynamically applies configurations at runtime, making it easier to maintain multiple environment configurations, such as development, staging, and production. |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +### Working with Kustomize |
| 20 | + |
| 21 | +#### Pre-existing YAML Files: |
| 22 | +- You typically start with Kubernetes resource files: |
| 23 | + - `deployment.yaml` |
| 24 | + - `service.yaml` |
| 25 | + - `configmap.yaml` |
| 26 | + |
| 27 | +#### Adding Kustomize: |
| 28 | +1. Create a new file named **`kustomization.yaml`**. |
| 29 | +2. Add your existing resource files to this file. For example: |
| 30 | + ```yaml |
| 31 | + resources: |
| 32 | + - deployment.yaml |
| 33 | + - service.yaml |
| 34 | + ``` |
| 35 | +3. Kustomize dynamically applies the configurations defined in the `kustomization.yaml` file to your original resource files **at runtime**. |
| 36 | + |
| 37 | +#### Running Kustomize: |
| 38 | +Use the following commands to execute Kustomize: |
| 39 | +- **View transformed YAML**: |
| 40 | + `kubectl kustomize .` |
| 41 | +- **Apply configurations directly to your cluster**: |
| 42 | + `kubectl apply -k .` |
| 43 | +- **Chain commands for flexibility**: |
| 44 | + `kubectl kustomize . | kubectl apply -f` |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +### Demonstration (Instructor Focus) |
| 49 | + |
| 50 | +**Steps to demonstrate Kustomize:** |
| 51 | +1. Create a `kustomization.yaml` file in the same directory as your resource files. |
| 52 | +2. Show how parameters in the `kustomization.yaml` file are applied dynamically to existing files. |
| 53 | +3. Explain the command outputs and how they affect the Kubernetes cluster. |
| 54 | + |
| 55 | +Example `kustomization.yaml` file: |
| 56 | +```yaml |
| 57 | +resources: |
| 58 | + - deployment.yaml |
| 59 | + - service.yaml |
| 60 | +``` |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +### Organizing Kustomize with Directories |
| 65 | + |
| 66 | +To manage environment-specific configurations, use the following directory structure: |
| 67 | + |
| 68 | +#### Directory Structure: |
| 69 | +- **`base` directory**: |
| 70 | + - Contains common YAML files, such as: |
| 71 | + - `deployment.yaml` |
| 72 | + - `service.yaml` |
| 73 | + - `configmap.yaml` |
| 74 | + - `kustomization.yaml` |
| 75 | +- **`overlays` directory**: |
| 76 | + - Contains environment-specific configurations: |
| 77 | + - `dev/` |
| 78 | + - `staging/` |
| 79 | + - `prod/` |
| 80 | + |
| 81 | +#### Example: |
| 82 | +1. Move all base resources (`deployment.yaml`, `service.yaml`, etc.) into the `base` directory. |
| 83 | +2. In each environment folder under `overlays`, create a `kustomization.yaml` file that references the base directory: |
| 84 | + ```yaml |
| 85 | + resources: |
| 86 | + - ../../base |
| 87 | + ``` |
| 88 | +3. Apply patches for specific environments using **`patchesStrategicMerge`**: |
| 89 | + - Add a new file `deployment-patch.yaml` inside the specific environment folder. |
| 90 | + - Reference this file in the `kustomization.yaml` for the environment: |
| 91 | + ```yaml |
| 92 | + patchesStrategicMerge: |
| 93 | + - deployment-patch.yaml |
| 94 | + ``` |
| 95 | + |
| 96 | +#### Applying the Configuration: |
| 97 | +- To apply the dev configuration: |
| 98 | + ```bash |
| 99 | + kubectl apply -k overlays/dev/ |
| 100 | + ``` |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +For a more detailed example, refer to the following structure: |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +## Understanding Kustomize Terminologies |
| 111 | + |
| 112 | +### Directory Structure: |
| 113 | +1. **Base Directory**: |
| 114 | + - Contains shared resources for all environments. |
| 115 | + - Files: |
| 116 | + - `base-deployment.yaml` |
| 117 | + - `kustomization.yaml` |
| 118 | +2. **Overlays Directory**: |
| 119 | + - Contains environment-specific configurations. |
| 120 | + - Files: |
| 121 | + - `kustomization.yaml` |
| 122 | + - `patch.yaml` |
| 123 | + |
| 124 | +### Key Features of `kustomization.yaml`: |
| 125 | +- **ConfigMap Generator**: |
| 126 | + Dynamically generate ConfigMaps: |
| 127 | + ```yaml |
| 128 | + configMapGenerator: |
| 129 | + - name: my-configmap |
| 130 | + literals: |
| 131 | + - key=value |
| 132 | + ``` |
| 133 | +- **Secret Generator**: |
| 134 | + Similar to ConfigMap generator but for secrets: |
| 135 | + ```yaml |
| 136 | + secretGenerator: |
| 137 | + ``` |
| 138 | +- **Common Labels and Annotations**: |
| 139 | + Add labels/annotations globally: |
| 140 | + ```yaml |
| 141 | + commonLabels: |
| 142 | + app: my-app |
| 143 | + ``` |
| 144 | +- **Cross-cutting Fields**: |
| 145 | + Fields like common labels, annotations, or namespaces defined in the `kustomization.yaml` file will apply across all resources. |
| 146 | + |
| 147 | +### Patching Options: |
| 148 | +- **Strategic Merge Patches**: |
| 149 | + Use the `patchesStrategicMerge` field to define YAML patches that selectively modify base resources. |
| 150 | +- **JSON Patches**: |
| 151 | + Use the `patchJson6902` field for JSON-style patching. |
| 152 | + |
| 153 | +### Variables: |
| 154 | +Kustomize supports variables using the `$` syntax. |
| 155 | + |
| 156 | +--- |
| 157 | + |
| 158 | +## Kustomize Examples |
| 159 | + |
| 160 | +### Scenario 1: Multi-Datacenter Deployment |
| 161 | +Suppose a company has four datacenters running an Nginx application. The environments are: |
| 162 | +- Preview |
| 163 | +- Sales |
| 164 | +- Production |
| 165 | + |
| 166 | +#### Configuration: |
| 167 | +1. The **base directory** contains common Kubernetes resources: |
| 168 | + - Deployments |
| 169 | + - Services |
| 170 | + - ConfigMaps |
| 171 | +2. Each datacenter has environment-specific configurations, including: |
| 172 | + - Environment variables |
| 173 | + - Secrets |
| 174 | + - Persistent Volumes (PV) and Persistent Volume Claims (PVC) |
| 175 | + - Role-based Access Control (RBAC) roles |
| 176 | + |
| 177 | +Kustomize enables you to define the shared resources in the **base** directory and override environment-specific values in the respective **overlay** directories. |
| 178 | + |
| 179 | +--- |
| 180 | + |
| 181 | +### Real-Life Example: |
| 182 | +Consider a scenario with multiple clusters spread across different AWS regions. The challenges include: |
| 183 | +- Managing **compliance issues**. |
| 184 | +- Supporting **multi-tenancy** for different customers. |
| 185 | +- Enabling **region-specific features**. |
| 186 | + |
| 187 | +Kustomize simplifies this by: |
| 188 | +- Providing a common base configuration for all clusters. |
| 189 | +- Allowing region-specific customizations using overlays. |
| 190 | + |
| 191 | +--- |
| 192 | + |
| 193 | +## Helm vs Kustomize |
| 194 | + |
| 195 | +### Comparison Table: |
| 196 | + |
| 197 | +| Feature | **Helm** | **Kustomize** | |
| 198 | +|------------------|---------------------------------------|----------------------------------------| |
| 199 | +| **Type** | Package Manager for Kubernetes | Kubernetes Native Configuration Tool | |
| 200 | +| **Templating** | Yes (with Go templates) | No | |
| 201 | +| **Overlays** | Uses values files for customization | Uses patch-based overlays | |
| 202 | +| **Releases** | Manages lifecycle (install/upgrade) | Doesn’t manage state, just config | |
| 203 | +| **Dependencies** | Supports chart dependencies | No built-in support | |
| 204 | +| **Complexity** | Can be complex for simple cases | Simpler, focuses on YAML transformation| |
| 205 | +| **Community** | Large ecosystem (charts) | Part of Kubernetes (kubectl integration)| |
| 206 | +| **Versioning** | Yes | No, relies on Git for version control | |
| 207 | + |
| 208 | +### Key Takeaways: |
| 209 | +- **Helm**: Ideal for managing complex applications with lifecycle management. |
| 210 | +- **Kustomize**: Best for lightweight YAML transformations without requiring templating. |
| 211 | + |
| 212 | +--- |
| 213 | +``` |
0 commit comments