Kubernetes teams need rapid object management. They often choose kubectl
imperative commands for direct control. This guide shows how to create, update, delete, and manage live Kubernetes objects on the fly. You learn syntax, patterns, workflow, and real command examples.
TL;DR
kubectl
Imperative commands let you manage live objects with single-line commands.- Common commands:
create
,run
,expose
,scale
,label
,annotate
,patch
,delete
. - Syntax:
kubectl --options
. - Imperative flow: user → API server → etcd → scheduler → kubelet.
- Use cases: quick testing, demos, bootstrapping resources, CI scripts.
- Prefer declarative YAML for production; use imperative for ad-hoc tasks.
Imperative Commands Basics
kubectl
ships built-in commands for direct control of Kubernetes objects. You issue commands at shell prompt. kubectl
translates them into API calls. API server writes state to etcd
. Scheduler assigns pods to nodes. Kubelet manages workloads. Imperative commands skip writing YAML files. You get instant results. You can still view YAML later with kubectl get -o yaml
.
Imperative Commands Syntax
All commands share a common syntax pattern:
kubectl <command> <resource-type> <name> [flags]
Key parts:
<command>
: create, run, expose, label, annotate, patch, scale, delete.<resource-type>
: pods, deployments, services, configmaps, secrets, namespaces.<name>
: arbitrary string. Lowercase. No spaces.[flags]
: options controlling image, ports, labels, selectors, replicas.
Example:
kubectl create deployment myapp --image=myimage:1.0 --replicas=3
This command calls POST /apis/apps/v1/namespaces/default/deployments
.
Imperative Commands for Creation
Imperative creation helps in rapid prototyping. You configure objects in one line. kubectl
supports multiple resource types.
Deployment
kubectl create deployment nginx-deploy --image=nginx:1.21 --replicas=2
This creates a Deployment with two replicas.
Pod
kubectl run busybox-pod --image=busybox --restart=Never -- sleep 3600
Here --restart=Never
sets a Pod instead of Deployment.
Service
kubectl expose deployment nginx-deploy --port=80 --target-port=80 --type=ClusterIP
The command maps Deployment pods to a ClusterIP Service.
ConfigMap
kubectl create configmap app-config --from-literal=key1=value1 --from-file=./config.yaml
This embeds key1
and file
content into a ConfigMap
.
Imperative Commands for Updates
Imperative updates let you tweak live objects without full YAML. Use scale
, label
, annotate
, patch
, and rollout
.
Scale
kubectl scale deployment nginx-deploy --replicas=5
Adjusts replicas in one call.
Label
kubectl label pods busybox-pod env=dev
Adds or updates labels on existing Pods.
Annotate
kubectl annotate svc myservice [email protected]
Stores metadata on Service objects.
Patch
kubectl patch deployment nginx-deploy -p '{"spec":{"template":{"spec":{"containers":[{"name":"nginx","image":"nginx:1.22"}]}}}}'
Applies JSON Merge Patch on the Deployment spec.
Rollout Restart
kubectl rollout restart deployment nginx-deploy
Triggers a rolling restart by updating pod template hash.
Imperative Commands for Deletion
Remove objects with delete commands. This calls the DELETE method on resources.
kubectl delete deployment nginx-deploy
kubectl delete pod busybox-pod
kubectl delete svc myservice
You can delete by label selector:
kubectl delete pods -l env=dev
This deletes all Pods with the label env=dev
.
Use Cases and Patterns
Use imperative commands when you need fast feedback. Ideal for:
- Local demos and workshops.
- CI/CD pipelines that bootstrap test clusters.
- Debug scripts requiring quick updates.
- Edge cases where small patches suffice.
- Learning and experimentation in training labs.
Reserve declarative YAML for production workloads. Store versioned manifests in Git. Use kubectl apply for drift control.
References
Suggested Reading
PostHashID: 740d856262845f61b8656b7cd39e5a9cd53ee01056be899c6b0ccf9c4a4c709d