Tutorials

Detailed walkthroughs of common Kubernetes operations and workflows.

Edit This Page

Managing Kubernetes Objects Using Imperative Commands

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.

Trade-offs

The kubectl tool supports three kinds of object management:

See Kubernetes Object Management 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.

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.

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:

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:

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:

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:

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.

How to delete objects

You can use the delete command to delete an object from a cluster:

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:

kubectl delete deployment/nginx

How to view an object

There are several commands for printing information about an object:

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:

kubectl create service clusterip <myservicename> -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.
  2. The kubectl set --local -f - -o yaml command reads the configuration from stdin, and writes the updated configuration to stdout as YAML.
  3. 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:

kubectl create service clusterip my-svc -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.
  2. The kubectl create --edit command opens the configuration file for editing before it creates the object.

What’s next

Analytics

Create an Issue Edit this Page