使用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
(更现代的替代工具)。