| 1 | # kustomize |
| 2 | |
| 3 | `kustomize` lets you customize raw, template-free YAML |
| 4 | files for multiple purposes, leaving the original YAML |
| 5 | untouched and usable as is. |
| 6 | |
| 7 | `kustomize` targets kubernetes; it understands and can |
| 8 | patch [kubernetes style] API objects. It's like |
| 9 | [`make`], in that what it does is declared in a file, |
| 10 | and it's like [`sed`], in that it emits edited text. |
| 11 | |
| 12 | This tool is sponsored by [sig-cli] ([KEP]). |
| 13 | |
| 14 | - [Installation instructions](https://kubectl.docs.kubernetes.io/installation/kustomize/) |
| 15 | - [General documentation](https://kubectl.docs.kubernetes.io/references/kustomize/) |
| 16 | - [Examples](examples) |
| 17 | |
| 18 | [](https://prow.k8s.io/job-history/kubernetes-jenkins/pr-logs/directory/kustomize-presubmit-master) |
| 19 | [](https://goreportcard.com/report/github.com/kubernetes-sigs/kustomize) |
| 20 | |
| 21 | ## kubectl integration |
| 22 | |
| 23 | To find the kustomize version embedded in recent versions of kubectl, run `kubectl version`: |
| 24 | |
| 25 | ```sh |
| 26 | > kubectl version --client |
| 27 | Client Version: v1.31.0 |
| 28 | Kustomize Version: v5.4.2 |
| 29 | ``` |
| 30 | |
| 31 | The kustomize build flow at [v2.0.3] was added |
| 32 | to [kubectl v1.14][kubectl announcement]. The kustomize |
| 33 | flow in kubectl remained frozen at v2.0.3 until kubectl v1.21, |
| 34 | which [updated it to v4.0.5][kust-in-kubectl update]. It will |
| 35 | be updated on a regular basis going forward, and such updates |
| 36 | will be reflected in the Kubernetes release notes. |
| 37 | |
| 38 | | Kubectl version | Kustomize version | |
| 39 | | --------------- | ----------------- | |
| 40 | | < v1.14 | n/a | |
| 41 | | v1.14-v1.20 | v2.0.3 | |
| 42 | | v1.21 | v4.0.5 | |
| 43 | | v1.22 | v4.2.0 | |
| 44 | | v1.23 | v4.4.1 | |
| 45 | | v1.24 | v4.5.4 | |
| 46 | | v1.25 | v4.5.7 | |
| 47 | | v1.26 | v4.5.7 | |
| 48 | | v1.27 | v5.0.1 | |
| 49 | |
| 50 | [v2.0.3]: https://github.com/kubernetes-sigs/kustomize/releases/tag/v2.0.3 |
| 51 | [#2506]: https://github.com/kubernetes-sigs/kustomize/issues/2506 |
| 52 | [#1500]: https://github.com/kubernetes-sigs/kustomize/issues/1500 |
| 53 | [kust-in-kubectl update]: https://github.com/kubernetes/kubernetes/blob/4d75a6238a6e330337526e0513e67d02b1940b63/CHANGELOG/CHANGELOG-1.21.md#kustomize-updates-in-kubectl |
| 54 | |
| 55 | For examples and guides for using the kubectl integration please |
| 56 | see the [kubernetes documentation]. |
| 57 | |
| 58 | ## Usage |
| 59 | |
| 60 | |
| 61 | ### 1) Make a [kustomization] file |
| 62 | |
| 63 | In some directory containing your YAML [resource] |
| 64 | files (deployments, services, configmaps, etc.), create a |
| 65 | [kustomization] file. |
| 66 | |
| 67 | This file should declare those resources, and any |
| 68 | customization to apply to them, e.g. _add a common |
| 69 | label_. |
| 70 | |
| 71 | ``` |
| 72 | |
| 73 | base: kustomization + resources |
| 74 | |
| 75 | kustomization.yaml deployment.yaml service.yaml |
| 76 | +---------------------------------------------+ +-------------------------------------------------------+ +-----------------------------------+ |
| 77 | | apiVersion: kustomize.config.k8s.io/v1beta1 | | apiVersion: apps/v1 | | apiVersion: v1 | |
| 78 | | kind: Kustomization | | kind: Deployment | | kind: Service | |
| 79 | | labels: | | metadata: | | metadata: | |
| 80 | | - includeSelectors: true | | name: myapp | | name: myapp | |
| 81 | | pairs: | | spec: | | spec: | |
| 82 | | app: myapp | | selector: | | selector: | |
| 83 | | resources: | | matchLabels: | | app: myapp | |
| 84 | | - deployment.yaml | | app: myapp | | ports: | |
| 85 | | - service.yaml | | template: | | - port: 6060 | |
| 86 | | configMapGenerator: | | metadata: | | targetPort: 6060 | |
| 87 | | - name: myapp-map | | labels: | +-----------------------------------+ |
| 88 | | literals: | | app: myapp | |
| 89 | | - KEY=value | | spec: | |
| 90 | +---------------------------------------------+ | containers: | |
| 91 | | - name: myapp | |
| 92 | | image: myapp | |
| 93 | | resources: | |
| 94 | | limits: | |
| 95 | | memory: "128Mi" | |
| 96 | | cpu: "500m" | |
| 97 | | ports: | |
| 98 | | - containerPort: 6060 | |
| 99 | +-------------------------------------------------------+ |
| 100 | |
| 101 | ``` |
| 102 | |
| 103 | File structure: |
| 104 | |
| 105 | > ``` |
| 106 | > ~/someApp |
| 107 | > ├── deployment.yaml |
| 108 | > ├── kustomization.yaml |
| 109 | > └── service.yaml |
| 110 | > ``` |
| 111 | |
| 112 | The resources in this directory could be a fork of |
| 113 | someone else's configuration. If so, you can easily |
| 114 | rebase from the source material to capture |
| 115 | improvements, because you don't modify the resources |
| 116 | directly. |
| 117 | |
| 118 | Generate customized YAML with: |
| 119 | |
| 120 | ``` |
| 121 | kustomize build ~/someApp |
| 122 | ``` |
| 123 | |
| 124 | The YAML can be directly [applied] to a cluster: |
| 125 | |
| 126 | > ``` |
| 127 | > kustomize build ~/someApp | kubectl apply -f - |
| 128 | > ``` |
| 129 | |
| 130 | |
| 131 | ### 2) Create [variants] using [overlays] |
| 132 | |
| 133 | Manage traditional [variants] of a configuration - like |
| 134 | _development_, _staging_ and _production_ - using |
| 135 | [overlays] that modify a common [base]. |
| 136 | |
| 137 | ``` |
| 138 | |
| 139 | overlay: kustomization + patches |
| 140 | |
| 141 | kustomization.yaml replica_count.yaml cpu_count.yaml |
| 142 | +-----------------------------------------------+ +-------------------------------+ +------------------------------------------+ |
| 143 | | apiVersion: kustomize.config.k8s.io/v1beta1 | | apiVersion: apps/v1 | | apiVersion: apps/v1 | |
| 144 | | kind: Kustomization | | kind: Deployment | | kind: Deployment | |
| 145 | | labels: | | metadata: | | metadata: | |
| 146 | | - includeSelectors: true | | name: myapp | | name: myapp | |
| 147 | | pairs: | | spec: | | spec: | |
| 148 | | variant: prod | | replicas: 80 | | template: | |
| 149 | | resources: | +-------------------------------+ | spec: | |
| 150 | | - ../../base | | containers: | |
| 151 | | patches: | | - name: myapp | |
| 152 | | - path: replica_count.yaml | | resources: | |
| 153 | | - path: cpu_count.yaml | | limits: | |
| 154 | +-----------------------------------------------+ | memory: "128Mi" | |
| 155 | | cpu: "7000m" | |
| 156 | +------------------------------------------+ |
| 157 | ``` |
| 158 | |
| 159 | |
| 160 | File structure: |
| 161 | > ``` |
| 162 | > ~/someApp |
| 163 | > ├── base |
| 164 | > │ ├── deployment.yaml |
| 165 | > │ ├── kustomization.yaml |
| 166 | > │ └── service.yaml |
| 167 | > └── overlays |
| 168 | > ├── development |
| 169 | > │ ├── cpu_count.yaml |
| 170 | > │ ├── kustomization.yaml |
| 171 | > │ └── replica_count.yaml |
| 172 | > └── production |
| 173 | > ├── cpu_count.yaml |
| 174 | > ├── kustomization.yaml |
| 175 | > └── replica_count.yaml |
| 176 | > ``` |
| 177 | |
| 178 | Take the work from step (1) above, move it into a |
| 179 | `someApp` subdirectory called `base`, then |
| 180 | place overlays in a sibling directory. |
| 181 | |
| 182 | An overlay is just another kustomization, referring to |
| 183 | the base, and referring to patches to apply to that |
| 184 | base. |
| 185 | |
| 186 | This arrangement makes it easy to manage your |
| 187 | configuration with `git`. The base could have files |
| 188 | from an upstream repository managed by someone else. |
| 189 | The overlays could be in a repository you own. |
| 190 | Arranging the repo clones as siblings on disk avoids |
| 191 | the need for git submodules (though that works fine, if |
| 192 | you are a submodule fan). |
| 193 | |
| 194 | Generate YAML with |
| 195 | |
| 196 | ```sh |
| 197 | kustomize build ~/someApp/overlays/production |
| 198 | ``` |
| 199 | |
| 200 | The YAML can be directly [applied] to a cluster: |
| 201 | |
| 202 | > ```sh |
| 203 | > kustomize build ~/someApp/overlays/production | kubectl apply -f - |
| 204 | > ``` |
| 205 | |
| 206 | ## Community |
| 207 | |
| 208 | - [file a bug](https://kubectl.docs.kubernetes.io/contributing/kustomize/bugs/) |
| 209 | - [contribute a feature](https://kubectl.docs.kubernetes.io/contributing/kustomize/features/) |
| 210 | - [propose a larger enhancement](https://github.com/kubernetes-sigs/kustomize/tree/master/proposals) |
| 211 | |
| 212 | ### Code of conduct |
| 213 | |
| 214 | Participation in the Kubernetes community |
| 215 | is governed by the [Kubernetes Code of Conduct]. |
| 216 | |
| 217 | [`make`]: https://www.gnu.org/software/make |
| 218 | [`sed`]: https://www.gnu.org/software/sed |
| 219 | [DAM]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#declarative-application-management |
| 220 | [KEP]: https://github.com/kubernetes/enhancements/blob/master/keps/sig-cli/2377-Kustomize/README.md |
| 221 | [Kubernetes Code of Conduct]: code-of-conduct.md |
| 222 | [applied]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#apply |
| 223 | [base]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#base |
| 224 | [declarative configuration]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#declarative-application-management |
| 225 | [kubectl announcement]: https://kubernetes.io/blog/2019/03/25/kubernetes-1-14-release-announcement |
| 226 | [kubernetes documentation]: https://kubernetes.io/docs/tasks/manage-kubernetes-objects/kustomization/ |
| 227 | [kubernetes style]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#kubernetes-style-object |
| 228 | [kustomization]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#kustomization |
| 229 | [overlay]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#overlay |
| 230 | [overlays]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#overlay |
| 231 | [release page]: https://github.com/kubernetes-sigs/kustomize/releases |
| 232 | [resource]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#resource |
| 233 | [resources]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#resource |
| 234 | [sig-cli]: https://github.com/kubernetes/community/blob/master/sig-cli/README.md |
| 235 | [variants]: https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#variant |