Illustrate how to work with Kustomize overlays and components.
You can inherit Kubernetes settings with Kustomize bases and overlays. But! You can also compose Kubernetes settings with Kustomize components.
The following procedure describes how to deployment app app to a hypothetical env1 environment on Docker Desktop on your laptop.
-
Create your Kubernetes cluster on Docker Desktop on your laptop—see mbigras/hello-kubernetes repo.
-
Get the code.
git clone [email protected]:mbigras/kustomize-demo.git cd kustomize-demo
-
Run example.sh script.
./example.sh
Your output should look like the following:
$ ./example.sh # ... deployment.apps/app created # ... { "app": "app", "env": "env1", "color": "cheapblue", "password": "s3cr3t", "features": [ "feature1", "feature2" ] } deployment.apps "app" deleted
Observation: Notice that you created your app—that is, a app Kubernetes Deployment—, send a test request, and deleted your app!
The following procedure describes how to create an app Kubernetes Deployment with Kustomize overlays and components.
-
Build your app Kubernetes settings for env1 environment.
kustomize build [email protected]:mbigras/kustomize-demo.git//overlays/env1?ref=35b728e6ba9ad874fa6f49985aa5081508fcc52d
Your output should look like the following:
$ kustomize build [email protected]:mbigras/kustomize-demo.git//overlays/env1?ref=35b728e6ba9ad874fa6f49985aa5081508fcc52d apiVersion: apps/v1 kind: Deployment metadata: labels: app: app name: app spec: selector: matchLabels: app: app template: metadata: labels: app: app spec: containers: - env: - name: ENV value: env1 - name: COLOR value: cheapblue - name: PASSWORD value: s3cr3t - name: FEATURE2 value: "on" - name: FEATURE1 value: "on" image: mbigras/app:2023-07-15 name: app
Consider the following details:
- Observation: Notice that the app Deployment contains an app Container that is configured to run in env1 environment.
- Note: You inject settings as environment variables—follows excellent twelve-factor methodology—see https://12factor.net/config page.
-
To understand how the ENV, COLOR, and PASSWORD settings reach app running in env1 environment, consider the overlays/env1/kustomization.yaml#L3-L6 code.
kind: Kustomization apiVersion: kustomize.config.k8s.io/v1beta1 resources: - ../../base patches: - path: deployment.yaml # ...
Observation: Notice how your overlay inherits your base, then patches—inheritance.
-
To understand how the FEATURE1 and FEATURE2 settings reach app running in env1 environment, consider the overlays/env1/kustomization.yaml#L7-L9 code.
kind: Kustomization apiVersion: kustomize.config.k8s.io/v1beta1 # ... components: - ../../components/feature1 - ../../components/feature2
Observation: Notice how your overlay reaches your components like plug and play composition—excellent!
You can freely compose Kubernetes YAML objects with Kustomize bases, overlays, and components. Kustomize bases and overlays compose like inheritance. Kustomize components compose like plug and play.