CentOS7搭建Prometheus 监控Linux主机,


 简介

prometheus可以拆分成多个节点进行指标收集。

安装环境:CentOS7


安装prometheus

  1. wget -c https://github.com/prometheus/prometheus/releases/download/v2.23.0/prometheus-2.23.0.linux-amd64.tar.gz 
  2. tar zxvf prometheus-2.23.0.linux-amd64.tar.gz  -C /opt/ 
  3. cd /opt/ 
  4. ln -s prometheus-2.23.0.linux-amd64 prometheus 
  5. cat > /etc/systemd/system/prometheus.service <<EOF 
  6. [Unit] 
  7. Description=prometheus 
  8. After=network.target 
  9.  
  10. [Service] 
  11. Type=simple 
  12. WorkingDirectory=/opt/prometheus 
  13. ExecStart=/opt/prometheus/prometheus --config.file="/opt/prometheus/prometheus.yml" 
  14. LimitNOFILE=65536 
  15. PrivateTmp=true 
  16. RestartSec=2 
  17. StartLimitInterval=0 
  18. Restart=always 
  19.  
  20. [Install] 
  21. WantedBy=multi-user.target 
  22. EOF 
  23. systemctl daemon-reload  
  24. systemctl enable prometheus 
  25. systemctl start prometheus 

 

配置Prometheus

这里配置的是监听/opt/prometheus/servers/目录下的json文件

  1. cat > /opt/prometheus/prometheus.yml <<EOF 
  2. # my global config 
  3. global: 
  4.   scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. 
  5.   evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. 
  6.   # scrape_timeout is set to the global default (10s). 
  7. # Alertmanager configuration 
  8. alerting: 
  9.   alertmanagers: 
  10.   - static_configs: 
  11.     - targets: 
  12.       # - alertmanager:9093 
  13.  
  14. # Load rules once and periodically evaluate them according to the global 'evaluation_interval'. 
  15. rule_files: 
  16.   # - "first_rules.yml" 
  17.   # - "second_rules.yml" 
  18.  
  19. # A scrape configuration containing exactly one endpoint to scrape: 
  20. # Here it's Prometheus itself. 
  21. scrape_configs: 
  22.   # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. 
  23.   - job_name: 'prometheus' 
  24.  
  25.     # metrics_path defaults to '/metrics' 
  26.     # scheme defaults to 'http'. 
  27.  
  28.     static_configs: 
  29.     - targets: ['localhost:9090'] 
  30.      
  31.   - job_name: 'servers' 
  32.     file_sd_configs: 
  33.     - refresh_interval: 61s 
  34.       files: 
  35.         - /opt/prometheus/servers/*.json 
  36. EOF 
  37. systemctl restart prometheus 

json格式


每个json文件需要是一个数组对象,如果不需要自定义标签,可以直接写到targets里面去也可以,可以有多个文件

  1. [     
  2.     { 
  3.         "targets": [ 
  4.             "192.168.1.164:9100" 
  5.         ], 
  6.         "labels": { 
  7.             "instance": "192.168.1.164", 
  8.             "job": "node_exporter" 
  9.         } 
  10.     }, 
  11.     { 
  12.         "targets": [ 
  13.             "192.168.1.167:9100" 
  14.         ], 
  15.         "labels": { 
  16.             "instance": "192.168.1.167", 
  17.             "job": "node_exporter" 
  18.         } 
  19.     } 

安装node_exporter

安装到/opt/node_exporter路径下,保持默认的端口

  1. https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz 
  2. tar zxvf node_exporter-1.0.1.linux-amd64.tar.gz -C /opt/ 
  3. cd /opt/ 
  4. ln -s  node_exporter-1.0.1.linux-amd64 node_exporter 
  5. cat > /etc/systemd/system/node_exporter.service <<EOF 
  6. [Unit] 
  7. Description=node_exporter 
  8. After=network.target 
  9.  
  10. [Service] 
  11. Type=simple 
  12. WorkingDirectory=/opt/node_exporter 
  13. ExecStart=/opt/node_exporter/node_exporter 
  14. LimitNOFILE=65536 
  15. PrivateTmp=true 
  16. RestartSec=2 
  17. StartLimitInterval=0 
  18. Restart=always 
  19.  
  20. [Install] 
  21. WantedBy=multi-user.target 
  22. EOF 
  23. systemctl daemon-reload 
  24. systemctl enable node_exporter 
  25. systemctl start node_exporter 

图形展示

直接安装grafana进行展示

  1. yum -y install   https://dl.grafana.com/oss/release/grafana-7.3.6-1.x86_64.rpm 
  2. systemctl enable grafana-server 
  3. systemctl start grafana-server 

启动之后,grafana默认监听的是3000端口,直接使用浏览器进行访问就可以了,默认用户名密码是admin/admin,第一次登陆之后会提示修改。


配置数据源:鼠标左边的菜单 Configuration -> Data Source -> Add data source -> 选择prometheus -> url那栏填入prometheus的地址就可以了 -> 最后 Save & test 就可以了。

grafana.com/grafana/dashboards 官网已经有人做好的模板,我们直接import进来就可以了。

导入面板:鼠标左边的菜单 Dashboards -> Import -> 填入id -> Load -> 选择数据源就可以了。

我经常用的是:1860 、8919 这两个来查看node_exporter监控


总结

安装这些服务都是使用systemd进行管理的,操作起来比较方便的。

这里没有设置告警,可以根据自己的需要设置对应的告警规则,使用alertmanager进行告警。

相关内容