A beginner-friendly guide to commonly used Kubernetes commands for managing pods, services, deployments, and other resources in a cluster. This cheatsheet includes commands for monitoring and troubleshooting, and covers key concepts such as scaling and rolling updates. Designed to help new users get started with Kubernetes and become familiar with the most essential commands in a concise and easy-to-follow format.
kubectl get pods
: list all pods in the current namespacekubectl describe pod <pod-name>
: show detailed information about a specific podkubectl logs <pod-name>
: show the logs of a specific podkubectl exec -it <pod-name> <command>
: run a command in a specific podkubectl port-forward <pod-name> <local-port>:<remote-port>
: forward a local port to a specific podkubectl cp <file-path> <pod-name>:<destination-path>
: copy a file to a specific podkubectl label <resource-type> <resource-name> <label-key>=<label-value>
: add a label to a specific resourcekubectl get pods --all-namespaces
: list pods across all namespaceskubectl get pods --watch
: list pods and stream updates to the outputkubectl get pods -l <label-key>
: list pods with a specific labelkubectl run <deployment-name> --image=<image-name> --port=<port-number>
: create and run a new deployment using the specified image and portkubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}'
: list the names of all pods in jsonpath formatkubectl get pods -o jsonpath='{.items[*].status.phase}'
: list the status of all pods in jsonpath formatkubectl get pods -o jsonpath='{.items[*].spec.containers[*].name}'
: list the container names of all pods in jsonpath formatkubectl get pods -o jsonpath='{.items[*].spec.containers[*].image}'
: list the container images of all pods in jsonpath formatkubectl get pods --sort-by='.status.containerStatuses[0].restartCount'
: list all pods sorted by the number of times their main container has been restartedkubectl get pods --sort-by='.status.startTime'
: list all pods sorted by start timekubectl get pods --sort-by='.status.phase'
: list all pods sorted by status phasekubectl get pods -o jsonpath='{.items[*].metadata.labels}'
: list all the labels of pods in jsonpath formatkubectl get pods -o jsonpath='{.items[*].metadata.annotations}'
: list all the annotations of pods in jsonpath format
kubectl get services
: list all services in the current namespacekubectl describe service <service-name>
: show detailed information about a specific servicekubectl expose deployment <deployment-name> --type=<service-type> --port=<port-number> --name=<service-name>
: create a new service for a deploymentkubectl get services --all-namespaces
: list services across all namespaceskubectl describe endpoints <endpoints-name>
: show detailed information about a specific endpointskubectl get services -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}'
: list the names of all services in jsonpath formatkubectl get services -o jsonpath='{.items[*].spec.ports[*].name}'
: list the ports name of all services in jsonpath foatkubectl get services -o jsonpath='{.items[*].spec.ports[*].port}'
: list the ports number of all services in jsonpath format
kubectl get deployments
: list all deployments in the current namespacekubectl describe deployment <deployment-name>
: show detailed information about a specific deploymentkubectl scale deployment <deployment-name> --replicas=<number>
: scale the number of replicas in a deploymentkubectl rolling-update <deployment-name> --image=<new-image-name>
: perform a rolling update to a deployment, replacing the current image with the specified new imagekubectl set image deployment <deployment-name> <container-name>=<new-image-name>
: update the image of a specific coainer in a deploymentkubectl create -f <yaml-file> --record
: create resources from a YAML file and create a rollback revisionkubectl rollout undo deployment <deployment-name>
: undo the last rollout of a deploymentkubectl set resources deployment <deployment-name> -c <container-name> --limits=cpu=200m,memory=512Mi
: set resource limits for a specific container in a deploymentkubectl get deployments --all-namespaces
: list deployments across all namespaceskubectl get deployments --sort-by=.metadata.name
: list all deployments sorted by name
kubectl get nodes
: list all nodes in the clusterkubectl describe node <node-name>
: show detailed information about a specific nodekubectl cordon <node-name>
: mark a node as unschedulablekubectl uncordon <node-name>
: mark a node as schedulablekubectl drain <node-name> --force --ignore-daemonsets
: Drain a node in the cluster, which will evicts the pods running on that node, and marks it unschedulable.kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}'
: list the names of all nodes in jsonpath formatkubectl get nodes -o jsonpath='{.items[*].status.conditions[*].type}'
: list the conditions of all nodes in jsonpath format
kubectl get namespace
: list all namespaceskubectl create namespace <namespace-name>
: create a new namespacekubectl config set-context --current --namespace=<namespace-name>
: set the current namespace for kubectlkubectl config view
: show the current kubectl configurationkubectl get pods --selector=<label-key>=<label-value>
: list all pods with a specific label-value
kubectl version
: show the version of kubectl and the connected Kubernetes clusterkubectl help
: show the list of available kubectl commands and their usagekubectl api-resources
: list all the resources that are available through Kubernetes APIkubectl config view
: show the current kubectl configurationkubectl config set-cluster <cluster-name> --server=<cluster-url>
: set the cluster for kubectl to usekubectl get events
: list all events in the current namespacekubectl get events --sort-by=.lastTimestamp
: list all events sorted by timestampkubectl get events --field-selector=involvedObject.kind=Pod
: list all events related to podskubectl get events -w
: list all events and stream updates to the outputkubectl get events --all-namespaces
: list all events across all namespaces