Imperative Commands in Kubernetes — A Primer
“kubectl”, the command line tool of K8s is used for interacting with Kubernetes clusters. The tool is used to deploy the container workloads into production clusters, inspect and manage cluster resources, view logs etc. According to K8s it is like a “Swiss Army Knife” of container orchestration and management.
There are fundamentally two types of K8s object management namely Imperative (with kubectl commands) and Declarative ( by writing manifests and using kubectl apply
) modes. Each has its own advantages and disadvantages, and its highly recommended to manage a K8s object by a single method. Imperative commands are in general good for learning and interactive experiments on K8s, though they donot expose the full flexibility of the API. Declarative commands are useful for reproducible deployments and production.
In this article, I attempt to describe on how to use kubectl to interact with clusters and in particular summaries the imperative commands that can be used in our everyday life as part of cluster management.
Object Creation
Imperative commands, objects are created, managed/modified using the CLI. As per kubernetes Documentation, the kubectl
tool 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.
kubectl 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.
kubectl create service nodeport <myservicename>
Some of the verb-driven commands are,
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 auto scale object to automatically horizontally scale a controller, such as a Deployment.autoscale
: Create a new Autoscaler object to automatically horizontally scale a controller, such as a Deployment.
For example to create a namespace, a deployment and a service we can use the following CLI commands:
kubectl create ns tester
kubectl run webserver --image=nginx -n tester
kubectl expose deployments webserver --port 8080 --type LoadBalancer -n tester
Commands for common update operations include,
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.set
<field>
: Set an aspect of an object.
For example to set labels on a deployment, scale pods in a deployment, set environment variable, etc, we can use the following CLI commands
kubectl label deployment webapp app=frontend
kubectl scale --replicas 3 deployment webapp
kubectl set env deployment/webapp WHOAMI="HAL 9000"
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 Convention
We can edit any object in-place using kubectl edit
. This will open up the object’s manifest in the default editor:
kubectl edit deployment/nginx-deployment
kubectl edit svc/nginx-service # Edit the service
KUBE_EDITOR="nano" kubectl edit pod/nginx # Use an alternative editor
Using patch
, objects field can be updated on the fly without having to open up in editor. patch
also allows for more complex updates with various merging and patching strategies. Update API Objects in Place Using kubectl patch detailed documentation on patch
The following command will patch the nginx-deployment
object to update the replicas
field from 4
kubectl patch deployment nginx-deployment -p'{"spec":{"replicas":4}}'
Delete Objects
delete
command can be used to delete an object from a cluster
delete <type>/<name>
For example to delete a nginx deployment,
kubectl delete deployment/nginx
View Objects
Following are the 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.logs
: Prints the stdout and stderr for a container running in a Pod.
Use kubectl get
to list all Deployments, Replica Sets and Pods in your default namespace:
kubectl get deployments # or deploy for shortform
kubectl get replicasets # gets replica set
kubectl get pods # gets pods
To see the object’s state as saved in “etcd”, use the --output
option (or -o
):
kubectl get deployment webapp -o yaml
To gather more details, including recent Events (e.g., errors) related to the object, use the kubectl describe
command:
kubectl describe deployment webapp
kubectl logs, can be used to get the logs of a deployment or pods with appropriate parameters to log the same
kubectl logs deployment/webapp --since 5m > /tmp/logs.txt
grep error /tmp/logs.txt
# more grep
Additional Commands
Kubectl explain
kubectl explain is a kind of built-in documentation for YAML k8s manifest files.
kubectl explain pods
Kubectl run — restart flag
“kubectl run” command basically creates different workloads/objects based on what is being passed to the flag.

References
https://medium.com/@nassim.kebbani/how-to-beat-kubernetes-ckad-certification-c84bff8d61b1