Stop 08 · Kubernetes & OrchestrationTopics · 11/12

Kubectl Essentials

Part of the DevOps Roadmap

In short

Almost everything you do with Kubernetes goes through kubectl. Learn about ten commands well and you can deploy, inspect, debug and roll back without opening a single dashboard.

The command-line tool for interacting with a Kubernetes cluster — get, describe, apply, logs, and exec cover the vast majority of day-to-day operations and troubleshooting.

The mascot points a chunky remote control at a shelf of small boxes that light up in turn.

Think of it like this

The TV remote

You don't open the TV to change the channel. One remote switches channels, raises volume and shows what's playing. kubectl is the remote for your cluster: get, describe, logs and apply are the buttons you'll press a hundred times a day.

Watch it happen

The everyday kubectl loop

get
describe
logs
exec
apply

1/5get gives the quick picture: which pods exist, their status and restart counts.

The big ideas

What you need to know

Declarative wins

Prefer kubectl apply -f with files in Git over one-off create or edit commands nobody can repeat.

Mind the namespace

Most 'not found' errors are the wrong namespace. Use -n, or set a default for your context.

Output formats

-o wide adds node and IP. -o yaml shows the full object. -l app=web filters by label.

Rollouts are built in

kubectl rollout status, history and undo let you watch and reverse a Deployment change.

See it in code

Walk through the code

bash
1kubectl get pods -n shop -o wide         # what's running, where2kubectl describe pod api-7d9f -n shop    # events, restarts, why3kubectl logs api-7d9f -n shop --previous # logs from the crashed run4kubectl exec -it api-7d9f -n shop -- sh  # shell inside the container5kubectl apply -f k8s/                    # create or update from files6kubectl rollout status deploy/api -n shop7kubectl rollout undo deploy/api -n shop  # back to the last version8kubectl config set-context --current --namespace=shop

1/4Look first: list pods, then describe the suspicious one and read its Events.

Replace api-7d9f with a real pod name from kubectl get pods. Tab completion (kubectl completion bash or zsh) saves a lot of typing.

Save yourself the pain

Common mistakes

Oops

Fixing production with kubectl edit and forgetting to update the YAML in Git.

Do this instead

Change the file, commit it and apply. Otherwise the next deploy silently undoes your fix.

Oops

Running a delete command without checking which cluster you're pointed at.

Do this instead

Run kubectl config current-context first, and show the context in your shell prompt.

Your mission

Ten-command drill

~30 min

Quick check

A pod keeps restarting and kubectl logs shows nothing useful. What should you try next?

Go deeper

  1. 1Practice kubectl get, describe, and logs on resources in your local cluster.
  2. 2Use kubectl apply -f to deploy a manifest file, rather than imperative commands.
  3. 3Learn kubectl exec to get a shell inside a running container for live debugging.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge