Kubernetes Cheatsheet

A comprehensive reference for Kubernetes (kubectl) commands, options, and examples.

Version: 1.28.0

Basic Commands

Essential commands for working with Kubernetes resources

kubectl get
Display one or many resources.

Common Options:

-o, --output - Output format (json|yaml|wide|name)
-A, --all-namespaces - List resources across all namespaces
-w, --watch - Watch for changes after listing/getting the requested objects

Examples:

kubectl get pods
kubectl get pods -o wide
kubectl get pods -A
kubectl get deployment,svc
kubectl describe
Show detailed information about a specific resource.

Common Options:

-n, --namespace - The namespace scope for this CLI request

Examples:

kubectl describe pod my-pod
kubectl describe node my-node
kubectl describe deployment my-deployment -n my-namespace
kubectl create
Create a resource from a file or from stdin.

Common Options:

-f, --filename - File containing the configuration to create
--dry-run - Only print the object that would be sent, without sending it

Examples:

kubectl create -f deployment.yaml
kubectl create namespace my-namespace
kubectl create deployment nginx --image=nginx
kubectl apply
Apply a configuration to a resource by file or stdin.

Common Options:

-f, --filename - File containing the configuration to apply
--record - Record current kubectl command in the resource annotation

Examples:

kubectl apply -f deployment.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/examples/master/guestbook/frontend-deployment.yaml
kubectl apply -f ./dir

Pod Management

Commands for managing Kubernetes pods

kubectl run
Create and run a particular image in a pod.

Common Options:

--image - The image for the container to run
-i, --stdin - Keep stdin open on the container(s) in the pod
-t, --tty - Allocate a TTY for the container(s) in the pod

Examples:

kubectl run nginx --image=nginx
kubectl run -it busybox --image=busybox -- sh
kubectl run nginx --image=nginx --port=80 --expose
kubectl exec
Execute a command in a container.

Common Options:

-i, --stdin - Pass stdin to the container
-t, --tty - Stdin is a TTY
-c, --container - Container name. If omitted, the first container in the pod will be chosen

Examples:

kubectl exec my-pod -- ls /
kubectl exec -it my-pod -- bash
kubectl exec -it my-pod -c my-container -- bash
kubectl logs
Print the logs for a container in a pod.

Common Options:

-f, --follow - Follow the log output
-p, --previous - Print the logs for the previous instance of the container in a pod if it exists
-c, --container - Print the logs of this container

Examples:

kubectl logs my-pod
kubectl logs -f my-pod
kubectl logs -p my-pod -c my-container
kubectl delete pod
Delete a pod.

Common Options:

--force - Force delete the pod
--grace-period - Period of time in seconds given to the pod to terminate gracefully

Examples:

kubectl delete pod my-pod
kubectl delete pod my-pod --force --grace-period=0
kubectl delete pod -l app=nginx

Deployment Management

Commands for managing Kubernetes deployments

kubectl rollout
Manage the rollout of a resource.

Common Options:

status - Show the status of the rollout
history - View rollout history
undo - Undo a previous rollout

Examples:

kubectl rollout status deployment/nginx-deployment
kubectl rollout history deployment/nginx-deployment
kubectl rollout undo deployment/nginx-deployment
kubectl rollout undo deployment/nginx-deployment --to-revision=2
kubectl scale
Set a new size for a Deployment, ReplicaSet, or Replication Controller.

Common Options:

--replicas - The new number of replicas

Examples:

kubectl scale deployment nginx-deployment --replicas=3
kubectl scale --replicas=3 -f deployment.yaml
kubectl set image
Update image of a pod template.

Examples:

kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1
kubectl set image deployment/nginx-deployment nginx=nginx:1.17.0 --record

Configuration

Commands for managing Kubernetes configuration

kubectl config
Modify kubeconfig files.

Common Options:

view - Display merged kubeconfig settings or a specified kubeconfig file
use-context - Set the current-context in a kubeconfig file
set-context - Set a context entry in kubeconfig

Examples:

kubectl config view
kubectl config current-context
kubectl config use-context my-cluster-name
kubectl config set-context --current --namespace=my-namespace
kubectl config get-contexts
Display one or many contexts from the kubeconfig file.

Examples:

kubectl config get-contexts
kubectl config get-contexts my-context