kubectl 常用命令速查

常用命令

kubectl config current-context
kubectl config get-contexts
kubectl config use-context prod
kubectl get pods -A -o wide
kubectl get deploy,svc,ingress -n app
kubectl describe pod <pod> -n app
kubectl logs <pod> -n app -f
kubectl logs <pod> -n app --previous
kubectl exec -it <pod> -n app -- sh
kubectl apply -f manifest.yaml
kubectl diff -f manifest.yaml
kubectl delete -f manifest.yaml
kubectl rollout status deploy/app -n app
kubectl rollout history deploy/app -n app
kubectl rollout undo deploy/app -n app
kubectl scale deploy/app --replicas=3 -n app
kubectl port-forward svc/app 8080:80 -n app
kubectl cp app/pod:/tmp/file ./file
kubectl top pod -n app
kubectl get events -n app --sort-by=.lastTimestamp

场景速查

目的 命令
查资源定义 kubectl explain deploy.spec.strategy
查 API 资源 kubectl api-resources
查权限 kubectl auth can-i --list -n app
临时 Pod kubectl run tmp --rm -it --image=busybox -- sh
调试节点 kubectl debug node/<node> -it --image=busybox
导出 YAML kubectl get deploy app -n app -o yaml
按标签筛选 kubectl get pods -l app=web -n app
删除卡住的 Pod kubectl delete pod <pod> --grace-period=0 --force

易错点

相关命令