📖 Kubernetes 命令速查

按使用场景分组,点击"复制"按钮一键复制命令

集群与上下文
查看客户端与服务端版本
kubectl version
查看集群信息(API 地址)
kubectl cluster-info
查看集群节点
kubectl get nodes
查看全部上下文(环境切换列表)
kubectl config get-contexts
切换上下文
kubectl config use-context <context-name>
Pod 与工作负载
查看 Pod 列表
kubectl get pods
查看 Pod 详情(事件 / 状态)
kubectl describe pod <name>
跟踪容器日志
kubectl logs -f <pod> -c <container>
进入容器终端
kubectl exec -it <pod> -- /bin/sh
应用 / 更新资源配置
kubectl apply -f <file.yaml>
删除资源 ⚠️
kubectl delete -f <file.yaml>
扩缩容副本数
kubectl scale deployment <name> --replicas=3
服务与排查
查看 Service 列表
kubectl get svc
端口转发到本地调试
kubectl port-forward svc/<name> 8080:80
指定命名空间操作
kubectl -n <namespace> get pods
查看节点 / Pod 资源占用
`kubectl top node` / `kubectl top pod`
滚动重启(重新拉取镜像)
kubectl rollout restart deployment/<name>
配置与存储
创建 ConfigMap
kubectl create configmap <name> --from-file=<key>=<file>
创建 Secret(字符串)
kubectl create secret generic <name> --from-literal=<key>=<value>
查看 PVC 与状态
kubectl get pvc