Managing Kubernetes Objects Using Imperative Commands
Install .
You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:
To check the version, enter kubectl version
.
Trade-offs
The kubectl
tool supports three kinds of object management:
- Imperative object configuration
- Declarative object configuration
See Kubernetes Object Management for a discussion of the advantages and disadvantage of each kind of object management.
How to create objects
run
: Create a new Pod to run a Container.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:
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:
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:
- : 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.
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.
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.
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:
How to view an object
There are several commands for printing information about an object:
get
: Prints basic information about matching objects. Useget -h
to see a list of options.describe
: Prints aggregated detailed information about matching objects.- : Prints the stdout and stderr for a container running in a Pod.
- The
kubectl create service -o yaml --dry-run=client
command creates the configuration for the Service, but prints it to stdout as YAML instead of sending it to the Kubernetes API server. - The
kubectl set selector --local -f - -o yaml
command reads the configuration from stdin, and writes the updated configuration to stdout as YAML. - 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:
- The
kubectl create service
command creates the configuration for the Service and saves it to/tmp/srv.yaml
. - The
kubectl create --edit
command opens the configuration file for editing before it creates the object.