This guide covers the most commonly used Kubernetes commands, explanations, and examples.
| Category | Command | Purpose |
|---|---|---|
| Cluster | `kubectl cluster-info` | Show cluster information |
| Cluster | `kubectl version` | Show client/server versions |
| Namespace | `kubectl get ns` | List namespaces |
| Namespace | `kubectl create ns <name>` | Create namespace |
| Namespace | `kubectl delete ns <name>` | Delete namespace |
| Resources | `kubectl get pods` | List pods |
| Resources | `kubectl get deployments` | List deployments |
| Resources | `kubectl get svc` | List services |
| Resources | `kubectl get nodes` | List nodes |
| Resources | `kubectl get all` | List common resources |
| Details | `kubectl describe pod <pod>` | Detailed pod information |
| Details | `kubectl describe deployment <name>` | Deployment details |
| Details | `kubectl describe node <node>` | Node details |
| Create | `kubectl apply -f file.yaml` | Create/update resources |
| Create | `kubectl create deployment …` | Create deployment quickly |
| Update | `kubectl edit deployment <name>` | Edit deployment |
| Update | `kubectl scale deployment <name> –replicas=N` | Scale deployment |
| Update | `kubectl set image …` | Update container image |
| Delete | `kubectl delete pod <pod>` | Delete pod |
| Delete | `kubectl delete deployment <name>` | Delete deployment |
| Delete | `kubectl delete service <name>` | Delete service |
| Logs | `kubectl logs <pod>` | Show logs |
| Logs | `kubectl logs -f <pod>` | Stream logs |
| Logs | `kubectl logs –previous <pod>` | Show previous container logs |
| Exec | `kubectl exec -it <pod> – bash` | Open shell inside container |
| Network | `kubectl port-forward pod/<pod> 8080:80` | Forward local port |
| Files | `kubectl cp file.txt pod:/tmp/` | Copy file to pod |
| Files | `kubectl cp pod:/tmp/file.txt .` | Copy file from pod |
| Rollout | `kubectl rollout status deployment/<name>` | Check rollout status |
| Rollout | `kubectl rollout history deployment/<name>` | Show rollout history |
| Rollout | `kubectl rollout undo deployment/<name>` | Rollback deployment |
| Config | `kubectl config get-contexts` | List contexts |
| Config | `kubectl config use-context <ctx>` | Switch context |
| Debug | `kubectl top pod` | Show pod CPU/memory |
| Debug | `kubectl top node` | Show node CPU/memory |
kubectl cluster-info
Displays API server and cluster services.
Example:
kubectl cluster-info
kubectl version
Example:
kubectl version --short
Output:
Client Version: v1.34.0 Server Version: v1.34.0
kubectl get namespaces
Short form:
kubectl get ns
kubectl create namespace dev
kubectl delete namespace dev
kubectl get pods
kubectl get pods -A
kubectl get deployments
kubectl get svc
kubectl get nodes
kubectl get all
Displays:
kubectl describe pod nginx
Useful for:
kubectl describe deployment nginx
kubectl describe node worker-1
Shows:
kubectl apply -f deployment.yaml
Most common Kubernetes command.
Example:
kubectl apply -f nginx-deployment.yaml
kubectl apply -f manifests/
kubectl create deployment nginx --image=nginx
kubectl edit deployment nginx
kubectl scale deployment nginx --replicas=5
kubectl set image deployment/nginx nginx=nginx:1.27
kubectl delete pod nginx
kubectl delete deployment nginx
kubectl delete service nginx
kubectl delete -f deployment.yaml
kubectl logs nginx
kubectl logs -f nginx
Equivalent to:
tail -f
kubectl logs nginx -c app
kubectl logs nginx --previous
Useful when troubleshooting:
kubectl exec nginx -- ls
kubectl exec -it nginx -- bash
or
kubectl exec -it nginx -- sh
kubectl port-forward pod/nginx 8080:80
Access application:
http://localhost:8080
kubectl port-forward deployment/nginx 8080:80
kubectl cp test.txt nginx:/tmp/test.txt
kubectl cp nginx:/tmp/log.txt .
kubectl rollout status deployment/nginx
kubectl rollout history deployment/nginx
kubectl rollout undo deployment/nginx
kubectl config get-contexts
kubectl config current-context
kubectl config use-context production
kubectl top pod
kubectl top node
When an application is not working:
kubectl get pods kubectl describe pod <pod-name> kubectl logs <pod-name> kubectl exec -it <pod-name> -- sh kubectl get events --sort-by=.metadata.creationTimestamp
Typical troubleshooting order: