Kubectl Cheatsheet

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

OptionCommand ExampleDescription
aliasalias k=kubectlCreate an alias for kubectl
-o=jsonkubectl get pods -o=jsonOutput in JSON format
-o=yamlkubectl get pods -o=yamlOutput in YAML format
-o=widekubectl get pods -o=wideOutput in wide format (includes node names for Pods)
-nkubectl get pods -n=<namespace_name>Specify namespace
-fkubectl create -f ./<file name>Use file, directory, or URL to create resources
-lkubectl logs -l name=<label_name>Filter resources using label selector
-hkubectl -hShow help for kubectl

Configuration Files (Manifest Files)

CommandDescription
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

CommandDescription
kubectl cluster-infoDisplay endpoint information about the cluster master and services
kubectl versionDisplay Kubernetes client and server version
kubectl config viewDisplay the cluster configuration
kubectl config current-contextDisplay the current context
kubectl config get-contextsList all available contexts
kubectl config use-context <context_name>Set the default context
kubectl api-resourcesList available API resources
kubectl api-versionsList available API versions
kubectl get all -AList all resources across all namespaces

DaemonSets

CommandDescription
kubectl get daemonsetList 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 daemonsetManage DaemonSet rollout
kubectl describe ds <daemonset_name> -n <namespace>View the details of a DaemonSet in a namespace

Deployments

CommandDescription
kubectl get deploymentList 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

CommandDescription
kubectl get eventsList events for all resources
kubectl get events --field-selector type=WarningList events filtered by warning type
kubectl get events --sort-by=.metadata.creationTimestampSort events by creation timestamp

Logs

CommandDescription
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

CommandDescription
kubectl create namespace <namespace_name>Create a new namespace
kubectl get namespacesList all namespaces
kubectl describe namespace <namespace_name>View details of a specific namespace
kubectl delete namespace <namespace_name>Delete a namespace

Nodes

CommandDescription
kubectl get nodeList 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

CommandDescription
kubectl get podList 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

CommandDescription
kubectl get rcList all Replication Controllers
kubectl get rc --namespace=<namespace_name>List Replication Controllers by namespace

ReplicaSets

CommandDescription
kubectl get replicasetsList 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

CommandDescription
kubectl create secretCreate a new secret
kubectl get secretsList all secrets
kubectl describe secretsShow details about a specific secret
kubectl delete secret <secret_name>Delete a secret

Services

CommandDescription
kubectl get servicesList all services
kubectl describe servicesShow detailed information about a service
kubectl expose deployment <deployment_name>Expose a deployment as a service
kubectl edit servicesEdit a service’s definition

Service Accounts

CommandDescription
kubectl get serviceaccountsList all service accounts
kubectl describe serviceaccountsShow details about a specific service account
kubectl delete serviceaccount <service_account_name>Delete a service account

StatefulSets

CommandDescription
kubectl get statefulsetList all StatefulSets
kubectl delete statefulset <stateful_set_name> --cascade=falseDelete 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!