kubectl常用命令
kubectl
常用指令表格(繁體中文)
以下是 kubectl
常用指令的表格整理,包含了集群資訊查看、資源管理、排錯調試、日誌查看等各類常用操作,方便你快速查找和使用。
功能分類 | 指令 | 說明 |
---|---|---|
集群資訊 | kubectl version |
查看客戶端和伺服器的版本資訊 |
kubectl cluster-info |
查看集群的基本資訊 | |
kubectl get nodes |
列出所有節點(Nodes) | |
kubectl describe node <node> |
查看指定節點的詳細資訊 | |
查看資源 | kubectl get pods |
查看目前命名空間的所有 Pods |
kubectl get pods -n <namespace> |
查看指定命名空間的 Pods | |
kubectl get services |
查看目前命名空間的所有 Services | |
kubectl get deployments |
查看目前命名空間的所有 Deployments | |
kubectl get all |
查看目前命名空間的所有資源 | |
查看詳細資訊 | kubectl describe pod <pod> |
查看 Pod 的詳細資訊 |
kubectl describe svc <service> |
查看 Service 的詳細資訊 | |
創建資源 | kubectl apply -f <file.yaml> |
使用 YAML 文件創建或更新資源 |
kubectl create deployment |
創建一個新的 Deployment | |
更新資源 | kubectl edit <resource> <name> |
直接編輯現有資源 |
刪除資源 | kubectl delete pod <pod> |
刪除指定的 Pod |
kubectl delete -f <file.yaml> |
根據 YAML 文件刪除資源 | |
查看日誌 | kubectl logs <pod> |
查看指定 Pod 的日誌 |
kubectl logs <pod> -f |
即時追蹤 Pod 的日誌 | |
kubectl logs <pod> --tail=100 |
查看最近 100 行的 Pod 日誌 | |
執行指令 | kubectl exec -it <pod> -- bash |
進入 Pod 的終端 |
kubectl exec <pod> -- <command> |
在 Pod 中執行指定的指令 | |
排錯與調試 | kubectl describe pod <pod> |
查看 Pod 的詳細資訊,用於排查錯誤 |
kubectl get events |
查看集群的事件日誌 | |
kubectl top node |
查看節點的資源使用狀況 | |
kubectl top pod |
查看 Pod 的資源使用狀況 | |
命名空間管理 | kubectl get ns |
查看所有命名空間 |
kubectl create namespace <ns> |
創建新的命名空間 | |
kubectl delete namespace <ns> |
刪除指定的命名空間 | |
配置管理 | kubectl config view |
查看當前的 kubeconfig 配置 |
kubectl config get-contexts |
查看可用的上下文(Contexts) | |
kubectl config use-context <ctx> |
切換至指定的上下文 | |
YAML 輸出 | kubectl get <resource> -o yaml |
將資源輸出為 YAML 格式 |
監控資源 | kubectl get pods --watch |
即時監控 Pod 的狀態變化 |
滾動更新 | kubectl rollout status <deploy> |
查看 Deployment 的滾動更新狀態 |
kubectl rollout undo <deploy> |
回滾 Deployment 至上一版本 |
kubectl create deployment nginx --image=nginx
kubectl expose deployment --port=80 --type=NodePort
kubectl get pod,svc
kubectl get cs
kubectl get pods
kubectl apply -f