kubectl Common Commands - CodeSwordsman

High-frequency commands for contexts, inspection, logs, rollouts and node maintenance, with risk notes.

kubectl 1.29+Updated 2026-10-2932 commands

Contexts & Namespaces

List all contexts; the asterisk marks the active cluster

kubectl config get-contexts

Switch context; later commands target that cluster

kubectl config use-context prod

Set the default namespace of the current context

kubectl config set-context --current --namespace=<NS>

Confirm which namespace is currently effective

kubectl config view --minify -o jsonpath='{..namespace}'

Without it commands hit default; always pass -n across environments.

List all namespaces in the cluster

kubectl get ns

Inspect Resources

List pods with node and IP columns

kubectl get pods -n <NS> -o wide

Show the main resources of a namespace at once

kubectl get all -n <NS>

Inspect events, images, mounts and probes of a pod

kubectl describe pod <POD> -n <NS>

List events by time to spot scheduling or image pull errors

kubectl get events -n <NS> --sort-by=.lastTimestamp

Dump full pod YAML for diffing or backup

kubectl get pod <POD> -n <NS> -o yaml

Logs & Containers

Follow the last 100 log lines of a container

kubectl logs -f --tail=100 <POD> -n <NS> -c <CONTAINER>

Pods with several containers require -c, otherwise it fails.

Show logs from the previous crash to debug restarts

kubectl logs <POD> -n <NS> --previous

Open a shell inside a container; slim images may lack bash

kubectl exec -it <POD> -n <NS> -- sh

Forward a cluster service port to your local machine

kubectl port-forward svc/<SVC> 8080:80 -n <NS>

Copy files between a pod and your local machine

kubectl cp <NS>/<POD>:/var/log/app.log ./app.log

Requires tar inside the container, otherwise it fails.

Deploy & Rollout

Create a deployment quickly for validation

kubectl create deployment my-app --image=nginx:1.25 --replicas=2 -n <NS>

Update the image and trigger a rolling update

kubectl set image deployment/my-app my-app=nginx:1.26 -n <NS>

Watch the rollout progress until it completes

kubectl rollout status deployment/my-app -n <NS>

Roll the pods again without changing the image tag

kubectl rollout restart deployment/my-app -n <NS>

Roll back one revision; use --to-revision to pick one

kubectl rollout undo deployment/my-app -n <NS>

Scale replicas; verify free node capacity first

kubectl scale deployment/my-app --replicas=4 -n <NS>

Troubleshooting & Nodes

Show pod memory usage sorted by consumption

kubectl top pod -n <NS> --sort-by=memory

Inspect node allocation, taints and bound pods

kubectl describe node <NODE>

Mark a node unschedulable while pods keep running

kubectl cordon <NODE>
Risky

Evict all pods from a node; check replicas and PDBs first

kubectl drain <NODE> --ignore-daemonsets --delete-emptydir-data

Single-replica services go down; run uncordon after maintenance.

Restore scheduling on the node

kubectl uncordon <NODE>
Risky

Delete a pod to force recreation; safe only under a controller

kubectl delete pod <POD> -n <NS>

Standalone pods deleted this way are gone for good.

Config & Manifests

Create a ConfigMap from a config file

kubectl create configmap app-config --from-file=app.conf -n <NS>

Create a secret; the value stays in shell history

kubectl create secret generic app-secret --from-literal=password=<PASSWORD> -n <NS>

Prefer --from-file or stdin to keep values out of history.

Create or update resources from a manifest

kubectl apply -f deployment.yaml -n <NS>

Preview what apply would change

kubectl diff -f deployment.yaml -n <NS>
Risky

Delete every resource in the manifest; diff before running

kubectl delete -f deployment.yaml -n <NS>

Resources removed this way are hard to recover.

Related tools

This website uses cookies to ensure you get the best experience on our website. Privacy Policy.