Search and filter 100+ kubectl commands by category. Covers Pods, Deployments, Services, Secrets, Namespaces, Logs, Rollouts, RBAC, and useful flags. Click any card to copy.
Showing 113 of 113 commands — click any card to copy
Frequently Asked Questions
What is the difference between kubectl apply and kubectl create?
kubectl create creates a resource and fails if it already exists. kubectl apply creates the resource if it doesn't exist, or updates it if it does — it performs a three-way merge between the current live state, the last applied configuration, and the new desired state. In practice, prefer kubectl apply -f for declarative, idempotent infrastructure management. Use kubectl create for one-off imperative commands or when you deliberately want to fail on duplicates.
How do I switch between Kubernetes namespaces?
You can use -n <namespace> on any kubectl command for a one-off switch. To set a persistent default namespace for your current context, run: kubectl config set-context --current --namespace=<namespace>. Many teams install kubens (part of the kubectx project) which lets you switch namespace with a single command: kubens staging. All subsequent kubectl commands then target that namespace without needing -n.
What is the difference between a Deployment and a Pod?
A Pod is the smallest deployable unit in Kubernetes — it wraps one or more containers that share network and storage. However, Pods are ephemeral; if a Pod dies, it is not automatically restarted. A Deployment is a higher-level controller that manages a ReplicaSet, which in turn ensures the desired number of identical Pods are always running. Deployments handle rolling updates, rollbacks, and self-healing. In production you almost always create Deployments (or other controllers like StatefulSet, DaemonSet), not bare Pods.
How do I debug a crashed pod in Kubernetes?
Start with kubectl describe pod <name> to see the events section — it shows image pull errors, OOMKilled, CrashLoopBackOff reasons, and more. Then check logs with kubectl logs <name> --previous to see the stdout/stderr of the last crashed container. If the container exits too fast to inspect, use kubectl debug pod/<name> --image=busybox --stdin --tty to attach an ephemeral debug container (Kubernetes 1.23+), or run a temporary debug pod with kubectl run debug --image=busybox --restart=Never -it --rm -- sh.
What is port-forward used for?
kubectl port-forward creates a secure tunnel between your local machine and a Pod, Service, or Deployment inside the cluster — without needing a LoadBalancer or NodePort. It is exclusively a development/debugging tool; traffic goes through the Kubernetes API server. For example, kubectl port-forward svc/postgres 5432:5432 lets you connect to a database inside the cluster using localhost:5432 from your laptop. It is not suitable for production traffic routing.