Using Kustomize

Tram Ho

P1. What is Kustomize?

Helm supports advanced editing of each location of yaml on a given environment . Kustomize has the strength of supporting multiple environments (prod, stag, dev1, dev2…..) through the use of overlays. (Also, we can still combine kustomize and helm together)

  1. Setting

  1. Kustomize’s directory structure

  1. Instructions for creating kustomization.yaml

YAML sample full article, you can refer here https://github.com/worldhello12/kustomize-sample

P2: Resources

Definition: Resources consist of 1 or more files .yaml k8s > As input input for kustomize stir-fry.

Example of using Resources

  1. Edit the file kustomization.yaml just init

  1. Prepare deployment and sample service: api-depl.yaml and api-service.yaml

  1. Run Test build Kustomize

P3: Transformers common

(reference: https://github.com/kubernetes-sigs/kustomize/tree/master/examples/transformerconfigs )

Kustomize supports transformers converting the following types:

Let’s go into the transformer example:

Step 1. Prepare deployment and sample service (api-depl.yaml and api-service.yaml)

Step 2. Edit kustomization.yaml

Result:

P3: Overlays-base

Concept: To separate the parameters by environment ( prod/stag/dev ). We can use kustomize overlays.

In which the general parameters will be placed in the “base” directory, the environment-specific configurations (such as replicas, secret-configmap) will be placed in the “overlays” directory.

1. Create a basic directory tree

2. Edit kustomization.yaml in all 3 subfolders of overlays (prod/stag/dev) with the following content

3. Create config-map.yaml for each environment

4. Run kustomize to check if the BASE declaration is correct

Result: Each environment we deploy api-depl.yaml and api-service.yaml, but config-map.yaml account and password will be different for each environment

P4: Patches

(Refer:)

Jsonpath6902: https://github.com/kubernetes-sigs/kustomize/blob/master/examples/jsonpatch.md

Strategic merge: https://fabianlee.org/2022/04/18/kubernetes-kustomize-transformations-with-patchesstrategicmerge/

Patches have 3 editing functions on yaml k8s: replace, delete, add

Example 1: Replace patch

The topic posed:

Solution: There are 2 ways to use Json6092 or Strategic Merge Patch

Solution 1: Json6902 patch

Result:

> the results are in stag, dev run by yourself (kustomize build .).

Solution 2: Strategic merge patch

Result

You can apply kustomize patches to replace any parameters you want: for example in services switch the NodePort/ClusterIP/Balance type for each environment

Example 2: Add patch

The same result both ways:

Example 3: Delete patch

P6: Component

(Reference: https://github.com/kubernetes-sigs/kustomize/blob/master/examples/components.md )

The purpose of the generated Component is to reuse the components that need to be reused for each environment. For example, we have the following table

OverlaysExternal-DB historyCache
prodYesYes
stagNoYes
devOptionsOptions

As the table above.

  • Prod needs extra DB to store user history data, and cache to increase data load from ram
  • Stag doesn’t need to DB heavy traffic of historical data, need cache to test performance
  • Dev can add more DB and Cache if needed to test, or not. We will design the component file as follows:

Edit the kustomization.yaml file according to each environment. (Yaml sample: https://github.com/worldhello12/kustomize-sample/tree/main/05.component )

File components/external_db/kustomization.yaml. We notice kind, apiVersion will be Component

Run kustomize build on all 3 environments, we will have the same results as the table.

OverlaysExternal-DB historyCache
prodYesYes
stagNoYes
devOptionsOptions

.

The article is within the knowledge of the editor, if you have a better solution, please recommend and comment. thank.

Share the news now

Source : Viblo