This cheat sheet provides a quick reference to common kubectl
commands for managing your Kubernetes (K8s) clusters and workloads. Each command is organized by category, making it easier to find what you need.
Common Options
Option Command Example Description alias
alias k=kubectl
Create an alias for kubectl
-o=json
kubectl get pods -o=json
Output in JSON format -o=yaml
kubectl get pods -o=yaml
Output in YAML format -o=wide
kubectl get pods -o=wide
Output in wide format (includes node names for Pods) -n
kubectl get pods -n=<namespace_name>
Specify namespace -f
kubectl create -f ./<file name>
Use file, directory, or URL to create resources -l
kubectl logs -l name=<label_name>
Filter resources using label selector -h
kubectl -h
Show help for kubectl
Configuration Files (Manifest Files)
Command Description kubectl apply -f <file>
Apply configuration from a file kubectl create -f <file>
Create resources from a configuration file kubectl create -f <directory>
Create resources from all files in a directory kubectl delete -f <file>
Delete resources defined in a configuration file
Cluster Management & Context
Command Description kubectl cluster-info
Display endpoint information about the cluster master and services kubectl version
Display Kubernetes client and server version kubectl config view
Display the cluster configuration kubectl config current-context
Display the current context kubectl config get-contexts
List all available contexts kubectl config use-context <context_name>
Set the default context kubectl api-resources
List available API resources kubectl api-versions
List available API versions kubectl get all -A
List all resources across all namespaces
DaemonSets
Command Description kubectl get daemonset
List all DaemonSets kubectl edit daemonset <daemonset_name>
Edit the definition of a DaemonSet kubectl delete daemonset <daemonset_name>
Delete a DaemonSet kubectl create daemonset <daemonset_name>
Create a new DaemonSet kubectl rollout daemonset
Manage DaemonSet rollout kubectl describe ds <daemonset_name> -n <namespace>
View the details of a DaemonSet in a namespace
Deployments
Command Description kubectl get deployment
List all Deployments kubectl describe deployment <deployment_name>
Show detailed information about a Deployment kubectl edit deployment <deployment_name>
Edit a Deployment kubectl create deployment <deployment_name>
Create a new Deployment kubectl delete deployment <deployment_name>
Delete a Deployment kubectl rollout status deployment <deployment_name>
Show the rollout status of a Deployment kubectl set image deployment/<deployment_name> <container_name>=<new_image_version>
Update container image kubectl rollout undo deployment/<deployment_name>
Rollback to a previous Deployment
Events
Command Description kubectl get events
List events for all resources kubectl get events --field-selector type=Warning
List events filtered by warning type kubectl get events --sort-by=.metadata.creationTimestamp
Sort events by creation timestamp
Logs
Command Description kubectl logs <pod_name>
Print logs for a pod kubectl logs --since=6h <pod_name>
Show logs from the last 6 hours kubectl logs --tail=50 <pod_name>
Show the most recent 50 lines of logs kubectl logs -f <pod_name>
Follow logs for a pod kubectl logs -c <container_name> <pod_name>
Show logs from a specific container in the pod
Namespaces
Command Description kubectl create namespace <namespace_name>
Create a new namespace kubectl get namespaces
List all namespaces kubectl describe namespace <namespace_name>
View details of a specific namespace kubectl delete namespace <namespace_name>
Delete a namespace
Nodes
Command Description kubectl get node
List all nodes kubectl taint node <node_name>
Apply a taint to a node kubectl top node <node_name>
Show resource usage for a node kubectl cordon node <node_name>
Mark a node as unschedulable kubectl uncordon node <node_name>
Mark a node as schedulable kubectl drain node <node_name>
Drain a node for maintenance
Pods
Command Description kubectl get pod
List all Pods kubectl delete pod <pod_name>
Delete a Pod kubectl describe pod <pod_name>
Show detailed information about a Pod kubectl exec <pod_name> -c <container_name> <command>
Execute a command in a container within a pod kubectl port-forward <pod_name> <local_port>:<pod_port>
Forward a port from a local machine to a pod
Replication Controllers
Command Description kubectl get rc
List all Replication Controllers kubectl get rc --namespace=<namespace_name>
List Replication Controllers by namespace
ReplicaSets
Command Description kubectl get replicasets
List all ReplicaSets kubectl describe replicasets <replicaset_name>
Show detailed information about a ReplicaSet kubectl scale --replicas=[x]
Scale a ReplicaSet to a specified number of replicas
Secrets
Command Description kubectl create secret
Create a new secret kubectl get secrets
List all secrets kubectl describe secrets
Show details about a specific secret kubectl delete secret <secret_name>
Delete a secret
Services
Command Description kubectl get services
List all services kubectl describe services
Show detailed information about a service kubectl expose deployment <deployment_name>
Expose a deployment as a service kubectl edit services
Edit a service’s definition
Service Accounts
Command Description kubectl get serviceaccounts
List all service accounts kubectl describe serviceaccounts
Show details about a specific service account kubectl delete serviceaccount <service_account_name>
Delete a service account
StatefulSets
Command Description kubectl get statefulset
List all StatefulSets kubectl delete statefulset <stateful_set_name> --cascade=false
Delete StatefulSet without deleting Pods
This cheat sheet offers a concise reference for managing Kubernetes resources and clusters. Keep it handy to quickly access kubectl
commands!