泽兴芝士网

一站式 IT 编程学习资源平台

Prometheus实战教程:k8s平台-node-exporter监控物理机


监控图示

部署方式:

(1)docker或docker-compose部署 (本文使用) (2)二进制
(3)kubernetes中使用daemonset

步骤 1:准备工作

  1. 确保 Docker 已安装
    在部署之前,确保你的系统已经安装了 Docker。可以通过以下命令检查 Docker 是否已安装:
  2. docker --version
  3. 如果未安装,请参考 Docker 官方文档 进行安装。
  4. 拉取 node_exporter 镜像
    从 Docker Hub 或其他镜像仓库拉取 node_exporter 镜像:
  5. docker pull prom/node-exporter:v1.8.2

步骤 2:创建 docker-compose.yml 文件

在本地创建一个 docker-compose.yml 文件,内容如下:

version: '3'
services:
  node-exporter:
    image: prom/node-exporter:v1.8.2
    container_name: node-exporter
    ports:
      - "9100:9100"
    restart: always

docker命令运行

docker run -d \
  --name node-exporter \
  -p 9100:9100 \
  --restart always \
  harbor.fq.com/prometheus/node-exporter:v1.8.2    

步骤 3:启动 node_exporter

  1. 在终端中导航到包含 docker-compose.yml 文件的目录。
  2. 运行以下命令启动 node_exporter:
  3. docker-compose up -d
  4. -d 参数表示以“后台模式”运行。

步骤 4:验证 node_exporter 是否正常运行

  1. 检查容器状态
    运行以下命令,确认容器是否正常运行:
  2. docker ps -f name=node-exporter
  3. 输出示例:
  4. CONTAINER ID IMAGE COMMAND STATUS PORTS NAMES abc123def456 prom/node-exporter:v1.8.2 "/bin/node_exporter" Up 5 minutes 0.0.0.0:9100->9100/tcp node-exporter
  5. 访问 node_exporter 指标接口
    打开浏览器或使用 curl 访问 node_exporter 的指标接口:
  6. curl http://localhost:9100/metrics
  7. 如果一切正常,你会看到类似以下的输出(这是 Prometheus 格式的指标数据):
  8. # HELP go_gc_duration_seconds A summary of the GC invocation durations. # TYPE go_gc_duration_seconds summary go_gc_duration_seconds{quantile="0"} 0.000123 go_gc_duration_seconds{quantile="0.25"} 0.000456 ...
  9. 检查日志
    查看容器的日志,确认没有错误:
  10. docker logs node-exporter
  11. 查看主机端口
  12. [root@k8s-node03 ~]# ss -alutn |grep 9100 tcp LISTEN 0 10240 *:9100 *:* tcp LISTEN 0 10240 [::]:9100 [::]:*
---

### **步骤 5:配置 Prometheus 抓取 `node_exporter` 数据**

如果你使用 Prometheus 监控系统,需要将 `node_exporter` 添加到 Prometheus 的配置文件中。

5. 编辑 Prometheus 的配置文件 `prometheus.yml`,添加以下内容:

   ```yaml
   scrape_configs:
     - job_name: 'node_exporter'
       static_configs:
         - targets: ['<your-server-ip>:9100']

将 <your-server-ip> 替换为运行 node_exporter 的服务器的 IP 地址。 示例:

# [root@k8s-master02 monitoring]# cat prometheus/prometheus.yml
global:
  scrape_interval: 15s   # 每 15 秒抓取一次数据
  evaluation_interval: 15s

alerting:
  alertmanagers:
    - static_configs:
        - targets: ['alertmanager:9093']

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'node-exporter'
    static_configs:
      - targets: ['node-exporter:9100']

  - job_name: 'cadvisor'
    static_configs:
      - targets: ['cadvisor:8080']

  - job_name: 'pushgateway'
    static_configs:
      - targets: ['pushgateway:9091']
  - job_name: 'node-linux'
    static_configs:
      - targets: ['10.255.209.40:9100']
rule_files:
  - '/etc/prometheus/alert_rules.yml'
  1. 重启 Prometheus 以加载新配置:
  2. docker-compose restart prometheus
  3. 在 Prometheus 的 Web UI 中验证是否成功抓取数据:
  4. 打开 Prometheus 的 Web UI(默认地址:http://localhost:9090)。
  5. 在搜索栏中输入 up,查看 node_exporter 的状态是否为 1(表示正常)。

步骤 6:停止和清理

如果需要停止或删除 node_exporter 容器,可以运行以下命令: 3. 停止容器:

docker-compose down
  1. 删除容器和镜像:
  2. docker-compose down --rmi all
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言