Kubectl Cheat Sheet - Anji Keesari
Kubectl Cheat Sheet - Anji Keesari
Kubectl Commands
Introduction
In this article, I am going to present a comprehensive cheat sheet of commonly used Kubectl
commands with examples.
Kubectl is the command line configuration tool for Kubernetes that communicates with a
Kubernetes API server. Using Kubectl allows you to create, inspect, update, and delete Kubernetes
objects.
Installing Kubectl
Use the following commands to install kubectl on Linux, macOS, and Windows.
# Linux
sudo apt-get update
sudo apt-get install -y kubectl
https://anjikeesari.com/developertools/cheatsheets/kubectl-cheat-sheet/#help-summary 1/16
1/6/25, 5:37 PM Kubectl Cheat Sheet - Anji Keesari
az account list
# or
az account list --output table
# Connect to Cluster
Cluster Information
This command provides an overview of the cluster information, including the cluster endpoint,
certificate authority, and other relevant details.
kubectl cluster-info
Version
Retrieves the Kubernetes version information for the client, server, and other components.
kubectl version
kubectl version --short
kubectl version --short --client
kubectl --help
This command provides a comprehensive overview of the kubectl command-line tool's usage,
available commands, and options. It displays a detailed help message that guides you through the
various functionalities and usage patterns of kubectl
https://anjikeesari.com/developertools/cheatsheets/kubectl-cheat-sheet/#help-summary 2/16
1/6/25, 5:37 PM Kubectl Cheat Sheet - Anji Keesari
kubectl --help
kubectl logs --help
kubectl exec --help
kubectl describe --help
kubectl port-forward --help
# set Alias
New-Alias -Name 'k' -Value 'kubectl'
# Verify
k get pods -n sample
alias k=kubectl
Configuration file
Kubectl uses a configuration file, usually located at ~/.kube/config by default, to store cluster
information, authentication details, and other settings. The configuration file is written in YAML
format and can contain multiple contexts, each representing a different cluster.
https://anjikeesari.com/developertools/cheatsheets/kubectl-cheat-sheet/#help-summary 3/16
1/6/25, 5:37 PM Kubectl Cheat Sheet - Anji Keesari
Listing resources
Use the kubectl get command followed by the resource type you want to list.
https://anjikeesari.com/developertools/cheatsheets/kubectl-cheat-sheet/#help-summary 4/16
1/6/25, 5:37 PM Kubectl Cheat Sheet - Anji Keesari
Creating a Resource
Create a resource such as a service, deployment, job, or namespace using the kubectl create
command.
# Create a Deployment
kubectl create deployment nginx-deployment --image=nginx -n sample
# Create a Service
kubectl create service clusterip my-service --tcp=80:8080
# Create a PersistentVolume
kubectl create persistentvolume my-pv --capacity=1Gi --host-path=/data
# Create a PersistentVolumeClaim
kubectl create persistentvolumeclaim my-pvc --namespace=my-namespace
--storageClassName=standard --request=1Gi
https://anjikeesari.com/developertools/cheatsheets/kubectl-cheat-sheet/#help-summary 5/16
1/6/25, 5:37 PM Kubectl Cheat Sheet - Anji Keesari
Creating vs Apply
kubectl create is used for creating new resources, while kubectl apply is used for creating
and updating resources. kubectl apply provides a more flexible and incremental approach to
managing resource configurations.
# Get all worker nodes (use a selector to exclude results that have a label
# named 'node-role.kubernetes.io/control-plane')
kubectl get node --selector='!node-role.kubernetes.io/control-plane'
Updating resources
# Rolling update "www" containers of "frontend" deployment, updating the image
kubectl set image deployment/frontend www=image:v2
# Check the history of deployments including the revision
kubectl rollout history deployment/frontend
# Rollback to the previous deployment
kubectl rollout undo deployment/frontend
# Rollback to a specific revision
https://anjikeesari.com/developertools/cheatsheets/kubectl-cheat-sheet/#help-summary 6/16
1/6/25, 5:37 PM Kubectl Cheat Sheet - Anji Keesari
Patching resources
# Update a container's image; spec.containers[*].name is required because it's a
merge key
kubectl patch pod my-pod -n my-namespace -p '{"spec":{"containers":
[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'
Editing resources
Scaling resources
# Scale a replicaset named 'my-rs' to 3
kubectl scale --replicas=2 rs/my-rs -n my-namespace
https://anjikeesari.com/developertools/cheatsheets/kubectl-cheat-sheet/#help-summary 7/16
1/6/25, 5:37 PM Kubectl Cheat Sheet - Anji Keesari
Deleting resources
# Delete pods and services with same names "aspnet-api" and "aspnet-api"
kubectl delete pod,service aspnet-api aspnet-api -n sample
# Delete pods and services with same names "baz" and "foo"
kubectl delete pod,service my-pod1, my-pod2
kubectl logs
kubectl logs pod/my-pod -n my-namespace
kubectl logs svc/my-svc -n my-namespace
Port-forward
# listen on local port 5000 and forward to port 5000 on Service backend
kubectl port-forward svc/my-service 5000
https://anjikeesari.com/developertools/cheatsheets/kubectl-cheat-sheet/#help-summary 8/16
1/6/25, 5:37 PM Kubectl Cheat Sheet - Anji Keesari
# listen on local port 5000 and forward to Service target port with name <my-
service-port>
kubectl port-forward svc/my-service 5000:my-service-port
# list files
# root@my-pod:/app# ls
# root@my-pod:/app# exit
# describe pod
kubectl describe pod/nginx-deployment-9456bbbf9-ngv7f -n sample
Show metrics
# Show node metrics:
kubectl top nodes
Formatting output
You can format the output of commands to customize the information displayed.
# all pods
kubectl get pods -n sample -o=yaml
kubectl get pods -n sample -o=json
kubectl get pods -n sample -o=name
https://anjikeesari.com/developertools/cheatsheets/kubectl-cheat-sheet/#help-summary 9/16
1/6/25, 5:37 PM Kubectl Cheat Sheet - Anji Keesari
# single pod
kubectl get pod/aspnet-api-6699db6d4b-66d7m -n sample -o=yaml
# This will get all container with the namespace in a pretty format:
kubectl get pods --all-namespaces -o=custom-
columns=NameSpace:.metadata.namespace,NAME:.metadata.name,CONTAINERS:.spec.contai
Command invoke
Use command invoke to access a private Azure Kubernetes Service (AKS) cluster, reference -
https://learn.microsoft.com/en-us/azure/aks/command-invoke
https://anjikeesari.com/developertools/cheatsheets/kubectl-cheat-sheet/#help-summary 10/16
1/6/25, 5:37 PM Kubectl Cheat Sheet - Anji Keesari
kubectl api-resources
The kubectl api-resources command allows you to view the available resource types in your
Kubernetes cluster. It provides a list of the supported resource types along with their short names,
API group, and whether they are namespaced or not.
https://anjikeesari.com/developertools/cheatsheets/kubectl-cheat-sheet/#help-summary 11/16
1/6/25, 5:37 PM Kubectl Cheat Sheet - Anji Keesari
resourcequotas quota v1
true ResourceQuota
secrets v1
true Secret
serviceaccounts sa v1
true ServiceAccount
services svc v1
true Service
azureassignedidentities
aadpodidentity.k8s.io/v1 true AzureAssignedIdentity
azureidentities
aadpodidentity.k8s.io/v1 true AzureIdentity
azureidentitybindings
aadpodidentity.k8s.io/v1 true AzureIdentityBinding
azurepodidentityexceptions
aadpodidentity.k8s.io/v1 true AzurePodIdentityException
challenges acme.cert-
manager.io/v1 true Challenge
orders acme.cert-
manager.io/v1 true Order
mutatingwebhookconfigurations
admissionregistration.k8s.io/v1 false MutatingWebhookConfiguration
validatingwebhookconfigurations
admissionregistration.k8s.io/v1 false
ValidatingWebhookConfiguration
customresourcedefinitions crd,crds
apiextensions.k8s.io/v1 false CustomResourceDefinition
apiservices
apiregistration.k8s.io/v1 false APIService
controllerrevisions apps/v1
true ControllerRevision
daemonsets ds apps/v1
true DaemonSet
deployments deploy apps/v1
true Deployment
replicasets rs apps/v1
true ReplicaSet
statefulsets sts apps/v1
true StatefulSet
tokenreviews
authentication.k8s.io/v1 false TokenReview
localsubjectaccessreviews
authorization.k8s.io/v1 true LocalSubjectAccessReview
selfsubjectaccessreviews
authorization.k8s.io/v1 false SelfSubjectAccessReview
selfsubjectrulesreviews
authorization.k8s.io/v1 false SelfSubjectRulesReview
subjectaccessreviews
authorization.k8s.io/v1 false SubjectAccessReview
horizontalpodautoscalers hpa autoscaling/v2
true HorizontalPodAutoscaler
cronjobs cj batch/v1
true CronJob
https://anjikeesari.com/developertools/cheatsheets/kubectl-cheat-sheet/#help-summary 12/16
1/6/25, 5:37 PM Kubectl Cheat Sheet - Anji Keesari
jobs batch/v1
true Job
certificaterequests cr,crs cert-manager.io/v1
true CertificateRequest
certificates cert,certs cert-manager.io/v1
true Certificate
clusterissuers cert-manager.io/v1
false ClusterIssuer
issuers cert-manager.io/v1
true Issuer
certificatesigningrequests csr
certificates.k8s.io/v1 false CertificateSigningRequest
configs config
config.gatekeeper.sh/v1alpha1 true Config
k8sazurev1blockdefault
constraints.gatekeeper.sh/v1beta1 false K8sAzureV1BlockDefault
k8sazurev1ingresshttpsonly
constraints.gatekeeper.sh/v1beta1 false K8sAzureV1IngressHttpsOnly
k8sazurev1serviceallowedports
constraints.gatekeeper.sh/v1beta1 false K8sAzureV1ServiceAllowedPorts
k8sazurev2blockautomounttoken
constraints.gatekeeper.sh/v1beta1 false K8sAzureV2BlockAutomountToken
k8sazurev2blockhostnamespace
constraints.gatekeeper.sh/v1beta1 false K8sAzureV2BlockHostNamespace
k8sazurev2containerallowedimages
constraints.gatekeeper.sh/v1beta1 false
K8sAzureV2ContainerAllowedImages
k8sazurev2noprivilege
constraints.gatekeeper.sh/v1beta1 false K8sAzureV2NoPrivilege
k8sazurev3allowedcapabilities
constraints.gatekeeper.sh/v1beta1 false K8sAzureV3AllowedCapabilities
k8sazurev3allowedusersgroups
constraints.gatekeeper.sh/v1beta1 false K8sAzureV3AllowedUsersGroups
k8sazurev3containerlimits
constraints.gatekeeper.sh/v1beta1 false K8sAzureV3ContainerLimits
k8sazurev3disallowedcapabilities
constraints.gatekeeper.sh/v1beta1 false
K8sAzureV3DisallowedCapabilities
k8sazurev3enforceapparmor
constraints.gatekeeper.sh/v1beta1 false K8sAzureV3EnforceAppArmor
k8sazurev3hostfilesystem
constraints.gatekeeper.sh/v1beta1 false K8sAzureV3HostFilesystem
k8sazurev3hostnetworkingports
constraints.gatekeeper.sh/v1beta1 false K8sAzureV3HostNetworkingPorts
k8sazurev3noprivilegeescalation
constraints.gatekeeper.sh/v1beta1 false
K8sAzureV3NoPrivilegeEscalation
k8sazurev3readonlyrootfilesystem
constraints.gatekeeper.sh/v1beta1 false
K8sAzureV3ReadOnlyRootFilesystem
leases
coordination.k8s.io/v1 true Lease
https://anjikeesari.com/developertools/cheatsheets/kubectl-cheat-sheet/#help-summary 13/16
1/6/25, 5:37 PM Kubectl Cheat Sheet - Anji Keesari
endpointslices discovery.k8s.io/v1
true EndpointSlice
events ev events.k8s.io/v1
true Event
flowschemas
flowcontrol.apiserver.k8s.io/v1beta2 false FlowSchema
prioritylevelconfigurations
flowcontrol.apiserver.k8s.io/v1beta2 false PriorityLevelConfiguration
clustertriggerauthentications cta,clustertriggerauth keda.sh/v1alpha1
false ClusterTriggerAuthentication
scaledjobs sj keda.sh/v1alpha1
true ScaledJob
scaledobjects so keda.sh/v1alpha1
true ScaledObject
triggerauthentications ta,triggerauth keda.sh/v1alpha1
true TriggerAuthentication
nodes
metrics.k8s.io/v1beta1 false NodeMetrics
pods
metrics.k8s.io/v1beta1 true PodMetrics
ingressclasses networking.k8s.io/v1
false IngressClass
ingresses ing networking.k8s.io/v1
true Ingress
networkpolicies netpol networking.k8s.io/v1
true NetworkPolicy
runtimeclasses node.k8s.io/v1
false RuntimeClass
poddisruptionbudgets pdb policy/v1
true PodDisruptionBudget
clusterrolebindings
rbac.authorization.k8s.io/v1 false ClusterRoleBinding
clusterroles
rbac.authorization.k8s.io/v1 false ClusterRole
rolebindings
rbac.authorization.k8s.io/v1 true RoleBinding
roles
rbac.authorization.k8s.io/v1 true Role
priorityclasses pc scheduling.k8s.io/v1
false PriorityClass
secretproviderclasses secrets-store.csi.x-
k8s.io/v1 true SecretProviderClass
secretproviderclasspodstatuses secrets-store.csi.x-
k8s.io/v1 true SecretProviderClassPodStatus
volumesnapshotclasses vsclass,vsclasses
snapshot.storage.k8s.io/v1 false VolumeSnapshotClass
volumesnapshotcontents vsc,vscs
snapshot.storage.k8s.io/v1 false VolumeSnapshotContent
volumesnapshots vs
snapshot.storage.k8s.io/v1 true VolumeSnapshot
constraintpodstatuses
status.gatekeeper.sh/v1beta1 true ConstraintPodStatus
https://anjikeesari.com/developertools/cheatsheets/kubectl-cheat-sheet/#help-summary 14/16
1/6/25, 5:37 PM Kubectl Cheat Sheet - Anji Keesari
constrainttemplatepodstatuses
status.gatekeeper.sh/v1beta1 true ConstraintTemplatePodStatus
csidrivers storage.k8s.io/v1
false CSIDriver
csinodes storage.k8s.io/v1
false CSINode
csistoragecapacities storage.k8s.io/v1
true CSIStorageCapacity
storageclasses sc storage.k8s.io/v1
false StorageClass
volumeattachments storage.k8s.io/v1
false VolumeAttachment
constrainttemplates constraints
templates.gatekeeper.sh/v1 false ConstraintTemplate
Help summary
Basic Commands (Beginner):
create Create a resource from a file or from stdin
expose Take a replication controller, service, deployment or pod and
expose it as a new Kubernetes service
run Run a particular image on the cluster
set Set specific features on objects
Deploy Commands:
rollout Manage the rollout of a resource
scale Set a new size for a deployment, replica set, or replication
controller
autoscale Auto-scale a deployment, replica set, stateful set, or
replication controller
https://anjikeesari.com/developertools/cheatsheets/kubectl-cheat-sheet/#help-summary 15/16
1/6/25, 5:37 PM Kubectl Cheat Sheet - Anji Keesari
Advanced Commands:
diff Diff the live version against a would-be applied version
apply Apply a configuration to a resource by file name or stdin
patch Update fields of a resource
replace Replace a resource by file name or stdin
wait Experimental: Wait for a specific condition on one or many
resources
kustomize Build a kustomization target from a directory or URL.
Settings Commands:
label Update the labels on a resource
annotate Update the annotations on a resource
completion Output shell completion code for the specified shell (bash,
zsh, fish, or powershell)
Other Commands:
alpha Commands for features in alpha
api-resources Print the supported API resources on the server
api-versions Print the supported API versions on the server, in the form of
"group/version"
config Modify kubeconfig files
plugin Provides utilities for interacting with plugins
version Print the client and server version information
References
https://kubernetes.io/docs/reference/kubectl/cheatsheet/
https://anjikeesari.com/developertools/cheatsheets/kubectl-cheat-sheet/#help-summary 16/16