Master the Kubernetes CLI (kubectl) - Cheatsheet

October 29, 2019

This cheatsheet contains a list of most common commands you will use when working with Kubernetes cluster and its resources.

If you're working with Kubernetes on daily basis or if you're just learning about Kubernetes you will run into a set of commands that are used often than the other commands. The ones used more often are also usually easy to remember (especially if you're typing them out multiple times a day).

The problem becomes when you're either trying to do something more advanced or run a command against a resource you're aren't using very often or not too familiar with it. This is where this cheatsheet comes into play - it will help you quickly find the command you want, so you can copy & paste it, run it and move on.

How to use this kubectl Cheatsheet

This cheatsheet is organized into multiple sections that are based on the actions you are trying to perform against Kubernetes resources. Each section contains a table with the command and the explanation of what that command does.

Introduction

Welcome friends! I am glad you've decided to either learn more about kubectl or just trying to find some useful commands or ideas. First things first - since you're here, I am assuming you have a Kubernetes cluster running and have access to it and you also have kubectl installed.

The basics

The commands in this cheatsheet are all using the full name of the Kubernetes CLI - kubectl. If you'll be working with Kubernetes often, I strongly advise you to create an alias for that command - it will save you a lot of typing in the long run. If you want to take this to another level, check out this set of Kubectl aliases.

To create an alias for kubectl, you can run the following command:

alias k='kubectl'

Or just put it directly in your ~/.bash_profile file:

echo "alias k='kubectl'" >> ~/.bash_profile

In most of the example below, I am using a pod resource as an example - you can replace pod with any other Kubernetes resource (unless the commnd is specific to that one reasource only).

I am also using the full resource names such as pod, deployment etc. but you don't have to. Refer to the Resource names section for short resource names such as po, svc or deploy.

Let's get started!

Basic resource information

CommandExplanation
kubectl get pod mypodLists the pod with name mypod in the current namespace
kubectl get podLists all pods in the current namespace
kubectl get pod -n mynamespaceLists all pods in mynamespace namespace
kubectl get pod --all-namespacesLists all pods in all namespaces
kubectl get pod --all-namespaces -o wideLists all pods in all namespaces with IP and Node
kubectl get all --all-namespacesLists all resources in the cluster
kubectl describe pod mypodDescribes the resource in mor details (perfect for diagnosing issues)
kubectl get pod --show-labelsShows the labels next to all pods
kubectl explain podShows documentation for the resource (e.g. pod in our case)
kubectl get pod mypod -o yamlShows the YAML representation of the pod called mypod
kubectl get pod mypod -o jsonShows the JSON representation of the pod called mypod
kubectl cluster-infoShows information about the cluster
kubectl describeLists all resource names
kubectl get pod -wWatches the pods (useful when waiting for e.g. pods to start)

Note 1: replace the pod in above commands with any other resource (e.g. service, deployment, ...)

Note 2: use the short name for resources. For example: po for pods, svc for service, deploy for deployment, etc.

Detailed resource information

CommandExplanation
kubectl get pod --selector="app=myapp"Lists all pods with label app=myapp
kubectl get pod --selector="app=myapp" -o jsonpath='{.items[*].metadata.name}'Get the pod names that have the label app=myapp set
kubectl get pod --selector="app=myapp" -o jsonpath='{.items[*].spec.containers[*].image}'Get the image names of pods that have the label app=myapp set
kubectl get pod mypod -o jsonpath='{.items[*].status.podIP}'Get the pod IPs of the mypod
kubectl get pod mypod -o jsonpath='{.spec.containers[0].ports[0].containerPort}'Get the first container port in the pod
kubectl -v 9 get podGets the pods with maximum (9) verbosity

Sorting and filtering

CommandExplanation
kubectl get pod --sory-by=.metadata.nameLists the pods and sorts them by their names
kubectl get pod --sory-by=.metadata.creationTimestampLists the pods by creation time, oldest first
kubectl get pod -l app=myappShow pods that have the label app=myapp set

Labelling and annotating resources

CommandExplanation
kubectl label pod mypod mylabel=myvalueAdds the label mylabel=myvalue to the pod named mypod
kubectl label pod mypod mylabel-Removes the mylabel from the pod named mypod
kubectl get pod mypod -o jsonpath='{.items[*].metadata.annotations}'Get all annotations for the pod named mypod
kubectl annotate pod mypod name=valueAdd an annotation name=value to the pod named mypod
kubectl annotate pod mypod name-Removes the annotation name from the pod named mypod

Creating, editing and deleting resources

CommandExplanation
kubectl create -f ./file.yamlCreate resources defined in file file.yaml
kubectl delete -f ./file.yamlDelete resources defined in file file.yaml
kubectl delete pod mypodDelete a pod named mypod in the current namespace
kubectl run mypod --image=myimageCreates a pod (and deployment) called mypod with image myimage
kubectl run mypod --image=myimage --port=8080Creates a pod (and deployment) called mypod with image myimage and exposes port 8080

Advanced commands

CommandExplanation
kubectl run curl --image=radial/busyboxplus:curl -i --ttyRuns the radial/busyboxplus:curl image and gives you a terminal into it (useful for accessing pods/services within the cluster)
kubectl port-forward mypod 8080:CONTAINER_PORTForwards the local port 8080 to CONTAINER_PORT (replace this with actual container port number)
kubectl expose deployment mydeployment --type="NodePort" --port 8080Exposes deployment mydeployment to external traffic and makes it available on port 8080
kubectl exec -it mypod -- /bin/bashRuns the /bin/bash (terminal) inside the pod called mypod
kubectl attach mypodWatch the standard output of a container in real time
kubectl cp mypod:/some/path/file.txt .Copies the file /some/path/file.txt from mypod to the current directory

Resource names

Here's the list of all Kubernetes resource names and their shorthand equivalents (if available).

Note: you also run kubectl describe to get a list of these.

Long nameShort name
all
certificatesigningrequestscsr
clusterrolebindings
clusterroles
componentstatusescs
configmapscm
controllerrevisions
cronjobs
customresourcedefinitioncrd
daemonsetsds
deploymentsdeploy
endpointsep
eventsev
horizontalpodautoscalershpa
ingressesing, ingress
jobs
limitrangeslimits
namepsacesns
networkpoliciesnetpol
nodesno
persistentvolumeclaimspvc
persistentvolumespv
poddisruptionbudgetspdb
podpreset
podspo, pod
podsecuritypoliciespsp
podtemplates
replicasetsrs
replicationcontrollersrc
resourcequotasquota
rolebindings
roles
secrets
serviceaccountssa
servicessvc
statefulsetssts
storageclassessc

Enjoyed this post? Receive the next one in your inbox!