100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
YAML

kubectl Basics

A practical introduction to kubectl, the command-line tool used to interact with a Kubernetes cluster's API server.

Kubernetes FundamentalsBeginner9 min readJul 8, 2026
Analogies

kubectl Basics

kubectl is the official command-line tool for interacting with a Kubernetes cluster. It communicates with kube-apiserver over HTTPS to create, inspect, modify, and delete cluster resources. Nearly all day-to-day Kubernetes work -- deploying applications, debugging issues, inspecting cluster state -- happens through kubectl.

🏏

Cricket analogy: kubectl is like the team manager's radio link to the match referee (kube-apiserver): every substitution, appeal, or strategy change goes through that one channel, and nearly all day-to-day team decisions happen this way.

How kubectl Finds the Cluster

kubectl reads connection details -- the API server address, credentials, and default namespace -- from a kubeconfig file, typically located at ~/.kube/config. This file can define multiple 'contexts,' allowing you to switch between different clusters (e.g., local, staging, production) using 'kubectl config use-context'.

🏏

Cricket analogy: kubeconfig is like a player's team registration card listing which club (cluster), which league credentials, and which home ground (namespace) applies by default; switching contexts is like a player transferring between Mumbai Indians and Chennai Super Kings squads.

bash
# Check which cluster/context kubectl is currently pointed at
kubectl config current-context

# List all available contexts
kubectl config get-contexts

# Switch to a different context
kubectl config use-context my-cluster

kubectl get and describe: Inspecting Resources

'kubectl get' lists resources of a given type -- pods, deployments, services, nodes, and more. Adding '-o wide' shows extra columns, and '-o yaml' dumps the full object definition. 'kubectl describe' goes further, giving a detailed, human-readable summary including configuration, status, and recent Events -- often the fastest way to diagnose why a pod is failing to start (e.g., ImagePullBackOff or a failed scheduling attempt).

🏏

Cricket analogy: kubectl get is like checking the scoreboard for a quick summary of every match, wide view adds extra stats columns, and full yaml is the entire match report; kubectl describe is like the post-match press conference explaining exactly why a batsman was given out.

bash
# List all pods in the current namespace
kubectl get pods

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

# Get full YAML for a specific pod
kubectl get pod my-app-abc123 -o yaml

# Get a detailed status summary and recent Events
kubectl describe pod my-app-abc123

kubectl apply, logs, and delete

'kubectl apply -f <file>' declaratively creates or updates resources defined in a YAML manifest -- this is the standard way to deploy to Kubernetes, and it is idempotent: running it repeatedly with the same file converges the cluster to match the file. By contrast, 'kubectl create' is imperative and errors if the resource already exists. 'kubectl logs' retrieves stdout/stderr from a container inside a pod. 'kubectl delete' removes resources by name or by manifest file.

🏏

Cricket analogy: kubectl apply is like updating the team's official lineup sheet with the board — run it repeatedly and the squad converges to match the sheet exactly; kubectl create errors if you try registering an already-registered player, while kubectl logs pulls the commentary transcript and kubectl delete deregisters a player.

bash
# Apply a manifest to create or update resources
kubectl apply -f pod.yaml

# Stream logs from a pod's container
kubectl logs my-app-abc123

# Follow logs in real time
kubectl logs -f my-app-abc123

# Delete a resource defined in a manifest
kubectl delete -f pod.yaml

# Delete a pod by name directly
kubectl delete pod my-app-abc123

Namespaces and the -n Flag

Most kubectl commands operate within a namespace, defaulting to 'default' unless otherwise configured. Use the '-n' or '--namespace' flag to target a specific namespace, or '--all-namespaces' (or '-A') to query across all of them, e.g. 'kubectl get pods -n kube-system'. Forgetting the '-n' flag is one of the most common sources of confusion for beginners -- resources may exist and look 'missing' simply because you're querying the wrong namespace.

🏏

Cricket analogy: Most kubectl commands operate within a namespace like a domestic league defaults to your home team's division unless specified; forgetting -n is like searching for a player in the wrong league table and thinking they don't exist.

  • kubectl talks to the cluster exclusively through kube-apiserver, using settings from a kubeconfig file (~/.kube/config).
  • 'kubectl get' lists resources; '-o yaml' or '-o wide' expand the output detail.
  • 'kubectl describe' shows detailed status and recent Events -- the first stop for debugging.
  • 'kubectl apply -f <file>' is the standard declarative way to create or update resources.
  • 'kubectl logs' retrieves container logs; add '-f' to stream them live.
  • Most commands are namespace-scoped; use '-n <namespace>' or '-A' for all namespaces.

Practice what you learned

Was this page helpful?

Topics covered

#YAML#DockerKubernetesStudyNotes#DevOps#KubectlBasics#Kubectl#Finds#Cluster#Get#Kubernetes#StudyNotes#SkillVeris