TL;DR

kubectl get pods to check status, kubectl logs -f <pod> to follow logs, kubectl exec -it <pod> -- sh to get a shell, kubectl apply -f x.yaml to apply config, kubectl port-forward pod/<pod> 8080:80 to tunnel a port locally.

Inspecting Resources

ActionCommand
List podskubectl get pods
All namespaceskubectl get pods -A (--all-namespaces)
Everything in a namespacekubectl get all -n <ns>
Nodeskubectl get nodes
Services / ConfigMapskubectl get svc / kubectl get configmap
Events (sorted)kubectl get events --sort-by='.lastTimestamp'
Details & troubleshootingkubectl describe pod <pod>

Logs

ActionCommand
Recent logskubectl logs <pod>
Follow outputkubectl logs -f <pod>
Last N lineskubectl logs --tail=50 <pod>
Last 6 hourskubectl logs --since=6h <pod>
Logs before crashkubectl logs -p <pod> (--previous)
Multi-container: pick onekubectl logs <pod> -c <container>

Exec & Debugging

ActionCommand
Shell into a podkubectl exec -it <pod> -- sh (use -- bash if available)
Run a command in a podkubectl exec <pod> -- curl localhost:8080
Copy fileskubectl cp <pod>:/path/file ./local
Ephemeral debug containerkubectl debug <pod> -it --image=busybox

Port Forwarding

ActionCommand
Forward a pod portkubectl port-forward pod/<pod> 8080:80
Forward a service portkubectl port-forward svc/<svc> 8080:80

Apply & Rollouts

ActionCommand
Apply YAMLkubectl apply -f deploy.yaml
Delete resourceskubectl delete -f deploy.yaml / kubectl delete pod <pod>
Rolling restartkubectl rollout restart deploy/<name>
Rollout statuskubectl rollout status deploy/<name>
Scalekubectl scale deploy/<name> --replicas=5

Context & Namespaces

ActionCommand
Current contextkubectl config current-context
List / switch contextkubectl config get-contexts / kubectl config use-context <name>
Target a namespacekubectl get pods -n <ns> (or pin it: kubectl config set-context --current --namespace=<ns>)

Resource Usage

ActionCommand
Pod usagekubectl top pods
Node usagekubectl top nodes

Handy Aliases

alias k='kubectl'
alias kgp='kubectl get pods'
alias kgpa='kubectl get pods -A'
alias kl='kubectl logs'
alias klf='kubectl logs -f'
alias kex='kubectl exec -it'

Sources

最后更新:2026-08-04