Skip to content

Kubernetes Cheat Sheet

Kubernetes, often abbreviated as K8s (where 8 represents the eight characters between ‘K’ and ’s’ in ‘ubernete’), is an open-source system for managing containerized applications across multiple hosts in a cloud platform. The goal of Kubernetes is to make deploying containerized applications simple and efficient (powerful). Kubernetes provides a mechanism for application deployment, scheduling, updating, and maintenance.

View Resource Information

Nodes

Resource name: nodes, Abbreviation: no

$ kubectl get no          # List all nodes
$ kubectl get no -o wide  # List all nodes with more details
$ kubectl describe no     # Describe nodes in detail
$ kubectl get no -o yaml  # Describe nodes in YAML format
$ kubectl get node --selector=[label_name] # Filter nodes by label
$ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
# Output specific fields defined by jsonpath expression
$ kubectl top node [node_name] # Show node usage (CPU/Memory/Storage)

Pods

Resource name: pods, Abbreviation: po

$ kubectl get po 				# List all pods
$ kubectl get po -o wide
$ kubectl describe po
$ kubectl get po --show-labels 	# List pod labels
$ kubectl get po -l app=nginx
$ kubectl get po -o yaml
$ kubectl get pod [pod_name] -o yaml --export
$ kubectl get pod [pod_name] -o yaml --export > nameoffile.yaml 
# Export pod info to a YAML file
$ kubectl get pods --field-selector status.phase=Running 		
# Filter pods using a field selector

Namespaces

Resource name: namespaces, Abbreviation: ns

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

Deployments

Resource name: deployments, Abbreviation: deploy

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

Services

Resource name: services, Abbreviation: svc

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

DaemonSets

Resource name: daemonsets, Abbreviation: ds

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

Events

Resource name: events, Abbreviation: ev

$ 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

Resource name: serviceaccounts, Abbreviation: sa

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

ReplicaSets

Resource name: replicasets, Abbreviation: rs

$ 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

Resource name: configmaps, Abbreviation: cm

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

Ingresses

Resource name: ingresses, Abbreviation: ing

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

Persistent Volumes

Resource name: persistentvolumes, Abbreviation: pv

$ kubectl get pv 
$ kubectl describe pv

Persistent Volume Claims

Resource name: persistentvolumeclaims, Abbreviation: pvc

$ kubectl get pvc
$ kubectl describe pvc

Storage Classes

Resource name: storageclasses, Abbreviation: sc

$ kubectl get sc
$ kubectl get sc -o yaml

Multiple Resources

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

Changing Resource Attributes

Taints

$ kubectl taint [node_name] [taint_name]

Labels

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

Cordon / Uncordon

$ kubectl cordon [node_name]   # Mark node as unschedulable
$ kubectl uncordon [node_name] # Mark node as schedulable

Drain Node

$ kubectl drain [node_name]    # Drain the node

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 [deploy_name] --port=80 --type=NodePort
$ kubectl scale deploy [deploy_name] --replicas=5
$ kubectl delete ns [ns_name]
$ 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]

Service Accounts

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

Annotations

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

Adding Resources

Create Pods

$ kubectl create -f [name_of_file] 
$ kubectl apply -f [name_of_file]
$ kubectl run [pod_name] --image=nginx --restart=Never

Create Services

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

Create Deployments

$ kubectl create -f [name_of_file] 
$ kubectl apply -f [name_of_file]
$ kubectl create deploy [deploy_name] --image=nginx

Container Interaction

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

Output YAML File

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

Get Help

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

Requests

API Calls

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

Cluster Information

$ kubectl config
$ kubectl cluster-info
$ kubectl get componentstatus