|
1 | 1 | ---
|
2 |
| -title: "components" |
3 |
| -linkTitle: "components" |
| 2 | +title: "Components" |
| 3 | +linkTitle: "Components" |
4 | 4 | type: docs
|
5 | 5 | weight: 5
|
6 | 6 | description: >
|
7 | 7 | Compose kustomizations.
|
8 | 8 | ---
|
9 | 9 |
|
10 |
| -Please check out the existing [components guide](/guides/config_management/components/) for explanation with examples. |
11 |
| -More examples are in progress |
| 10 | +As of ``v3.7.0`` Kustomize supports a special type of kustomization that allows |
| 11 | +one to define reusable pieces of configuration logic that can be included from |
| 12 | +multiple overlays. |
| 13 | + |
| 14 | +Components come in handy when dealing with applications that support multiple |
| 15 | +optional features and you wish to enable only a subset of them in different |
| 16 | +overlays, i.e., different features for different environments or audiences. |
| 17 | + |
| 18 | +For more details regarding this feature you can read the |
| 19 | +[Kustomize Components KEP](https://github.com/kubernetes/enhancements/tree/master/keps/sig-cli/1802-kustomize-components) |
| 20 | +and the [components concept](/docs/concepts/components/) page. |
| 21 | + |
| 22 | +## Use case |
| 23 | + |
| 24 | +Suppose you've written a very simple Web application: |
| 25 | + |
| 26 | +```yaml |
| 27 | +apiVersion: apps/v1 |
| 28 | +kind: Deployment |
| 29 | +metadata: |
| 30 | + name: example |
| 31 | +spec: |
| 32 | + template: |
| 33 | + spec: |
| 34 | + containers: |
| 35 | + - name: example |
| 36 | + image: example:1.0 |
| 37 | +``` |
| 38 | +
|
| 39 | +You want to deploy a **community** edition of this application as SaaS, so you |
| 40 | +add support for persistence (e.g. an external database), and bot detection |
| 41 | +(e.g. Google reCAPTCHA). |
| 42 | +
|
| 43 | +You've now attracted **enterprise** customers who want to deploy it |
| 44 | +on-premises, so you add LDAP support, and disable Google reCAPTCHA. At the same |
| 45 | +time, the **devs** need to be able to test parts of the application, so they |
| 46 | +want to deploy it with some features enabled and others not. |
| 47 | +
|
| 48 | +Here's a matrix with the deployments of this application and the features |
| 49 | +enabled for each one: |
| 50 | +
|
| 51 | +| | External DB | LDAP | reCAPTCHA | |
| 52 | +|------------|:------------------:|:------------------:|:------------------:| |
| 53 | +| Community | ✔️ | | ✔️ | |
| 54 | +| Enterprise | ✔️ | ✔️ | | |
| 55 | +| Dev | ✅ | ✅ | ✅ | |
| 56 | +
|
| 57 | +(✔️ enabled, ✅: optional) |
| 58 | +
|
| 59 | +So, you want to make it easy to deploy your application in any of the above |
| 60 | +three environments. Here's how you can do this with Kustomize components: each |
| 61 | +opt-in feature gets packaged as a component, so that it can be referred to from |
| 62 | +multiple higher-level overlays. |
| 63 | +
|
| 64 | +First, define a place to work: |
| 65 | +
|
| 66 | +```shell |
| 67 | +DEMO_HOME=$(mktemp -d) |
| 68 | +``` |
| 69 | + |
| 70 | +Define a common **base** that has a `Deployment` and a simple `ConfigMap`, that |
| 71 | +is mounted on the application's container. |
| 72 | + |
| 73 | +```bash |
| 74 | +BASE=$DEMO_HOME/base |
| 75 | +mkdir $BASE |
| 76 | +``` |
| 77 | + |
| 78 | +```bash |
| 79 | +# $BASE/kustomization.yaml |
| 80 | +resources: |
| 81 | +- deployment.yaml |
| 82 | + |
| 83 | +configMapGenerator: |
| 84 | +- name: conf |
| 85 | + literals: |
| 86 | + - main.conf=| |
| 87 | + color=cornflower_blue |
| 88 | + log_level=info |
| 89 | +``` |
| 90 | + |
| 91 | +```bash |
| 92 | +# $BASE/deployment.yaml |
| 93 | +apiVersion: apps/v1 |
| 94 | +kind: Deployment |
| 95 | +metadata: |
| 96 | + name: example |
| 97 | +spec: |
| 98 | + template: |
| 99 | + spec: |
| 100 | + containers: |
| 101 | + - name: example |
| 102 | + image: example:1.0 |
| 103 | + volumeMounts: |
| 104 | + - name: conf |
| 105 | + mountPath: /etc/config |
| 106 | + volumes: |
| 107 | + - name: conf |
| 108 | + configMap: |
| 109 | + name: conf |
| 110 | +``` |
| 111 | + |
| 112 | +Define an `external_db` component, using `kind: Component`, that creates a |
| 113 | +`Secret` for the DB password and a new entry in the `ConfigMap`: |
| 114 | + |
| 115 | +```shell |
| 116 | +EXT_DB=$DEMO_HOME/components/external_db |
| 117 | +mkdir -p $EXT_DB |
| 118 | +``` |
| 119 | + |
| 120 | +```bash |
| 121 | +# $EXT_DB/kustomization.yaml |
| 122 | +apiVersion: kustomize.config.k8s.io/v1alpha1 # <-- Component notation |
| 123 | +kind: Component |
| 124 | + |
| 125 | +secretGenerator: |
| 126 | +- name: dbpass |
| 127 | + files: |
| 128 | + - dbpass.txt |
| 129 | + |
| 130 | +patchesStrategicMerge: |
| 131 | + - configmap.yaml |
| 132 | + |
| 133 | +patchesJson6902: |
| 134 | +- target: |
| 135 | + group: apps |
| 136 | + version: v1 |
| 137 | + kind: Deployment |
| 138 | + name: example |
| 139 | + path: deployment.yaml |
| 140 | +``` |
| 141 | + |
| 142 | +```bash |
| 143 | +# $EXT_DB/deployment.yaml |
| 144 | +- op: add |
| 145 | + path: /spec/template/spec/volumes/0 |
| 146 | + value: |
| 147 | + name: dbpass |
| 148 | + secret: |
| 149 | + secretName: dbpass |
| 150 | +- op: add |
| 151 | + path: /spec/template/spec/containers/0/volumeMounts/0 |
| 152 | + value: |
| 153 | + mountPath: /var/run/secrets/db/ |
| 154 | + name: dbpass |
| 155 | +``` |
| 156 | + |
| 157 | +```bash |
| 158 | +# $EXT_DB/configmap.yaml |
| 159 | +apiVersion: v1 |
| 160 | +kind: ConfigMap |
| 161 | +metadata: |
| 162 | + name: conf |
| 163 | +data: |
| 164 | + db.conf: | |
| 165 | + endpoint=127.0.0.1:1234 |
| 166 | + name=app |
| 167 | + user=admin |
| 168 | + pass=/var/run/secrets/db/dbpass.txt |
| 169 | +``` |
| 170 | + |
| 171 | +Define an `ldap` component, that creates a `Secret` for the LDAP password |
| 172 | +and a new entry in the `ConfigMap`: |
| 173 | + |
| 174 | +```shell |
| 175 | +LDAP=$DEMO_HOME/components/ldap |
| 176 | +mkdir -p $LDAP |
| 177 | +``` |
| 178 | + |
| 179 | +```bash |
| 180 | +# $LDAP/kustomization.yaml |
| 181 | +apiVersion: kustomize.config.k8s.io/v1alpha1 |
| 182 | +kind: Component |
| 183 | + |
| 184 | +secretGenerator: |
| 185 | +- name: ldappass |
| 186 | + files: |
| 187 | + - ldappass.txt |
| 188 | + |
| 189 | +patchesStrategicMerge: |
| 190 | + - configmap.yaml |
| 191 | + |
| 192 | +patchesJson6902: |
| 193 | +- target: |
| 194 | + group: apps |
| 195 | + version: v1 |
| 196 | + kind: Deployment |
| 197 | + name: example |
| 198 | + path: deployment.yaml |
| 199 | +``` |
| 200 | + |
| 201 | +```bash |
| 202 | +# $LDAP/deployment.yaml |
| 203 | +- op: add |
| 204 | + path: /spec/template/spec/volumes/0 |
| 205 | + value: |
| 206 | + name: ldappass |
| 207 | + secret: |
| 208 | + secretName: ldappass |
| 209 | +- op: add |
| 210 | + path: /spec/template/spec/containers/0/volumeMounts/0 |
| 211 | + value: |
| 212 | + mountPath: /var/run/secrets/ldap/ |
| 213 | + name: ldappass |
| 214 | +``` |
| 215 | + |
| 216 | +```bash |
| 217 | +# $LDAP/configmap.yaml |
| 218 | +apiVersion: v1 |
| 219 | +kind: ConfigMap |
| 220 | +metadata: |
| 221 | + name: conf |
| 222 | +data: |
| 223 | + ldap.conf: | |
| 224 | + endpoint=ldap://ldap.example.com |
| 225 | + bindDN=cn=admin,dc=example,dc=com |
| 226 | + pass=/var/run/secrets/ldap/ldappass.txt |
| 227 | +``` |
| 228 | + |
| 229 | +Define a `recaptcha` component, that creates a `Secret` for the reCAPTCHA |
| 230 | +site/secret keys and a new entry in the `ConfigMap`: |
| 231 | + |
| 232 | +```shell |
| 233 | +RECAPTCHA=$DEMO_HOME/components/recaptcha |
| 234 | +mkdir -p $RECAPTCHA |
| 235 | +``` |
| 236 | + |
| 237 | +```bash |
| 238 | +# $RECAPTCHA/kustomization.yaml |
| 239 | +apiVersion: kustomize.config.k8s.io/v1alpha1 |
| 240 | +kind: Component |
| 241 | + |
| 242 | +secretGenerator: |
| 243 | +- name: recaptcha |
| 244 | + files: |
| 245 | + - site_key.txt |
| 246 | + - secret_key.txt |
| 247 | + |
| 248 | +# Updating the ConfigMap works with generators as well. |
| 249 | +configMapGenerator: |
| 250 | +- name: conf |
| 251 | + behavior: merge |
| 252 | + literals: |
| 253 | + - recaptcha.conf=| |
| 254 | + enabled=true |
| 255 | + site_key=/var/run/secrets/recaptcha/site_key.txt |
| 256 | + secret_key=/var/run/secrets/recaptcha/secret_key.txt |
| 257 | + |
| 258 | +patchesJson6902: |
| 259 | +- target: |
| 260 | + group: apps |
| 261 | + version: v1 |
| 262 | + kind: Deployment |
| 263 | + name: example |
| 264 | + path: deployment.yaml |
| 265 | +``` |
| 266 | + |
| 267 | +```bash |
| 268 | +# $RECAPTCHA/deployment.yaml |
| 269 | +- op: add |
| 270 | + path: /spec/template/spec/volumes/0 |
| 271 | + value: |
| 272 | + name: recaptcha |
| 273 | + secret: |
| 274 | + secretName: recaptcha |
| 275 | +- op: add |
| 276 | + path: /spec/template/spec/containers/0/volumeMounts/0 |
| 277 | + value: |
| 278 | + mountPath: /var/run/secrets/recaptcha/ |
| 279 | + name: recaptcha |
| 280 | +``` |
| 281 | + |
| 282 | +Define a `community` variant, that bundles the external DB and reCAPTCHA |
| 283 | +components: |
| 284 | + |
| 285 | +```shell |
| 286 | +COMMUNITY=$DEMO_HOME/overlays/community |
| 287 | +mkdir -p $COMMUNITY |
| 288 | +``` |
| 289 | + |
| 290 | +```bash |
| 291 | + |
| 292 | +# $COMMUNITY/kustomization.yaml |
| 293 | +apiVersion: kustomize.config.k8s.io/v1beta1 |
| 294 | +kind: Kustomization |
| 295 | + |
| 296 | +resources: |
| 297 | + - ../../base |
| 298 | + |
| 299 | +components: |
| 300 | + - ../../components/external_db |
| 301 | + - ../../components/recaptcha |
| 302 | +``` |
| 303 | + |
| 304 | +Define an `enterprise` overlay, that bundles the external DB and LDAP |
| 305 | +components: |
| 306 | + |
| 307 | +```shell |
| 308 | +ENTERPRISE=$DEMO_HOME/overlays/enterprise |
| 309 | +mkdir -p $ENTERPRISE |
| 310 | +``` |
| 311 | + |
| 312 | +```bash |
| 313 | +# $ENTERPRISE/kustomization.yaml |
| 314 | +apiVersion: kustomize.config.k8s.io/v1beta1 |
| 315 | +kind: Kustomization |
| 316 | + |
| 317 | +resources: |
| 318 | + - ../../base |
| 319 | + |
| 320 | +components: |
| 321 | + - ../../components/external_db |
| 322 | + - ../../components/ldap |
| 323 | +``` |
| 324 | + |
| 325 | +Define a `dev` overlay, that points to all the components and has LDAP |
| 326 | +disabled: |
| 327 | + |
| 328 | +```shell |
| 329 | +DEV=$DEMO_HOME/overlays/dev |
| 330 | +mkdir -p $DEV |
| 331 | +``` |
| 332 | + |
| 333 | +```bash |
| 334 | +# $DEV/kustomization.yaml |
| 335 | +apiVersion: kustomize.config.k8s.io/v1beta1 |
| 336 | +kind: Kustomization |
| 337 | + |
| 338 | +resources: |
| 339 | + - ../../base |
| 340 | + |
| 341 | +components: |
| 342 | + - ../../components/external_db |
| 343 | + #- ../../components/ldap |
| 344 | + - ../../components/recaptcha |
| 345 | +``` |
| 346 | + |
| 347 | +Now, the workspace has the following directories: |
| 348 | + |
| 349 | +```shell |
| 350 | +├── base |
| 351 | +│ ├── deployment.yaml |
| 352 | +│ └── kustomization.yaml |
| 353 | +├── components |
| 354 | +│ ├── external_db |
| 355 | +│ │ ├── configmap.yaml |
| 356 | +│ │ ├── dbpass.txt |
| 357 | +│ │ ├── deployment.yaml |
| 358 | +│ │ └── kustomization.yaml |
| 359 | +│ ├── ldap |
| 360 | +│ │ ├── configmap.yaml |
| 361 | +│ │ ├── deployment.yaml |
| 362 | +│ │ ├── kustomization.yaml |
| 363 | +│ │ └── ldappass.txt |
| 364 | +│ └── recaptcha |
| 365 | +│ ├── deployment.yaml |
| 366 | +│ ├── kustomization.yaml |
| 367 | +│ ├── secret_key.txt |
| 368 | +│ └── site_key.txt |
| 369 | +└── overlays |
| 370 | + ├── community |
| 371 | + │ └── kustomization.yaml |
| 372 | + ├── dev |
| 373 | + │ └── kustomization.yaml |
| 374 | + └── enterprise |
| 375 | + └── kustomization.yaml |
| 376 | +``` |
| 377 | + |
| 378 | +With this structure, you can generate the YAML manifests for each deployment |
| 379 | +using `kustomize build`: |
| 380 | + |
| 381 | +```shell |
| 382 | +kustomize build overlays/community |
| 383 | +kustomize build overlays/enterprise |
| 384 | +kustomize build overlays/dev |
| 385 | +``` |
0 commit comments