move obj mgmt files to tasks (#14374)
* move obj mgmt files to tasks * add redirects for moved files
This commit is contained in:
committed by
Kubernetes Prow Robot
parent
2029b9b9d5
commit
110f5e1d5f
@@ -0,0 +1,4 @@
|
||||
---
|
||||
title: "Manage Kubernetes Objects"
|
||||
weight: 25
|
||||
---
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,171 @@
|
||||
---
|
||||
title: Managing Kubernetes Objects Using Imperative Commands
|
||||
content_template: templates/task
|
||||
weight: 30
|
||||
---
|
||||
|
||||
{{% capture overview %}}
|
||||
Kubernetes objects can quickly be created, updated, and deleted directly using
|
||||
imperative commands built into the `kubectl` command-line tool. This document
|
||||
explains how those commands are organized and how to use them to manage live objects.
|
||||
{{% /capture %}}
|
||||
|
||||
{{% capture prerequisites %}}
|
||||
Install [`kubectl`](docs/tasks/tools/install-kubectl/).
|
||||
|
||||
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
|
||||
|
||||
{{% /capture %}}
|
||||
|
||||
{{% capture steps %}}
|
||||
|
||||
## Trade-offs
|
||||
|
||||
The `kubectl` tool supports three kinds of object management:
|
||||
|
||||
* Imperative commands
|
||||
* Imperative object configuration
|
||||
* Declarative object configuration
|
||||
|
||||
See [Kubernetes Object Management](/docs/concepts/overview/object-management-kubectl/overview/)
|
||||
for a discussion of the advantages and disadvantage of each kind of object management.
|
||||
|
||||
## How to create objects
|
||||
|
||||
The `kubectl` tool supports verb-driven commands for creating some of the most common
|
||||
object types. The commands are named to be recognizable to users unfamiliar with
|
||||
the Kubernetes object types.
|
||||
|
||||
- `run`: Create a new Deployment object to run Containers in one or more Pods.
|
||||
- `expose`: Create a new Service object to load balance traffic across Pods.
|
||||
- `autoscale`: Create a new Autoscaler object to automatically horizontally scale a controller, such as a Deployment.
|
||||
|
||||
The `kubectl` tool also supports creation commands driven by object type.
|
||||
These commands support more object types and are more explicit about
|
||||
their intent, but require users to know the type of objects they intend
|
||||
to create.
|
||||
|
||||
- `create <objecttype> [<subtype>] <instancename>`
|
||||
|
||||
Some objects types have subtypes that you can specify in the `create` command.
|
||||
For example, the Service object has several subtypes including ClusterIP,
|
||||
LoadBalancer, and NodePort. Here's an example that creates a Service with
|
||||
subtype NodePort:
|
||||
|
||||
```shell
|
||||
kubectl create service nodeport <myservicename>
|
||||
```
|
||||
|
||||
In the preceding example, the `create service nodeport` command is called
|
||||
a subcommand of the `create service` command.
|
||||
|
||||
You can use the `-h` flag to find the arguments and flags supported by
|
||||
a subcommand:
|
||||
|
||||
```shell
|
||||
kubectl create service nodeport -h
|
||||
```
|
||||
|
||||
## How to update objects
|
||||
|
||||
The `kubectl` command supports verb-driven commands for some common update operations.
|
||||
These commands are named to enable users unfamiliar with Kubernetes
|
||||
objects to perform updates without knowing the specific fields
|
||||
that must be set:
|
||||
|
||||
- `scale`: Horizontally scale a controller to add or remove Pods by updating the replica count of the controller.
|
||||
- `annotate`: Add or remove an annotation from an object.
|
||||
- `label`: Add or remove a label from an object.
|
||||
|
||||
The `kubectl` command also supports update commands driven by an aspect of the object.
|
||||
Setting this aspect may set different fields for different object types:
|
||||
|
||||
- `set` `<field>`: Set an aspect of an object.
|
||||
|
||||
{{< note >}}
|
||||
In Kubernetes version 1.5, not every verb-driven command has an associated aspect-driven command.
|
||||
{{< /note >}}
|
||||
|
||||
The `kubectl` tool supports these additional ways to update a live object directly,
|
||||
however they require a better understanding of the Kubernetes object schema.
|
||||
|
||||
- `edit`: Directly edit the raw configuration of a live object by opening its configuration in an editor.
|
||||
- `patch`: Directly modify specific fields of a live object by using a patch string.
|
||||
For more details on patch strings, see the patch section in
|
||||
[API Conventions](https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#patch-operations).
|
||||
|
||||
## How to delete objects
|
||||
|
||||
You can use the `delete` command to delete an object from a cluster:
|
||||
|
||||
- `delete <type>/<name>`
|
||||
|
||||
{{< note >}}
|
||||
You can use `kubectl delete` for both imperative commands and imperative object
|
||||
configuration. The difference is in the arguments passed to the command. To use
|
||||
`kubectl delete` as an imperative command, pass the object to be deleted as
|
||||
an argument. Here's an example that passes a Deployment object named nginx:
|
||||
{{< /note >}}
|
||||
|
||||
```shell
|
||||
kubectl delete deployment/nginx
|
||||
```
|
||||
|
||||
## How to view an object
|
||||
|
||||
{{< comment >}}
|
||||
TODO(pwittrock): Uncomment this when implemented.
|
||||
|
||||
You can use `kubectl view` to print specific fields of an object.
|
||||
|
||||
- `view`: Prints the value of a specific field of an object.
|
||||
|
||||
{{< /comment >}}
|
||||
|
||||
|
||||
|
||||
There are several commands for printing information about an object:
|
||||
|
||||
- `get`: Prints basic information about matching objects. Use `get -h` to see a list of options.
|
||||
- `describe`: Prints aggregated detailed information about matching objects.
|
||||
- `logs`: Prints the stdout and stderr for a container running in a Pod.
|
||||
|
||||
## Using `set` commands to modify objects before creation
|
||||
|
||||
There are some object fields that don't have a flag you can use
|
||||
in a `create` command. In some of those cases, you can use a combination of
|
||||
`set` and `create` to specify a value for the field before object
|
||||
creation. This is done by piping the output of the `create` command to the
|
||||
`set` command, and then back to the `create` command. Here's an example:
|
||||
|
||||
```sh
|
||||
kubectl create service clusterip my-svc --clusterip="None" -o yaml --dry-run | kubectl set selector --local -f - 'environment=qa' -o yaml | kubectl create -f -
|
||||
```
|
||||
|
||||
1. The `kubectl create service -o yaml --dry-run` command creates the configuration for the Service, but prints it to stdout as YAML instead of sending it to the Kubernetes API server.
|
||||
1. The `kubectl set selector --local -f - -o yaml` command reads the configuration from stdin, and writes the updated configuration to stdout as YAML.
|
||||
1. The `kubectl create -f -` command creates the object using the configuration provided via stdin.
|
||||
|
||||
## Using `--edit` to modify objects before creation
|
||||
|
||||
You can use `kubectl create --edit` to make arbitrary changes to an object
|
||||
before it is created. Here's an example:
|
||||
|
||||
```sh
|
||||
kubectl create service clusterip my-svc --clusterip="None" -o yaml --dry-run > /tmp/srv.yaml
|
||||
kubectl create --edit -f /tmp/srv.yaml
|
||||
```
|
||||
|
||||
1. The `kubectl create service` command creates the configuration for the Service and saves it to `/tmp/srv.yaml`.
|
||||
1. The `kubectl create --edit` command opens the configuration file for editing before it creates the object.
|
||||
|
||||
{{% /capture %}}
|
||||
|
||||
{{% capture whatsnext %}}
|
||||
|
||||
* [Managing Kubernetes Objects Using Object Configuration (Imperative)](/docs/tasks/manage-kubernetes-objects/imperative-config/)
|
||||
* [Managing Kubernetes Objects Using Object Configuration (Declarative)](/docs/tasks/manage-kubernetes-objects/declarative-config/)
|
||||
* [Kubectl Command Reference](/docs/reference/generated/kubectl/kubectl/)
|
||||
* [Kubernetes API Reference](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/)
|
||||
|
||||
{{% /capture %}}
|
||||
@@ -0,0 +1,154 @@
|
||||
---
|
||||
title: Imperative Management of Kubernetes Objects Using Configuration Files
|
||||
content_template: templates/task
|
||||
weight: 40
|
||||
---
|
||||
|
||||
{{% capture overview %}}
|
||||
Kubernetes objects can be created, updated, and deleted by using the `kubectl`
|
||||
command-line tool along with an object configuration file written in YAML or JSON.
|
||||
This document explains how to define and manage objects using configuration files.
|
||||
{{% /capture %}}
|
||||
|
||||
{{% capture prerequisites %}}
|
||||
|
||||
Install [`kubectl`](docs/tasks/tools/install-kubectl/).
|
||||
|
||||
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
|
||||
|
||||
{{% /capture %}}
|
||||
|
||||
{{% capture steps %}}
|
||||
|
||||
## Trade-offs
|
||||
|
||||
The `kubectl` tool supports three kinds of object management:
|
||||
|
||||
* Imperative commands
|
||||
* Imperative object configuration
|
||||
* Declarative object configuration
|
||||
|
||||
See [Kubernetes Object Management](/docs/concepts/overview/object-management-kubectl/overview/)
|
||||
for a discussion of the advantages and disadvantage of each kind of object management.
|
||||
|
||||
## How to create objects
|
||||
|
||||
You can use `kubectl create -f` to create an object from a configuration file.
|
||||
Refer to the [kubernetes API reference](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/)
|
||||
for details.
|
||||
|
||||
* `kubectl create -f <filename|url>`
|
||||
|
||||
## How to update objects
|
||||
|
||||
{{< warning >}}
|
||||
Updating objects with the `replace` command drops all
|
||||
parts of the spec not specified in the configuration file. This
|
||||
should not be used with objects whose specs are partially managed
|
||||
by the cluster, such as Services of type `LoadBalancer`, where
|
||||
the `externalIPs` field is managed independently from the configuration
|
||||
file. Independently managed fields must be copied to the configuration
|
||||
file to prevent `replace` from dropping them.
|
||||
{{< /warning >}}
|
||||
|
||||
You can use `kubectl replace -f` to update a live object according to a
|
||||
configuration file.
|
||||
|
||||
* `kubectl replace -f <filename|url>`
|
||||
|
||||
## How to delete objects
|
||||
|
||||
You can use `kubectl delete -f` to delete an object that is described in a
|
||||
configuration file.
|
||||
|
||||
* `kubectl delete -f <filename|url>`
|
||||
|
||||
## How to view an object
|
||||
|
||||
You can use `kubectl get -f` to view information about an object that is
|
||||
described in a configuration file.
|
||||
|
||||
* `kubectl get -f <filename|url> -o yaml`
|
||||
|
||||
The `-o yaml` flag specifies that the full object configuration is printed.
|
||||
Use `kubectl get -h` to see a list of options.
|
||||
|
||||
## Limitations
|
||||
|
||||
The `create`, `replace`, and `delete` commands work well when each object's
|
||||
configuration is fully defined and recorded in its configuration
|
||||
file. However when a live object is updated, and the updates are not merged
|
||||
into its configuration file, the updates will be lost the next time a `replace`
|
||||
is executed. This can happen if a controller, such as
|
||||
a HorizontalPodAutoscaler, makes updates directly to a live object. Here's
|
||||
an example:
|
||||
|
||||
1. You create an object from a configuration file.
|
||||
1. Another source updates the object by changing some field.
|
||||
1. You replace the object from the configuration file. Changes made by
|
||||
the other source in step 2 are lost.
|
||||
|
||||
If you need to support multiple writers to the same object, you can use
|
||||
`kubectl apply` to manage the object.
|
||||
|
||||
## Creating and editing an object from a URL without saving the configuration
|
||||
|
||||
Suppose you have the URL of an object configuration file. You can use
|
||||
`kubectl create --edit` to make changes to the configuration before the
|
||||
object is created. This is particularly useful for tutorials and tasks
|
||||
that point to a configuration file that could be modified by the reader.
|
||||
|
||||
```shell
|
||||
kubectl create -f <url> --edit
|
||||
```
|
||||
|
||||
## Migrating from imperative commands to imperative object configuration
|
||||
|
||||
Migrating from imperative commands to imperative object configuration involves
|
||||
several manual steps.
|
||||
|
||||
1. Export the live object to a local object configuration file:
|
||||
|
||||
```shell
|
||||
kubectl get <kind>/<name> -o yaml --export > <kind>_<name>.yaml
|
||||
```
|
||||
|
||||
1. Manually remove the status field from the object configuration file.
|
||||
|
||||
1. For subsequent object management, use `replace` exclusively.
|
||||
|
||||
```shell
|
||||
kubectl replace -f <kind>_<name>.yaml
|
||||
```
|
||||
|
||||
## Defining controller selectors and PodTemplate labels
|
||||
|
||||
{{< warning >}}
|
||||
Updating selectors on controllers is strongly discouraged.
|
||||
{{< /warning >}}
|
||||
|
||||
The recommended approach is to define a single, immutable PodTemplate label
|
||||
used only by the controller selector with no other semantic meaning.
|
||||
|
||||
Example label:
|
||||
|
||||
```yaml
|
||||
selector:
|
||||
matchLabels:
|
||||
controller-selector: "extensions/v1beta1/deployment/nginx"
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
controller-selector: "extensions/v1beta1/deployment/nginx"
|
||||
```
|
||||
|
||||
{{% /capture %}}
|
||||
|
||||
{{% capture whatsnext %}}
|
||||
|
||||
* [Managing Kubernetes Objects Using Imperative Commands](/docs/tasks/manage-kubernetes-objects/imperative-command/)
|
||||
* [Managing Kubernetes Objects Using Object Configuration (Declarative)](/docs/tasks/manage-kubernetes-objects/declarative-config/)
|
||||
* [Kubectl Command Reference](/docs/reference/generated/kubectl/kubectl/)
|
||||
* [Kubernetes API Reference](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/)
|
||||
|
||||
{{% /capture %}}
|
||||
@@ -0,0 +1,830 @@
|
||||
---
|
||||
title: Declarative Management of Kubernetes Objects Using Kustomize
|
||||
content_template: templates/task
|
||||
weight: 20
|
||||
---
|
||||
|
||||
{{% capture overview %}}
|
||||
|
||||
[Kustomize](https://github.com/kubernetes-sigs/kustomize) is a standalone tool
|
||||
to customize Kubernetes objects
|
||||
through a [kustomization file](https://github.com/kubernetes-sigs/kustomize/blob/master/docs/kustomization.yaml).
|
||||
|
||||
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>
|
||||
```
|
||||
|
||||
{{% /capture %}}
|
||||
|
||||
{{% capture prerequisites %}}
|
||||
|
||||
Install [`kubectl`](docs/tasks/tools/install-kubectl/).
|
||||
|
||||
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
|
||||
|
||||
{{% /capture %}}
|
||||
|
||||
{{% capture steps %}}
|
||||
|
||||
## 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
|
||||
|
||||
ConfigMap and Secret hold config or sensitive data that are used by other Kubernetes objects, such as Pods. The source
|
||||
of truth of ConfigMap or Secret are usually from somewhere else, such as a `.properties` file or a ssh key file.
|
||||
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 `files` list in `configMapGenerator`. Here is an example of generating a ConfigMap with a data item from a file content.
|
||||
|
||||
```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 checked by 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
|
||||
```
|
||||
|
||||
ConfigMap can also be generated from literal key-value pairs. To generate a ConfigMap from a literal key-value pair, add an entry to `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
|
||||
```
|
||||
|
||||
#### secretGenerator
|
||||
|
||||
You can generate Secrets from files or literal key-value pairs. To generate a Secret from a file, add an entry to `files` list in `secretGenerator`. Here is an example of generating a Secret with a data item from a file.
|
||||
|
||||
```shell
|
||||
# Create a password.txt file
|
||||
cat <<EOF >./password.txt
|
||||
username=admin
|
||||
password=secret
|
||||
EOF
|
||||
|
||||
cat <<EOF >./kustomization.yaml
|
||||
secretGenerator:
|
||||
- name: example-secret-1
|
||||
files:
|
||||
- password.txt
|
||||
EOF
|
||||
```
|
||||
|
||||
The generated Secret is as follows:
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
data:
|
||||
password.txt: dXNlcm5hbWU9YWRtaW4KcGFzc3dvcmQ9c2VjcmV0Cg==
|
||||
kind: Secret
|
||||
metadata:
|
||||
name: example-secret-1-t2kt65hgtb
|
||||
type: Opaque
|
||||
```
|
||||
|
||||
To generate a Secret from a literal key-value pair, add an entry to `literals` list in `secretGenerator`. Here is an example of generating a Secret with a data item from a key-value pair.
|
||||
|
||||
```shell
|
||||
cat <<EOF >./kustomization.yaml
|
||||
secretGenerator:
|
||||
- name: example-secret-2
|
||||
literals:
|
||||
- username=admin
|
||||
- password=secert
|
||||
EOF
|
||||
```
|
||||
|
||||
The generated Secret is as follows:
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
data:
|
||||
password: c2VjZXJ0
|
||||
username: YWRtaW4=
|
||||
kind: Secret
|
||||
metadata:
|
||||
name: example-secret-2-t52t6g96d8
|
||||
type: Opaque
|
||||
```
|
||||
|
||||
#### generatorOptions
|
||||
|
||||
The generated ConfigMaps and Secrets have a suffix appended by hashing the contents. This ensures that a new ConfigMap or Secret is generated when the content is changed. To disable the behavior of appending a suffix, one can use `generatorOptions`. Besides that, it is also possible to specify cross-cutting options for generated ConfigMaps and Secrets.
|
||||
|
||||
```shell
|
||||
cat <<EOF >./kustomization.yaml
|
||||
configMapGenerator:
|
||||
- name: example-configmap-3
|
||||
literals:
|
||||
- FOO=Bar
|
||||
generatorOptions:
|
||||
disableNameSuffixHash: true
|
||||
labels:
|
||||
type: generated
|
||||
annotations:
|
||||
note: generated
|
||||
EOF
|
||||
```
|
||||
|
||||
Run`kubectl kustomize ./` to view the generated ConfigMap:
|
||||
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
data:
|
||||
FOO: Bar
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
annotations:
|
||||
note: generated
|
||||
labels:
|
||||
type: generated
|
||||
name: example-configmap-3
|
||||
```
|
||||
|
||||
### Setting cross-cutting fields
|
||||
|
||||
It is quite common to set cross-cutting fields for all Kubernetes resources in a project.
|
||||
Some use cases for setting cross-cutting fields:
|
||||
|
||||
* setting the same namespace for all Resource
|
||||
* adding the same name prefix or suffix
|
||||
* adding the same set of labels
|
||||
* adding the same set of annotations
|
||||
|
||||
Here is an example:
|
||||
|
||||
```shell
|
||||
# Create a deployment.yaml
|
||||
cat <<EOF >./deployment.yaml
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: nginx-deployment
|
||||
labels:
|
||||
app: nginx
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nginx
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx
|
||||
EOF
|
||||
|
||||
cat <<EOF >./kustomization.yaml
|
||||
namespace: my-namespace
|
||||
namePrefix: dev-
|
||||
nameSuffix: "-001"
|
||||
commonLabels:
|
||||
app: bingo
|
||||
commonAnnotations:
|
||||
oncallPager: 800-555-1212
|
||||
resources:
|
||||
- deployment.yaml
|
||||
EOF
|
||||
```
|
||||
|
||||
Run `kubectl kustomize ./` to view those fields are all set in the Deployment Resource:
|
||||
|
||||
```yaml
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
annotations:
|
||||
oncallPager: 800-555-1212
|
||||
labels:
|
||||
app: bingo
|
||||
name: dev-nginx-deployment-001
|
||||
namespace: my-namespace
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
app: bingo
|
||||
template:
|
||||
metadata:
|
||||
annotations:
|
||||
oncallPager: 800-555-1212
|
||||
labels:
|
||||
app: bingo
|
||||
spec:
|
||||
containers:
|
||||
- image: nginx
|
||||
name: nginx
|
||||
```
|
||||
|
||||
### Composing and Customizing Resources
|
||||
|
||||
It is common to compose a set of Resources in a project and manage them inside
|
||||
the same file or directory.
|
||||
Kustomize offers composing Resources from different files and applying patches or other customization to them.
|
||||
|
||||
#### Composing
|
||||
|
||||
Kustomize supports composition of different resources. The `resources` field, in the `kustomization.yaml` file, defines the list of resources to include in a configuration. Set the path to a resource's configuration file in the `resources` list.
|
||||
Here is an example for an nginx application with a Deployment and a Service.
|
||||
|
||||
```shell
|
||||
# Create a deployment.yaml file
|
||||
cat <<EOF > deployment.yaml
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-nginx
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
run: my-nginx
|
||||
replicas: 2
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
run: my-nginx
|
||||
spec:
|
||||
containers:
|
||||
- name: my-nginx
|
||||
image: nginx
|
||||
ports:
|
||||
- containerPort: 80
|
||||
EOF
|
||||
|
||||
# Create a service.yaml file
|
||||
cat <<EOF > service.yaml
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: my-nginx
|
||||
labels:
|
||||
run: my-nginx
|
||||
spec:
|
||||
ports:
|
||||
- port: 80
|
||||
protocol: TCP
|
||||
selector:
|
||||
run: my-nginx
|
||||
EOF
|
||||
|
||||
# Create a kustomization.yaml composing them
|
||||
cat <<EOF >./kustomization.yaml
|
||||
resources:
|
||||
- deployment.yaml
|
||||
- service.yaml
|
||||
EOF
|
||||
```
|
||||
|
||||
The Resources from `kubectl kustomize ./` contains both the Deployment and the Service objects.
|
||||
|
||||
#### Customizing
|
||||
|
||||
On top of Resources, one can apply different customizations by applying patches. Kustomize supports different patching
|
||||
mechanisms through `patchesStrategicMerge` and `patchesJson6902`. `patchesStrategicMerge` is a list of file paths. Each file should be resolved to a [strategic merge patch](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/strategic-merge-patch.md). The names inside the patches must match Resource names that are already loaded. Small patches that do one thing are recommended. For example, create one patch for increasing the deployment replica number and another patch for setting the memory limit.
|
||||
|
||||
```shell
|
||||
# Create a deployment.yaml file
|
||||
cat <<EOF > deployment.yaml
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-nginx
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
run: my-nginx
|
||||
replicas: 2
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
run: my-nginx
|
||||
spec:
|
||||
containers:
|
||||
- name: my-nginx
|
||||
image: nginx
|
||||
ports:
|
||||
- containerPort: 80
|
||||
EOF
|
||||
|
||||
# Create a patch increase_replicas.yaml
|
||||
cat <<EOF > increase_replicas.yaml
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-nginx
|
||||
spec:
|
||||
replicas: 3
|
||||
EOF
|
||||
|
||||
# Create another patch set_memory.yaml
|
||||
cat <<EOF > set_memory.yaml
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-nginx
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: my-nginx
|
||||
resources:
|
||||
limits:
|
||||
memory: 512Mi
|
||||
EOF
|
||||
|
||||
cat <<EOF >./kustomization.yaml
|
||||
resources:
|
||||
- deployment.yaml
|
||||
patchesStrategicMerge:
|
||||
- increase_replicas.yaml
|
||||
- set_memory.yaml
|
||||
EOF
|
||||
```
|
||||
|
||||
Run `kubectl kustomize ./` to view the Deployment:
|
||||
|
||||
```yaml
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-nginx
|
||||
spec:
|
||||
replicas: 3
|
||||
selector:
|
||||
matchLabels:
|
||||
run: my-nginx
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
run: my-nginx
|
||||
spec:
|
||||
containers:
|
||||
- image: nginx
|
||||
limits:
|
||||
memory: 512Mi
|
||||
name: my-nginx
|
||||
ports:
|
||||
- containerPort: 80
|
||||
```
|
||||
|
||||
Not all Resources or fields support strategic merge patches. To support modifying arbitrary fields in arbitrary Resources,
|
||||
Kustomize offers applying [JSON patch](https://tools.ietf.org/html/rfc6902) through `patchesJson6902`.
|
||||
To find the correct Resource for a Json patch, the group, version, kind and name of that Resource need to be
|
||||
specified in `kustomization.yaml`. For example, increasing the replica number of a Deployment object can also be done
|
||||
through `patchesJson6902`.
|
||||
|
||||
```shell
|
||||
# Create a deployment.yaml file
|
||||
cat <<EOF > deployment.yaml
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-nginx
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
run: my-nginx
|
||||
replicas: 2
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
run: my-nginx
|
||||
spec:
|
||||
containers:
|
||||
- name: my-nginx
|
||||
image: nginx
|
||||
ports:
|
||||
- containerPort: 80
|
||||
EOF
|
||||
|
||||
# Create a json patch
|
||||
cat <<EOF > patch.yaml
|
||||
- op: replace
|
||||
path: /spec/replicas
|
||||
value: 3
|
||||
EOF
|
||||
|
||||
# Create a kustomization.yaml
|
||||
cat <<EOF >./kustomization.yaml
|
||||
resources:
|
||||
- deployment.yaml
|
||||
|
||||
patchesJson6902:
|
||||
- target:
|
||||
group: apps
|
||||
version: v1
|
||||
kind: Deployment
|
||||
name: my-nginx
|
||||
path: patch.yaml
|
||||
EOF
|
||||
```
|
||||
|
||||
Run `kubectl kustomize ./` to see the `replicas` field is updated:
|
||||
|
||||
```yaml
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-nginx
|
||||
spec:
|
||||
replicas: 3
|
||||
selector:
|
||||
matchLabels:
|
||||
run: my-nginx
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
run: my-nginx
|
||||
spec:
|
||||
containers:
|
||||
- image: nginx
|
||||
name: my-nginx
|
||||
ports:
|
||||
- containerPort: 80
|
||||
```
|
||||
|
||||
In addition to patches, Kustomize also offers customizing container images or injecting field values from other objects into containers
|
||||
without creating patches. For example, you can change the image used inside containers by specifying the new image in `images` field in `kustomization.yaml`.
|
||||
|
||||
```shell
|
||||
cat <<EOF > deployment.yaml
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-nginx
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
run: my-nginx
|
||||
replicas: 2
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
run: my-nginx
|
||||
spec:
|
||||
containers:
|
||||
- name: my-nginx
|
||||
image: nginx
|
||||
ports:
|
||||
- containerPort: 80
|
||||
EOF
|
||||
|
||||
cat <<EOF >./kustomization.yaml
|
||||
resources:
|
||||
- deployment.yaml
|
||||
images:
|
||||
- name: nginx
|
||||
newName: my.image.registry/nginx
|
||||
newTag: 1.4.0
|
||||
EOF
|
||||
```
|
||||
Run `kubectl kustomize ./` to see that the image being used is updated:
|
||||
```yaml
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-nginx
|
||||
spec:
|
||||
replicas: 2
|
||||
selector:
|
||||
matchLabels:
|
||||
run: my-nginx
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
run: my-nginx
|
||||
spec:
|
||||
containers:
|
||||
- image: my.image.registry/nginx:1.4.0
|
||||
name: my-nginx
|
||||
ports:
|
||||
- containerPort: 80
|
||||
```
|
||||
|
||||
Sometimes, the application running in a Pod may need to use configuration values from other objects. For example,
|
||||
a Pod from a Deployment object need to read the corresponding Service name from Env or as a command argument.
|
||||
Since the Service name may change as `namePrefix` or `nameSuffix` is added in the `kustomization.yaml` file. It is
|
||||
not recommended to hard code the Service name in the command argument. For this usage, Kustomize can inject the Service name into containers through `vars`.
|
||||
|
||||
```shell
|
||||
# Create a deployment.yaml file
|
||||
cat <<EOF > deployment.yaml
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-nginx
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
run: my-nginx
|
||||
replicas: 2
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
run: my-nginx
|
||||
spec:
|
||||
containers:
|
||||
- name: my-nginx
|
||||
image: nginx
|
||||
command: ["start", "--host", "\$(MY_SERVICE_NAME)"]
|
||||
EOF
|
||||
|
||||
# Create a service.yaml file
|
||||
cat <<EOF > service.yaml
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: my-nginx
|
||||
labels:
|
||||
run: my-nginx
|
||||
spec:
|
||||
ports:
|
||||
- port: 80
|
||||
protocol: TCP
|
||||
selector:
|
||||
run: my-nginx
|
||||
EOF
|
||||
|
||||
cat <<EOF >./kustomization.yaml
|
||||
namePrefix: dev-
|
||||
nameSuffix: "-001"
|
||||
|
||||
resources:
|
||||
- deployment.yaml
|
||||
- service.yaml
|
||||
|
||||
vars:
|
||||
- name: MY_SERVICE_NAME
|
||||
objref:
|
||||
kind: Service
|
||||
name: my-nginx
|
||||
apiVersion: v1
|
||||
EOF
|
||||
```
|
||||
|
||||
Run `kubectl kustomize ./` to see that the Service name injected into containers is `dev-my-nginx-001`:
|
||||
|
||||
```yaml
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: dev-my-nginx-001
|
||||
spec:
|
||||
replicas: 2
|
||||
selector:
|
||||
matchLabels:
|
||||
run: my-nginx
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
run: my-nginx
|
||||
spec:
|
||||
containers:
|
||||
- command:
|
||||
- start
|
||||
- --host
|
||||
- dev-my-nginx-001
|
||||
image: nginx
|
||||
name: my-nginx
|
||||
```
|
||||
|
||||
## Bases and Overlays
|
||||
|
||||
Kustomize has the concepts of **bases** and **overlays**. A **base** is a directory with a `kustomization.yaml`, which contains a
|
||||
set of resources and associated customization. A base could be either a local directory or a directory from a remote repo,
|
||||
as long as a `kustomization.yaml` is present inside. An **overlay** is a directory with a `kustomization.yaml` that refers to other
|
||||
kustomization directories as its `bases`. A **base** has no knowledge of an overlay and can be used in multiple overlays.
|
||||
An overlay may have multiple bases and it composes all resources
|
||||
from bases and may also have customization on top of them.
|
||||
|
||||
Here is an example of a base:
|
||||
|
||||
```shell
|
||||
# Create a directory to hold the base
|
||||
mkdir base
|
||||
# Create a base/deployment.yaml
|
||||
cat <<EOF > base/deployment.yaml
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-nginx
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
run: my-nginx
|
||||
replicas: 2
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
run: my-nginx
|
||||
spec:
|
||||
containers:
|
||||
- name: my-nginx
|
||||
image: nginx
|
||||
EOF
|
||||
|
||||
# Create a base/service.yaml file
|
||||
cat <<EOF > base/service.yaml
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: my-nginx
|
||||
labels:
|
||||
run: my-nginx
|
||||
spec:
|
||||
ports:
|
||||
- port: 80
|
||||
protocol: TCP
|
||||
selector:
|
||||
run: my-nginx
|
||||
EOF
|
||||
# Create a base/kustomization.yaml
|
||||
cat <<EOF > base/kustomization.yaml
|
||||
resources:
|
||||
- deployment.yaml
|
||||
- service.yaml
|
||||
```
|
||||
|
||||
This base can be used in multiple overlays. You can add different `namePrefix` or other cross-cutting fields
|
||||
in different overlays. Here are two overlays using the same base.
|
||||
|
||||
```shell
|
||||
mkdir dev
|
||||
cat <<EOF > dev/kustomization.yaml
|
||||
bases:
|
||||
- ../base
|
||||
namePrefix: dev-
|
||||
EOF
|
||||
|
||||
mkdir prod
|
||||
cat <<EOF > prod/kustomization.yaml
|
||||
bases:
|
||||
- ../base
|
||||
namePrefix: prod-
|
||||
EOF
|
||||
```
|
||||
|
||||
## How to apply/view/delete objects using Kustomize
|
||||
|
||||
Use `--kustomize` or `-k` in `kubectl` commands to recognize Resources managed by `kustomization.yaml`.
|
||||
Note that `-k` should point to a kustomization directory, such as
|
||||
|
||||
```shell
|
||||
kubectl apply -k <kustomization directory>/
|
||||
```
|
||||
|
||||
Given the following `kustomization.yaml`,
|
||||
|
||||
```shell
|
||||
# Create a deployment.yaml file
|
||||
cat <<EOF > deployment.yaml
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: my-nginx
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
run: my-nginx
|
||||
replicas: 2
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
run: my-nginx
|
||||
spec:
|
||||
containers:
|
||||
- name: my-nginx
|
||||
image: nginx
|
||||
ports:
|
||||
- containerPort: 80
|
||||
EOF
|
||||
|
||||
# Create a kustomization.yaml
|
||||
cat <<EOF >./kustomization.yaml
|
||||
namePrefix: dev-
|
||||
commonLabels:
|
||||
app: my-nginx
|
||||
resources:
|
||||
- deployment.yaml
|
||||
EOF
|
||||
```
|
||||
|
||||
Run the following command to apply the Deployment object `dev-my-nginx`:
|
||||
|
||||
```shell
|
||||
> kubectl apply -k ./
|
||||
deployment.apps/dev-my-nginx created
|
||||
```
|
||||
|
||||
Run one of the following commands to view the Deployment object `dev-my-nginx`:
|
||||
|
||||
```shell
|
||||
kubectl get -k ./
|
||||
```
|
||||
|
||||
```shell
|
||||
kubectl describe -k ./
|
||||
```
|
||||
|
||||
Run the following command to delete the Deployment object `dev-my-nginx`:
|
||||
|
||||
```shell
|
||||
> kubectl delete -k ./
|
||||
deployment.apps "dev-my-nginx" deleted
|
||||
```
|
||||
|
||||
## Kustomize Feature List
|
||||
|
||||
| Field | Type | Explanation |
|
||||
|-----------------------|--------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------|
|
||||
| namespace | string | add namespace to all resources |
|
||||
| namePrefix | string | value of this field is prepended to the names of all resources |
|
||||
| nameSuffix | string | value of this field is appended to the names of all resources |
|
||||
| commonlabels | map[string]string | labels to add to all resources and selectors |
|
||||
| commonAnnotations | map[string]string | annotations to add to all resources |
|
||||
| resources | []string | each entry in this list must resolve to an existing resource configuration file |
|
||||
| configmapGenerator | [][ConfigMapArgs](https://github.com/kubernetes-sigs/kustomize/blob/master/pkg/types/kustomization.go#L195) | Each entry in this list generates a ConfigMap |
|
||||
| secretGenerator | [][SecretArgs](https://github.com/kubernetes-sigs/kustomize/blob/master/pkg/types/kustomization.go#L201) | Each entry in this list generates a Secret |
|
||||
| generatorOptions | [GeneratorOptions](https://github.com/kubernetes-sigs/kustomize/blob/master/pkg/types/kustomization.go#L239) | Modify behaviors of all ConfigMap and Secret generatos |
|
||||
| bases | []string | Each entry in this list should resolve to a directory containing a kustomization.yaml file |
|
||||
| patchesStrategicMerge | []string | Each entry in this list should resolve a strategic merge patch of a Kubernetes object |
|
||||
| patchesJson6902 | [][Json6902](https://github.com/kubernetes-sigs/kustomize/blob/master/pkg/patch/json6902.go#L23) | Each entry in this list should resolve to a Kubernetes object and a Json Patch |
|
||||
| vars | [][Var](https://github.com/kubernetes-sigs/kustomize/blob/master/pkg/types/var.go#L31) | Each entry is to capture text from one resource's field |
|
||||
| images | [][Image](https://github.com/kubernetes-sigs/kustomize/blob/master/pkg/image/image.go#L23) | Each entry is to modify the name, tags and/or digest for one image without creating patches |
|
||||
| configurations | []string | Each entry in this list should resolve to a file containing [Kustomize transformer configurations](https://github.com/kubernetes-sigs/kustomize/tree/master/examples/transformerconfigs) |
|
||||
| crds | []string | Each entry in this list should resolve to an OpenAPI definition file for Kubernetes types |
|
||||
|
||||
{{% /capture %}}
|
||||
|
||||
{{% capture whatsnext %}}
|
||||
|
||||
* [Kustomize](https://github.com/kubernetes-sigs/kustomize)
|
||||
* [Kubectl Book](https://kubectl.docs.kubernetes.io)
|
||||
* [Kubectl Command Reference](/docs/reference/generated/kubectl/kubectl/)
|
||||
* [Kubernetes API Reference](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/)
|
||||
|
||||
{{% /capture %}}
|
||||
Reference in New Issue
Block a user