使用kubectl命令行工具(基礎監控)
kubectl是Kubernetes官方命令行工具,無需額外安裝,適合快速查看集群基礎狀態。常用命令包括:
kubectl get nodes(顯示節點名稱、狀態、角色等信息,狀態需為Ready);kubectl get pods --all-namespaces(查看所有命名空間的Pod運行狀態,Running表示正常);kubectl get deployments --all-namespaces(查看Deployment的副本數、更新狀態等);kubectl describe pod <pod-name> -n <namespace>(查看Pod的事件、容器狀態等詳細信息);kubectl get events --all-namespaces(查看集群近期事件,如節點異常、Pod調度失敗等)。使用Kubernetes Dashboard(Web可視化)
Kubernetes Dashboard是官方提供的Web UI,適合直觀監控集群資源和操作。安裝步驟:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.4.0/aio/deploy/recommended.yaml;kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')(復制token字段值);https://<node-ip>:30001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/,粘貼Token登錄。使用Prometheus+Grafana(專業監控與可視化)
Prometheus(監控數據采集)+Grafana(可視化展示)是企業級Kubernetes監控方案,適合大規模集群。安裝步驟:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts && helm install prometheus prometheus-community/kube-prometheus-stack;helm install grafana prometheus-community/grafana;http://<node-ip>:3000,賬號admin/admin),添加Prometheus數據源(地址為http://prometheus-operated.monitoring.svc.cluster.local:9090);使用Metrics Server(輕量級指標收集)
Metrics Server是Kubernetes官方輕量級指標聚合器,用于收集CPU、內存等資源使用數據,為kubectl top命令提供支持。安裝步驟:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml;kubectl get pods -n kube-system | grep metrics-server(確保Pod狀態為Running);kubectl top命令:kubectl top nodes(查看節點資源使用)、kubectl top pods --all-namespaces(查看Pod資源使用)。使用第三方監控工具(企業級增強)
補充:系統級命令(宿主機監控)
若需監控CentOS宿主機的資源使用情況(如CPU、內存、磁盤),可使用以下命令:
top(按M排序內存使用,P排序CPU使用)、htop(增強版top,需安裝:yum install -y epel-release && yum install -y htop);free -h(顯示總內存、已用、空閑等,-h表示人類可讀格式);iostat -x 1(顯示磁盤讀寫速率、I/O等待時間,需安裝sysstat:yum install -y sysstat);netstat -tulnp(顯示監聽端口、進程信息)或ss -tulnp(更現代的替代工具)。