Skip to main content
blog title image

4 minute read - devops

Kubernetes kubectl cheat sheet

Nov 15, 2023

A cheat sheet to remind myself the most important kubectl commands. If I do forget then I recommend the VS Code Kubernetes plugin and remember to use --help if you get stuck, start with kubectl --help .

I had to use kubernetes again after not using it for about a month and I had forgotten everything I had learned before.

So I rummaged through my previous notes and collated the commands I’ve mostly used.

To help them stick in my head I categorised them with natural language terms “Where am I?”, “what is this doing?”, etc.

Official Cheet Sheet

The official kubectl cheat sheet is much bigger than this, but I don’t tend to use all the commands.

Use A UI

Part of the reason I forget the commands is that I often just use a tool to access and navigate the environments.

I’ve been using K9s which is a terminal UI, it is mostly easy to use, although you have to remember vi style commands to type : prior to entering a command like pods or namespaces to see a list of pods or namespaces. And / to enter a search term to filter the displayed list.

Today I experimented with the VS Code plugin

Visual Studio Code Kubernetes plugin

That seemed much faster and once I got used to it, it felt a lot more natural to use:

  • select the context from the tree view
  • either select the namespace from the list or click the toolbar at the bottom and enter the details
  • now both the context and namespace are set as the defaults
  • find the pods, jobs, etc. in the Workloads branch
  • then right clicking shows options like get, describe, logs, terminal

Also the logs view allows regex matching through an easy UI which means I didn’t have to use stern to monitor the logs.

If you’re working with Kubernetes then I do recommend the VS Code plugin, with the warning that extensive use of UI tools tends to weaken your CLI muscle memory.

Cheat Sheet

My cheat sheet notes for commands are below:

Where am I?

  • kubectl config current-context
  • kubectl cluster-info
  • kubectl config view --minify

Where can I go?

  • kubectl config get-contexts
  • kubectl get namespace

Work with a different context

  • remind me what that context is again?
    • kubectl get context a-context-name
  • ok use that one
    • kubectl config use-context a-context-name
  • or
    • kubectl config set current-context a-context-name
  • ok set this namespace to be the default
    • kubectl config set-context --current --namespace=a-namespace-name

What is here?

  • kubectl get pods
  • kubectl get jobs
  • etc.

use kubectl api-resources for a list of supported resources

What is that?

  • kubectl describe pod a-pod-name
  • kubectl describe job a-job-name
  • etc.

What is it doing?

  • kubectl logs -f a-pod-name --follow
    • follow tails it, so leave it off if you want a log dump
  • kubectl top pods
    • watch the command watch kubectl top pods

How do I access that?

  • kubectl get ingress -n a-namespace

Am i allowed to do that?

  • kubectl auth can-i get pods -n a-namespace

How do I run commands there?

  • get a shell
    • kubectl exec --stdin --tty a-pod-name -- /bin/bash
    • kubectl exec -it a-pod-name -- /bin/bash
  • run a command directly
    • kubectl exec a-pod-name -- ls
      • (add --stdin --tty for nicely formatted output)

How do I delete something?

  • kubectl delete pod a-pod-name
  • kubectl delete job a-job-name
  • etc.

How do I work with jobs?

  • kubectl get jobs

  • get all job names that match a particular pattern

    • kubectl get jobs --no-headers=true | awk '/part-of-name-.*/{print $1}'
  • delete all job names (and pods) that match a particular pattern (add xargs to above)

    • kubectl get jobs --no-headers=true | awk '/part-of-name-.*/{print $1}' | xargs kubectl delete job
  • trigger a cronjob

    • kubectl create job --from=cronjob/a-cronjob-name "name-for-new-job-$(date +%s)"

Common Patterns

  • add --help to find out what a command does e.g. kubectl auth --help
  • add -n a-namespace to run a command in a namespace or --namespace a-namespace
  • add -c a-context to run a command in a namespace or --context a-context
  • add -o json to see the output as json (e.g. for piping into jq)

Supporting Tools