惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Jina AI
Jina AI
博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
美团技术团队
腾讯CDC
博客园 - Franky
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
aimingoo的专栏
aimingoo的专栏
博客园_首页
V
V2EX
Martin Fowler
Martin Fowler
T
The Blog of Author Tim Ferriss

Learn Cloud Native

Plan-based validation for coding agents on Kubernetes | Learn Cloud Native PR validation at scale and a sandbox for every pull request on Kubernetes | Learn Cloud Native 7 practical MCP policies with agentgateway | Learn Cloud Native Agentgateway rate limiting for agents | Learn Cloud Native Local development with coding agents on Kubernetes using Signadot | Learn Cloud Native cuenv: one typed file for your whole project | Learn Cloud Native Preflight: AI Code Review Before You Push Anatomy of AI Agents Accessing Google Drive from Next.js Deploying to Fly.io using Dagger and Github Top Cloud-Native & Kubernetes Certifications [2026 Guide] Rapid microservices development with Signadot How to prepare for Istio certified associate exam (ICA) Global Rate Limiting in Istio with Envoy Rate Limit Service My Journey with Istio: From Incubation to Graduation Cilium Network Policy Tutorial: Secure Kubernetes Step by Step Kubernetes Networking: How kube-proxy and iptables Work Istio ServiceEntry: DNS vs. STATIC Resolution & Endpoints Explained Apply an Istio DestinationRule Globally (Mesh-Wide) Istio Rate Limiting: Configure a Local Rate Limiter in Envoy How to expose custom ports on Istio ingress gateway Portainer Tutorial: A Web UI for Kubernetes & Containers Traefik Proxy 2.x and TLS 101 Kubernetes CLI (kubectl) tips you didn't know about Setting up SSL certificates with Istio Gateway ArgoCD Best Practices You Should Know 在 OCI Ampere A1 计算实例上运行 AI Running AI On OCI Ampere A1 Instance How to Deploy Traefik Proxy Using Flux and GitOps Principles Firebase Emulators with Next.js: Local Setup Guide
Master the Kubernetes CLI (kubectl) - Cheatsheet
Peter Jausovec · 2019-10-30 · via Learn Cloud Native

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:

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

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

Note

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

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