Skip to main content
Documents
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Kubernetes 核心命令

🔥 一、集群级操作(Cluster)

查看集群信息

kubectl cluster-info
kubectl version
kubectl api-resources
kubectl api-versions

查看当前上下文

kubectl config get-contexts
kubectl config current-context

切换上下文

kubectl config use-context <context>

🔥 二、命名空间(Namespace)

查看所有命名空间

kubectl get ns

创建 / 删除

kubectl create ns dev
kubectl delete ns dev

选择 Namespace

kubectl -n dev get pods

🔥 三、Pod 核心命令

查看 Pod

kubectl get pods
kubectl get pods -A
kubectl get pods -owide
kubectl get pods -A --field-selector=status.phase!=Running
kubectl get pods -l app=nginx

查看 Pod 详细信息

kubectl describe pod <pod>
kubectl logs <pod>
kubectl logs -f <pod> -c <container>

进入 Pod 容器

kubectl exec -it <pod> -- sh
kubectl exec -it <pod> -c <container> -- bash

在指定 Namespace

kubectl -n dev exec -it <pod> -- sh

从 Pod 拷贝文件

kubectl cp <ns>/<pod>:<path> <local>
kubectl cp ./local-file dev/my-pod:/tmp/file

🔥 四、Deployment 核心命令

查看 Deployment

kubectl get deploy
kubectl describe deploy <name>

更新镜像

kubectl set image deployment/nginx nginx=nginx:1.25

如果有多个 container:

kubectl set image deploy/myapp c1=img1:v1 c2=img2:v2

发布 / 回滚

kubectl rollout restart deploy/nginx
kubectl rollout status deploy/nginx
kubectl rollout history deploy/nginx
kubectl rollout undo deploy/nginx --to-revision=3

🔥 五、Service 核心命令

查看 Service

kubectl get svc
kubectl describe svc nginx

临时访问(端口转发)

kubectl port-forward svc/my-svc 8080:80
# 关闭:Ctrl+C

🔥 六、ConfigMap / Secret

ConfigMap

kubectl create configmap conf --from-file=./config.yaml
kubectl get cm
kubectl describe cm conf
kubectl delete cm conf

Secret(Base64 自动编码)

kubectl create secret generic db-pass --from-literal=password=123456
kubectl get secret
kubectl describe secret db-pass

🔥 七、Node 节点操作命令

查看节点

kubectl get nodes -owide

查看带特定 label 的节点

kubectl get nodes -l node-role.kubernetes.io/mysql=''

给节点打标签

kubectl label node node01 node-role.kubernetes.io/mysql=

清空节点(排空)

kubectl drain node01 --ignore-daemonsets --delete-emptydir-data

取消排空

kubectl uncordon node01

🔥 八、事件与排错常用命令

查看事件

kubectl get events --sort-by=.metadata.creationTimestamp

查看某个 Pod 的事件

kubectl describe pod <pod>

跟踪日志

kubectl logs -f <pod>
kubectl logs -f <pod> -c <container>

🔥 九、强制删除资源

kubectl delete pod <pod> --force --grace-period=0

🔥 十、应用部署命令

从 YAML 部署

kubectl apply -f app.yaml
kubectl delete -f app.yaml

检查 YAML

kubectl apply --dry-run=client -f app.yaml

🔥 十一、资源分类查看命令

查看所有 workload(Pod/Deploy/StatefulSet/DaemonSet/Job)

kubectl get all

查看 Detailed Workload

kubectl get deploy,statefulset,daemonset,job,cronjob

🔥 十二、临时调试 Pod(Ephemeral Containers)

Kubernetes ≥ 1.25:

kubectl debug pod-name -it --image=busybox

🔥 十三、CRD(自定义资源)

kubectl get crd
kubectl describe crd <name>

🔥 十四、权限 RBAC

kubectl auth can-i get pods
kubectl auth can-i delete node --as admin
kubectl auth can-i create pods -n dev

🔥 十五、最常用短命令复习版(20 条必记)

kubectl get pods -A
kubectl logs -f pod
kubectl describe pod pod
kubectl exec -it pod -- sh
kubectl apply -f xxx.yaml
kubectl delete -f xxx.yaml
kubectl get deploy
kubectl set image deploy/myapp app=myimg:v2
kubectl rollout restart deploy/myapp
kubectl rollout undo deploy/myapp
kubectl get svc
kubectl port-forward svc/myapp 8080:80
kubectl get nodes -owide
kubectl drain node01 --ignore-daemonsets
kubectl uncordon node01
kubectl label node node01 key=value
kubectl get events --sort-by=.metadata.creationTimestamp
kubectl cp ns/pod:/file .
kubectl top pod
kubectl top node