TL;DR

kubectl get pods 看状态,kubectl logs -f <pod> 跟日志,kubectl exec -it <pod> -- sh 进容器,kubectl apply -f x.yaml 应用配置,kubectl port-forward pod/<pod> 8080:80 本地转发端口。

查看资源

操作命令
列出所有 Podkubectl get pods
所有命名空间kubectl get pods -A--all-namespaces
查看全部资源kubectl get all -n <ns>
节点kubectl get nodes
服务/配置kubectl get svc / kubectl get configmap
事件(按时间)kubectl get events --sort-by='.lastTimestamp'
详情与排查kubectl describe pod <pod>

日志

操作命令
最近日志kubectl logs <pod>
跟随输出kubectl logs -f <pod>
只取末尾 N 行kubectl logs --tail=50 <pod>
最近 6 小时kubectl logs --since=6h <pod>
崩溃前日志kubectl logs -p <pod>--previous
多容器指定容器kubectl logs <pod> -c <container>

进入容器与调试

操作命令
进入容器 shellkubectl exec -it <pod> -- sh(有 bash 用 -- bash
容器内执行命令kubectl exec <pod> -- curl localhost:8080
复制文件kubectl cp <pod>:/path/file ./local
临时调试容器kubectl debug <pod> -it --image=busybox

端口转发

操作命令
Pod 端口转发kubectl port-forward pod/<pod> 8080:80
Service 端口转发kubectl port-forward svc/<svc> 8080:80

应用与更新

操作命令
应用 YAMLkubectl apply -f deploy.yaml
删除资源kubectl delete -f deploy.yaml / kubectl delete pod <pod>
滚动重启kubectl rollout restart deploy/<name>
查看发布状态kubectl rollout status deploy/<name>
扩缩容kubectl scale deploy/<name> --replicas=5

上下文与命名空间

操作命令
当前上下文kubectl config current-context
列出/切换上下文kubectl config get-contexts / kubectl config use-context <name>
指定命名空间kubectl get pods -n <ns>(或用 kubectl config set-context --current --namespace=<ns> 固定)

资源占用

操作命令
Pod 资源占用kubectl top pods
节点资源占用kubectl top nodes

常用别名

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'

来源

最后更新:2026-08-04