kubernetes cheat sheet
Share on facebook
Share on twitter
Share on linkedin

Kubernetes Kubectl Commands Cheat Sheet

Chad Crowell
Chad Crowell

If you’re like me, you may have those moments where you’re at the terminal, hands hovering over your keyboard, and … nothing. I always seem to freeze up and probably rely too much on bash history. (The up arrow is my friend.)

While learning Kubernetes, I ended up posting 14 or 15 sticky notes on my monitor to help me in those moments — but after a while, I could barely read what was on the screen. So finally, I created one small, easy-to-read piece of paper to reference when I — or you — get stuck.

But first, let’s take a moment to establish some of the basics.

What is Kubernetes?

Kubernetes is a platform for managing containerized workloads. Kubernetes orchestrates computing,
networking and storage to provide a seamless portability across infrastructure providers. Confused? Check out Introduction to Kubernetes, our Kubernetes 101 crash course in all the fundamentals you need to know.

What is Kubectl?

kubectl is the Kubernetes command-line tool. It allows us to run commands against Kubernetes clusters — deploying applications, inspecting and managing cluster resources, and viewing logs.

Kubernetes learning resources

Kubectl commands in Kubernetes

This Kubernetes Cheat Sheet is meant to get you started performing Kubectl commands in Kubernetes and provide you with all the basic commands at a quick glance. (Check out the downloadable asset below!)

Command Results Some of the commands on this cheat sheet might not return any results, but have no fear! See below for some resources you can create, then quickly turn around and run the commands in your cheat sheet to alter your resources any way you wish! Let’s start with pods.

YAML for a basic busybox pod

apiVersion: v1kind: Podmetadata:name: busyboxspec:containers:- image: busybox:1.28.4command:- sleep- "3600"name: busyboxrestartPolicy: Always

Learn more about YAML here. Create the pod with this command:

kubectl create -f busybox.yaml

Use this command to create a deployment:

kubectl run nginx --image=nginx

Use this command to create a service from the deployment above:

kubectl expose deployment nginx --port=80 --type=NodePort

Here is the YAML for a simple persistent volume using local storage from the node:

apiVersion: v1kind: PersistentVolumemetadata:name: data-pvnamespace: webspec:storageClassName: local-storagecapacity:storage: 1GiaccessModes:- ReadWriteOncehostPath:path: /mnt/data

Use the following command to create the persistent volume:

kubectl apply -f my-pv.yaml

Here is the YAML for a simple ConfigMap:

apiVersion: v1kind: ConfigMapmetadata:name: my-config-mapdata:myKey: myValueanotherKey: anotherValue

Use the following command to create the ConfigMap:

kubectl apply -f configmap.yaml

Here is the YAML for a secret:

apiVersion: v1kind: Secretmetadata:name: my-secretstringData:myKey: myPassword

Use this command to create the secret:

kubectl apply -f secret.yaml

Here is the YAML for a service account:

apiVersion: v1kind: ServiceAccountmetadata:name: acrnamespace: defaultsecrets:- name: acr

Use this command to create the service account:

kubectl apply -f serviceaccount.yaml

Download Now!

Kubernetes Cheat Sheet

This should be enough to get you started! I’ve also created this PDF for you to download and keep next to you for reference! If you enjoyed these exercises and following along with the commands, check out our Cloud Native Certified Kubernetes Administrator (CKA) course to dig deeper into Kubernetes! 

Want more cloud tech goodness? Check these out:


Master the most in-demand skills

A Cloud Guru makes it easy (and awesome) to level up your cloud career — whether you’re new to cloud or a seasoned pro. Check out ACG’s current free courses or get started now with a free trial.


Kubernetes Cheat Sheet

Viewing Resource Information

Nodes

$ kubectl get no
$ kubectl get no -o wide
$ kubectl describe no
$ kubectl get no -o yaml
$ kubectl get node –select or =[ label _name]
$ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type==”ExternalIP”)].address}’
$ kubectl top node [node_name]

Pods

$ kubectl get po
$ kubectl get po -o wide
$ kubectl describe po
$ kubectl get po –show-labels
$ kubectl get po -l app=nginx
$ kubectl get po -o yaml
$ kubect l get pod [ pod_name] -o yaml –export
$ kubect l get pod [pod_name] -o yaml –export > nameoffile.yaml
$ kubectl get pods –field-selector status.phase=Running

Namespaces

$ kubectl get ns
$ kubectl get ns -o yaml
$ kubectl describe ns

Deployments

$ kubectl get deploy
$ kubectl describe deploy
$ kubectl get deploy -o wide
$ kubectl get deploy -o yam

