Declarative Management of Kubernetes Objects Using Kustomize [page]deterministic
[Kustomize](https://github.com/kubernetes-sigs/kustomize) is a standalone tool to customize Kubernetes objects through a [kustomization file](https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#kustomization).
Since 1.14, kubectl also supports the management of Kubernetes objects using a kustomization file. To view resources found in a directory containing a kustomization file, run the following command:
```shell kubectl kustomize <kustomization_directory> ```
To apply those resources, run `kubectl apply` with `--kustomize` or `-k` flag:
```shell kubectl apply -k <kustomization_directory> ```
##
Install [`kubectl`](/docs/tasks/tools/).
## Overview of Kustomize
Kustomize is a tool for customizing Kubernetes configurations. It has the following features to manage application configuration files:
* generating resources from other sources * setting cross-cutting fields for resources * composing and customizing collections of resources
### Generating Resources
ConfigMaps and Secrets hold configuration or sensitive data that are used by other Kubernetes objects, such as Pods. The source of truth of ConfigMaps or Secrets are usually external to a cluster, such as a `.properties` file or an SSH keyfile. Kustomize has `secretGenerator` and `configMapGenerator`, which generate Secret and ConfigMap from files or literals.
#### configMapGenerator
To generate a ConfigMap from a file, add an entry to the `files` list in `configMapGenerator`. Here is an example of generating a ConfigMap with a data item from a `.properties` file:
```shell # Create a application.properties file cat <<EOF >application.properties FOO=Bar EOF
cat <<EOF >./kustomization.yaml configMapGenerator: - name: example-configmap-1 files: - application.properties EOF ```
The generated ConfigMap can be examined with the following command:
```shell kubectl kustomize ./ ```
The generated ConfigMap is:
```yaml apiVersion: v1 data: application.properties: | FOO=Bar kind: ConfigMap metadata: name: example-configmap-1-8mbdf7882g ```
To generate a ConfigMap from an env file, add an entry to the `envs` list in `configMapGenerator`. Here is an example of generating a ConfigMap with a data item from a `.env` file:
```shell # Create a .env file cat <<EOF >.env FOO=Bar EOF
cat <<EOF >./kustomization.yaml configMapGenerator: - name: example-configmap-1 envs: - .env EOF ```
The generated ConfigMap can be examined with the following command:
```shell kubectl kustomize ./ ```
The generated ConfigMap is:
```yaml apiVersion: v1 data: FOO: Bar kind: ConfigMap metadata: name: example-configmap-1-42cfbf598f ```
> Note: Each variable in the `.env` file becomes a separate key in the ConfigMap that you generate. This is different from the previous example which embeds a file named `application.properties` (and all its entries) as the value for a single key.
ConfigMaps can also be generated from literal key-value pairs. To generate a ConfigMap from a literal key-value pair, add an entry to the `literals` list in configMapGenerator. Here is an example of generating a ConfigMap with a data item from a key-value pair:
```shell cat <<EOF >./kustomization.yaml configMapGenerator: - name: example-configmap-2 literals: - FOO=Bar EOF ```
The generated ConfigMap can be checked by the following command:
```shell kubectl kustomize ./ ```
The generated ConfigMap is:
```yaml apiVersion: v1 data: FOO: Bar kind: ConfigMap metadata: name: example-configmap-2-g2hdhfc6tk ```
To use a generated ConfigMap in a Deployment, reference it by the name of the configMapGenerator. Kustomize will automatically replace this name with the generated name.
This is an example deployment that uses a generated ConfigMap:
```yaml # Create an application.properties file cat <<EOF >application.properties FOO=Bar EOF
cat <<EOF >deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: my-app labels: app: my-app spec: selector …(trimmed)