Kustomize

Kustomize is a tool to combine multiple YAML files in a single kubernetes descriptor, it can be used to manage multiple environment definition of the same application and factorize dependencies between multiple deployments, for example given the following structure

flowchart TD A@{label: "app1", shape: proc} --> B@{label: "database", shape: db} C@{label: "app2", shape: proc} --> D@{label: "database", shape: db} E@{label: "app3", shape: proc} --> F@{label: "database", shape: db}

Instead of writing the same database descriptor for the 3 application kustomize allow to define a single descriptor to import in different deployments, for example the previous structure translates like this:

deployments
├── db
│   ├── deployment.yml
│   └── kustomization.yml
├── app1
│   └── kustomization.yml
├── app2
│   └── kustomization.yml
└── app3
    └── kustomization.yml

In a scenario where an app needs to be deployed in multiple environments, kustomize can factorize common definition like container images and leave the env vars or the volume configuration to specific overlays

flowchart TD subgraph test A@{label: "app1", shape: proc} --> B@{label: "database", shape: db} end subgraph staging C@{label: "app1", shape: proc} --> D@{label: "database", shape: db} end subgraph prod E@{label: "app1", shape: proc} --> F@{label: "database", shape: db} end
deployments
├── base
│   ├── deployment.yml
│   └── kustomization.yml
├── test
│   └── kustomization.yml
├── staging
│   └── kustomization.yml
└── prod
    └── kustomization.yml

Internal structure

Kustomize works by reading a kustomization which is a directory with a kustomization.yml file inside that describe a kubernetes RMD, kustomize is based on transformers, which are plugins that can modify the generated YAML files, all other fields inside a kustomization file are shortcuts for a specific transformer with a default set of parameters

Manage configMaps

To manage configMap kustomize offers a configMapGenerator field that can be used to generate a configMap from a file or from literal values, for example to configure a file named app.properties:

configMapGenerator:
- name: app.properties
  options:
    labels:
      app-config: app.properties
  files:
  - configure-pod-container/configmap/app.properties

Manage secrets

To manage secrets kustomize offer a similar approach with a secretGenerator field that can be used to generate a secret from a file or from literal values, for example to configure a file based on a .env file:

secretGenerator:
- name: db-user-pass
  envs:
  - .env

🔷 Note

this will generate secrets based on a .env file that can edited outside of scm

References

Link map