Services

$ kubectl get svc
$ kubectl describe svc
$ kubectl get svc -o wide
$ kubectl get svc -o yaml
$ kubectl get svc –show-labels

DaemonSets

$ kubectl get ds
$ kubectl get ds –all-namespaces
$ kubectl describe ds [daemonset _name] -n [namespace_name]
$ kubectl get ds [ds_name] -n [ns_name] -o yaml

Events

$ kubectl get events
$ kubectl get events -n kube-system
$ kubectl get events -w

Logs

$ kubectl logs [pod_name]
$ kubectl logs –since=1h [pod_name]
$ kubectl logs –tail =20 [pod_name]
$ kubectl logs -f -c [container_name] [pod_name]
$ kubectl logs [pod_name] > pod.log

Service Accounts

$ kubectl get sa
$ kubectl get sa -o yaml
$ kubectl get serviceaccounts default -o yaml > ./sa.yaml
$ kubectl replace serviceaccount default -f. /sa.yaml

ReplicaSets

$ kubectl get rs
$ kubectl describe rs
$ kubectl get rs -o wide
$ kubectl get rs -o yaml

Roles

$ kubectl get roles –all-namespaces
$ kubectl get roles –all-namespaces -o yaml

Secrets

$ kubectl get secrets
$ kubectl get secrets –all-namespaces
$ kubectl get secrets -o yaml

ConfigMaps

$ kubectl get cm
$ kubectl get cm –all-namespaces
$ kubectl get cm –all-namespaces -o yaml

Ingress

$ kubectl get ing
$ kubectl get ing –all-namespaces

PersistentVolume

$ kubectl get pv
$ kubectl describe pv

PersistentVolumeClaim

$ kubectl get pvc
$ kubectl describe pvc

StorageClass

$ kubectl get sc
$ kubectl get sc -o yaml

MultipleResources

$ kubectl get svc, po
$ kubectl get deploy, no
$ kubectl get all
$ kubectl get all –all-namespaces

Changing Resource Attributes

Taint

$ kubectl taint [node_name] [taint _name]

Labels

$ kubectl label [node_name] disktype=ssd
$ kubrectl label [pod_name] env=prod

Cordon/Uncordon

$ kubectl cordon [node_name]
$ kubectl uncordon [node_name]

Drain

$ kubectl drain [node_name]

Nodes/Pods

$ kubectl delete node [node_name]
$ kubectl delete pod [pod_name]
$ kubectl edit node [node_name]
$ kubectl edit pod [pod_name]

Deployments/Namespaces

$ kubectl edit deploy [deploy_name]
$ kubectl delete deploy [deploy_name]
$ kubectl expose deploy [depl oy_name] –port=80 –type=NodePort
$ kubectl scale deploy [deploy_name] –replicas=5
$ kubectl delete ns
$ kubectl edit ns [ns_name]

Services

$ kubectl edit svc [svc_name]
$ kubectl delete svc [svc_name]

DaemonSets

$ kubectl edit ds [ds_name] -n kube-system
$ kubectl delete ds [ds_name]

ServiceAccounts

$ kubectl edit sa [sa_name]
$ kubectl delete sa [sa_name]

Annotate

$ kubectl annotate po [pod_name] [annotation]
$ kubectl annotate no [node_name]

Adding Resources

Creating a Pod

$ kubectl create -f [name_of _file]
$ kubectl apply -f [name_of _file]
$ kubectl run [pod_name] –image=ngi nx –restart=Never
$ kubectl run [ pod_name] –generator =run-pod/v1 –image=nginx
$ kubectl run [ pod_name] –image=nginx –restart=Never

Creating a Service

$ kubectl create svc nodeport [svc_name] –tcp=8080:80

Creating a Deployment

$ kubectl create -f [name_of _file]
$ kubectl apply -f [name_of _file]
$ kubectl create deploy [deploy_name] –image=ngi nx

Interactive Pod

$ kubectl run [pod_name] –image=busybox –rm -it –restart=Never — sh

Output YAMLto aFile

$ kubectl create deploy [deploy_name] –image=ngi nx –dry-run -o yaml > deploy.yaml
$ kubectl get po [pod_name] -o yaml –export > pod. yaml

Getting Help

$ kubectl -h
$ kubectl create -h
$ kubectl run -h
$ kubectl explain deploy.spec

Requests

API Call

$ kubectl get –raw /apis/metrics.k8s.io/

Cluster Info

$ kubectl config
$ kubectl cluster -info
$ kubectl get componentstatuses

Recommended

Get more insights, news, and assorted awesomeness around all things cloud learning.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?