Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

2.1 Anatomy of a kubectl Command

⏱️ ~5 min read

TL;DR: Every kubectl command follows one pattern: kubectl [verb] [resource] [name] [flags]. Master this pattern and you can figure out any command on the fly.


The Command Structure

kubectl  <verb>    <resource>  <name>      <flags>
kubectl  get       pods        my-nginx    --namespace=default
kubectl  describe  node        minikube
kubectl  delete    deployment  my-app      --grace-period=0
kubectl  logs      pod/my-pod  -c sidecar  -f

That’s it. Once this pattern is in muscle memory, you don’t need to memorize hundreds of commands.


The Verbs

The verbs you’ll use 90% of the time:

VerbWhat it doesExample
getList one or more resourceskubectl get pods
describeDetailed info + events for a resourcekubectl describe pod my-pod
createCreate a resource from a filekubectl create -f pod.yaml
applyCreate or update a resource from a filekubectl apply -f deployment.yaml
deleteDelete a resourcekubectl delete pod my-pod
editOpen a resource in your editor livekubectl edit deployment my-app
logsStream container logskubectl logs my-pod
execRun a command inside a containerkubectl exec -it my-pod -- bash
port-forwardForward a local port to a podkubectl port-forward pod/my-pod 8080:80
scaleChange replica countkubectl scale deployment my-app --replicas=5
rolloutManage deployment rolloutskubectl rollout status deployment/my-app

The Resources

Resources are what K8s manages. You refer to them by their kind name (or shorthand):

Full NameShorthandWhat it is
podspoThe atomic unit
deploymentsdeployManages ReplicaSets
servicessvcNetwork endpoint
namespacesnsIsolation boundary
nodesnoCluster machines
configmapscmConfiguration data
secretsSensitive data
persistentvolumeclaimspvcStorage requests
ingressesingHTTP routing
# These are all equivalent
kubectl get deployments
kubectl get deployment
kubectl get deploy
# See ALL resource types in your cluster
kubectl api-resources

Essential Flags

FlagShortWhat it does
--namespace-nTarget a specific namespace
--all-namespaces-ATarget all namespaces
--output-oChange output format (yaml, json, wide)
--filename-fSpecify a file
--watch-wStream changes live
--selector-lFilter by label
--dry-run=clientPreview changes without applying
--forceForce delete (skip graceful termination)

Getting Help — Built In

You never need to Google basic syntax. kubectl has it all:

# Get help on any verb
kubectl get --help

# Get help on any resource type
kubectl explain pod
kubectl explain pod.spec
kubectl explain pod.spec.containers
kubectl explain pod.spec.containers.resources.limits

kubectl explain is like having the K8s API reference built into your terminal. Use it constantly.

Expected output for kubectl explain pod.spec.containers.resources.limits:

KIND:     Pod
VERSION:  v1

FIELD:    limits <map[string]Quantity>

DESCRIPTION:
     Limits describes the maximum amount of compute resources allowed. More info:
     https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/

Try It

# Confirm your cluster is running
kubectl get nodes

# List all pods across all namespaces
kubectl get pods -A

# Get help for the 'exec' verb
kubectl exec --help

Key Takeaways

#ConceptOne-liner
1kubectl [verb] [resource] [name]The universal pattern for all commands
2Shorthand aliasespo, deploy, svc, cm, ns save keystrokes
3kubectl explainBuilt-in API reference — use it instead of Googling
4-n / -A flagsScope commands to a namespace or all namespaces

✅ Quick Check

Q1: What does kubectl get deploy -A do?

Answer Lists all Deployments across every namespace in the cluster. `-A` is short for `--all-namespaces`, and `deploy` is the shorthand for `deployments`.

Q2: You want to see the detailed spec of a ConfigMap named app-config in the staging namespace. Which command?

Answer `kubectl describe configmap app-config -n staging` (or `kubectl describe cm app-config -n staging`). For the raw YAML: `kubectl get cm app-config -n staging -o yaml`.

Q3: You run kubectl delete pod my-pod but the pod comes back immediately. Why?

Answer The pod is managed by a controller — most likely a Deployment or ReplicaSet. Deleting the pod triggers the controller to immediately create a replacement to maintain the desired count. To permanently remove it, delete the Deployment: `kubectl delete deployment my-app`.