The kubectl
command-line tool supports several different ways to create and manage Kubernetes objects. This document provides an overview of the different approaches. Read the Kubectl book for details of managing objects by Kubectl.
Management technique | Operates on | Recommended environment | Supported writers | Learning curve |
---|---|---|---|---|
Imperative commands | Live objects | Development projects | 1+ | Lowest |
Imperative object configuration | Individual files | Production projects | 1 | Moderate |
Declarative object configuration | Directories of files | Production projects | 1+ | Highest |
When using imperative commands, a user operates directly on live objects in a cluster. The user provides operations to the kubectl
command as arguments or flags.
This is the recommended way to get started or to run a one-off task in a cluster. Because this technique operates directly on live objects, it provides no history of previous configurations.
Run an instance of the nginx container by creating a Deployment object:
kubectl create deployment nginx --image nginx
Advantages compared to object configuration:
Disadvantages compared to object configuration:
In imperative object configuration, the kubectl command specifies the operation (create, replace, etc.), optional flags and at least one file name. The file specified must contain a full definition of the object in YAML or JSON format.
See the API reference for more details on object definitions.
replace
command replaces the existing spec with the newly provided one, dropping all changes to the object missing from the configuration file. This approach should not be used with resource types whose specs are updated independently of the configuration file. Services of type LoadBalancer
, for example, have their externalIPs
field updated independently from the configuration by the cluster. Create the objects defined in a configuration file:
kubectl create -f nginx.yaml
Delete the objects defined in two configuration files:
kubectl delete -f nginx.yaml -f redis.yaml
Update the objects defined in a configuration file by overwriting the live configuration:
kubectl replace -f nginx.yaml
Advantages compared to imperative commands:
Disadvantages compared to imperative commands:
Advantages compared to declarative object configuration:
Disadvantages compared to declarative object configuration:
When using declarative object configuration, a user operates on object configuration files stored locally, however the user does not define the operations to be taken on the files. Create, update, and delete operations are automatically detected per-object by kubectl
. This enables working on directories, where different operations might be needed for different objects.
patch
API operation to write only observed differences, instead of using the replace
API operation to replace the entire object configuration. Process all object configuration files in the configs
directory, and create or patch the live objects. You can first diff
to see what changes are going to be made, and then apply:
kubectl diff -f configs/ kubectl apply -f configs/
Recursively process directories:
kubectl diff -R -f configs/ kubectl apply -R -f configs/
Advantages compared to imperative object configuration:
Disadvantages compared to imperative object configuration:
© 2022 The Kubernetes Authors
Documentation Distributed under CC BY 4.0.
https://kubernetes.io/docs/concepts/overview/working-with-objects/object-management