COMMANDS

Kubernetescheatsheet

kubectl commands for context, workloads, logs, rollouts, and access checks.

Download .txt ↓

40 commands

Know your context

Current context

View
kubectl config current-context

Available contexts

View
kubectl config get-contexts

Cluster endpoints

View
kubectl cluster-info

Switch context

Change
kubectl config use-context CONTEXT

Changes the local kubeconfig selection. Later commands target that context.

List namespaces

View
kubectl get namespaces

Permissions

Can I read pods?

View
kubectl auth can-i get pods -n NAMESPACE

Can I create deployments?

View
kubectl auth can-i create deployments -n NAMESPACE

Explain a manifest field

View
kubectl explain deployment.spec.template.spec.containers

Inspect workloads

Pods in a namespace

View
kubectl get pods -n NAMESPACE -o wide

Deployments and services

View
kubectl get deployments,services -n NAMESPACE

Describe a pod

View
kubectl describe pod POD -n NAMESPACE

Look at conditions and events when a pod cannot start.

Read a resource manifest

View
kubectl get deployment DEPLOYMENT -n NAMESPACE -o yaml

May contain sensitive configuration; avoid sharing output without review.

Recent namespace events

View
kubectl get events -n NAMESPACE --sort-by=.metadata.creationTimestamp

Event retention and aggregation depend on the cluster.

Logs & troubleshooting

Recent container logs

View
kubectl logs POD -c CONTAINER -n NAMESPACE --tail=100

Previous container instance logs

View
kubectl logs POD -c CONTAINER -n NAMESPACE --previous

Useful after a restart if the previous instance logs remain available.

Follow logs

View
kubectl logs -f POD -c CONTAINER -n NAMESPACE

Ctrl+C stops following; it does not stop the workload.

Forward a service to your machine

Connect
kubectl port-forward service/SERVICE 8080:80 -n NAMESPACE --address=127.0.0.1

Forwards localhost:8080 to service port 80. Requires access and a backing pod; stop with Ctrl+C.

Resource usage

View
kubectl top pods -n NAMESPACE

Requires a working metrics API, often provided by Metrics Server.

Changes & rollouts

Preview manifest differences

View
kubectl diff -f MANIFEST.yaml -n NAMESPACE

Exit 0 means no difference; 1 means differences; greater than 1 means an error.

Validate with server dry-run

View
kubectl apply --dry-run=server -f MANIFEST.yaml -n NAMESPACE

Sends the request for server validation without persisting the resource. Requires relevant permissions.

Apply a manifest

Change
kubectl apply -f MANIFEST.yaml -n NAMESPACE

Creates or updates resources. Inspect the target context, namespace, and diff first.

Watch a rollout

View
kubectl rollout status deployment/DEPLOYMENT -n NAMESPACE --timeout=120s

Restart a deployment

Change
kubectl rollout restart deployment/DEPLOYMENT -n NAMESPACE

Triggers a new rollout; capacity and availability depend on deployment settings.

Undo a rollout

Change
kubectl rollout undo deployment/DEPLOYMENT -n NAMESPACE

Rolls back the pod template to the prior revision when retained. It does not roll back databases or external configuration.

Delete a pod

Delete
kubectl delete pod POD -n NAMESPACE

Stops and deletes that pod. A controller may replace it; an unmanaged pod is not recreated.

Select and inspect resources

Select pods by a label

View
kubectl get pods -n NAMESPACE -l app=APP

List resource names only

View
kubectl get pods -n NAMESPACE -o name

Inspect resource requests and limits

View
kubectl describe deployment DEPLOYMENT -n NAMESPACE

Inspect service endpoints

View
kubectl get endpointslices -n NAMESPACE -l kubernetes.io/service-name=SERVICE

List resource quotas

View
kubectl get resourcequota,limitrange -n NAMESPACE

Inspect rollout revision history

View
kubectl rollout history deployment/DEPLOYMENT -n NAMESPACE

List API resource types

View
kubectl api-resources

List permitted actions in a namespace

View
kubectl auth can-i --list -n NAMESPACE

Inspect labels

View
kubectl get pods -n NAMESPACE --show-labels

Controlled workload changes

Scale a deployment

Change
kubectl scale deployment/DEPLOYMENT --replicas=2 -n NAMESPACE

Changes desired replica count; an autoscaler may subsequently change it.

Pause a deployment rollout

Change
kubectl rollout pause deployment/DEPLOYMENT -n NAMESPACE

Pauses rollout processing, not existing application traffic.

Resume a deployment rollout

Change
kubectl rollout resume deployment/DEPLOYMENT -n NAMESPACE

Queued template changes may now roll out.

Wait for pods with a label to become ready

View
kubectl wait --for=condition=Ready pod -l app=APP -n NAMESPACE --timeout=60s

Preview a namespace manifest

View
kubectl create namespace NAMESPACE --dry-run=client -o yaml

Prints YAML without creating the namespace.

Run a command in a container

Change
kubectl exec -it POD -c CONTAINER -n NAMESPACE -- sh

Starts a process in the workload; commands may change data. sh may be absent.