prometheus以及grafana的安装介绍

半日闲 2020年07月22日 41次浏览

准备工作

下载

wget https://github.com/prometheus/prometheus/releases/download/v2.19.2/prometheus-2.19.2.linux-amd64.tar.gz
wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz

官方文档

https://songjiayang.gitbooks.io/prometheus/content/

第三方exporter,本次示例只以node_exporter为例

https://prometheus.io/docs/instrumenting/exporters/

启动node_exporter

  1. 解压tar.gz的包,并放到/usr/local/

    tar xzvf node_exporter-1.0.1.linux-amd64.tar.gz
    mv node_exporter-1.0.1.linux-amd64 /usr/local/node_exporter
    
  2. 编写systemd配置文件

    [root@home prometheus]$ cat /etc/systemd/system/node_exporter.service
    [Unit]
    Description=Node_exporter
    After=network.target
    [Service]
    Type=simple
    ExecStart=/usr/local/node_exporter/node_exporter
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    
  3. 启动node_export

    systemctl daemon-reload && systemctl start node_exporter && systemctl status node_exporter
    

启动prometheus

  1. 解压tar.gz的包,并放到/usr/local/目录下

    tar xzvf prometheus-2.19.2.linux-amd64.tar.gz && mv prometheus-2.19.2.linux-amd64 /usr/local/prometheus
    
  2. 配置prometheus.yml文件

    # A scrape configuration containing exactly one endpoint to scrape:
    # Here it's Prometheus itself.
    scrape_configs:
      # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
      - job_name: 'prometheus'
    
        # metrics_path defaults to '/metrics'
        # scheme defaults to 'http'.
    
        static_configs:
        - targets: ['localhost:9090']
      ## 如果需要增加额外的exporter,直接在该文件中填写以下2行,重启prometheus即可
      - job_name: 'node_exporter'
        static_configs:
        - targets: ['localhost:9100']
    
  3. 编写systemd文件

    root@home local]$ cat /etc/systemd/system/prometheus.service
    [Unit]
    Description=Prometheus
    Documentation=https://prometheus.io/
    After=network.target
    [Service]
    Type=simple
    ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    
  4. 启动prometheus

    systemctl daemon-reload && systemctl start prometheus && systemctl status prometheus
    

启动grafana

  1. 下载grafanarpm

     wget https://dl.grafana.com/oss/release/grafana-7.1.0-1.x86_64.rpm && yum localinstall grafana-7.1.0-1.x86_64.rpm
    
  2. 启动grafana

    systemctl daemon-reload && systemctl start grafana-server
    
  3. 登录grafana,并设置prometheus为数据源

  4. 查看dashborad,并导入好看的dashboard,好看的dashboard可以去官网找