kubectl command-line tool supports several different ways to create and manage
Kubernetes objects. This document provides an overview of the different
Warning: A Kubernetes object should be managed using only one technique. Mixing and matching techniques for the same object results in undefined behavior.
|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
kubectl command as arguments or flags.
This is the simplest 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 run nginx --image nginx
Do the same thing using a different syntax:
kubectl create deployment nginx --image nginx
Advantages compared to object configuration:
Disadvantages compared to object configuration:
When using imperative object configuration, a user operates on object configuration files stored locally. An object configuration file defines a full object in either YAML or JSON.
The user provides an operation (create, replace, delete), one or more files,
and flags to the
This technique requires a deep understanding of the Kubernetes object definitions.
Note: While this technique defines the object itself through a declarative configuration file, the operations are imperative: create, replace, delete.
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.
Note: Declarative object configuration retains changes made by other
writers, even if the changes are not merged back to the object configuration file.
This is possible by using the
patch API operation to write only
observed differences, instead of using the
API operation to replace the entire object configuration.
Process all object configuration files in the
configs directory, and
create or patch the live objects:
kubectl apply -f configs/
Recursively process directories:
kubectl apply -R -f configs/
Advantages compared to imperative object configuration:
Disadvantages compared to imperative object configuration: