美文网首页
Prometheus安装配置

Prometheus安装配置

作者: 深入浅出 | 来源:发表于2023-10-30 17:59 被阅读0次

    1 Prometheus安装配置

    1.1 下载

    在 Prometheus 官网 https://prometheus.io/download/#prometheus 获取适用于 Linux 的 Prometheus 安装包。

    image.png

    1.2 安装

    系统环境如下:

    [root@centos ~]# cat /etc/redhat-release
    CentOS Linux release 7.9.2009 (Core)
    [root@centos ~]# uname -r
    3.10.0-957.el7.x86_64
    

    为了安全,不用 root 用户启动相关服务;自建 prometheus 用户启动服务

    groupadd prometheus
    useradd -g prometheus -M -s /sbin/nologin prometheus
    

    将安装包 prometheus-2.47.1.linux-amd64.tar.gz 上传至服务器 /opt/src 目录下
    解压压缩包

    [root@centos ~]# cd /opt/src
    [root@centos src]# tar -zxvf prometheus-2.47.1.linux-amd64.tar.gz -C /opt
    [root@centos src]# cd /opt
    [root@centos opt]# mv prometheus-2.47.1.linux-amd64/ prometheus
    [root@centos opt]# mkdir -pv /opt/prometheus/data
    [root@centos opt]# chown -R prometheus.prometheus /opt/prometheus
    

    切换到解压缩后的目录,执行 prometheus --version 命令查看是否正常

    [root@centos ~]# cd /opt/prometheus/
    [root@centos prometheus]# ./prometheus --version
    prometheus, version 2.47.1 (branch: HEAD, revision: c4d1a8beff37cc004f1dc4ab9d2e73193f51aaeb)
      build user:       root@4829330363be
      build date:       20231004-10:31:16
      go version:       go1.21.1
      platform:         linux/amd64
      tags:             netgo,builtinassets,stringlabels
    

    创建Systemd服务启动prometheus

    [root@centos opt]# vim /etc/systemd/system/prometheus.service
    
    [Unit]
    Description=Prometheus Server
    Documentation=https://prometheus.io/docs/introduction/overview/
    After=network-online.target
    
    [Service]
    User=prometheus
    Restart=on-failure
    ExecStart=/opt/prometheus/prometheus \
      --config.file=/opt/prometheus/prometheus.yml \
      --storage.tsdb.path=/opt/prometheus/data
    ExecReload=/bin/kill -HUP $MAINPID
    [Install]
    WantedBy=multi-user.target
    

    在service文件里面,我们定义了启动的命令,定义了数据存储在/opt/prometheus/data路径下。

    启停命令

    systemctl daemon-reload
    systemctl enable prometheus
    systemctl start prometheus
    systemctl status prometheus
    systemctl reload prometheus
    

    1.3 配置

    Prometheus 通过抓取监控目标上的 HTTP 端点来收集指标,而且 Prometheus 本身也暴露 metrics 指标接口,所以它也可以抓取并监控其自身的运行状况。

    1.3.1 收集Prometheus自身数据配置

    配置文件 /opt/prometheus-2.47.1.linux-amd64\prometheus.yml

    global:
      scrape_interval: 5s # 抓取频率
      
    scrape_configs:
      - job_name: "prometheus"
        static_configs:
          - targets: ["localhost:9090"]
    

    上面配置了 Prometheus 每 5s 从自身抓取指标。global 区域用于配置一些全局配置和默认值,scrape_configs 部分是用来告诉 Prometheus 要抓取哪些目标。

    在实际情况下,抓取频率间隔通常在 10 到 60 秒之间。

    1.3.2 安装配置 node_exporter

    为监控服务器 CPU , 内存 , 磁盘 , I/O 等信息,需要在被监控机器上安装 node_exporter 服务。
    首先我们需要从 node_exporter下载页 下载我们需要安装的版本。
    node_exporter-1.6.1.linux-amd64.tar.gz
    解压并安装 node_exporter 服务:

    [root@centos ~]# cd /opt/src
    [root@centos src]# tar -zxvf node_exporter-1.6.1.linux-amd64.tar.gz -C /opt
    [root@centos src]# cd /opt/
    [root@centos opt]# mv node_exporter-1.6.1.linux-amd64/ node_exporter
    [root@centos opt]# chown -R prometheus.prometheus /opt/node_exporter
    

    创建Systemd服务启动node_exporter

    [root@centos opt]# vim /etc/systemd/system/node_exporter.service
    
    [Unit]
    Description=Node Exporter
    Wants=network-online.target
    After=network-online.target
    
    [Service]
    User=prometheus
    ExecStart=/opt/node_exporter/node_exporter
    
    [Install]
    WantedBy=default.target
    

    启动 node_exporter 服务:

    systemctl daemon-reload
    systemctl enable node_exporter
    systemctl start node_exporter
    systemctl status node_exporter
    

    修改 prometheus 的配置文件/srv/prometheus/prometheus.yml,增加如下内容:

    scrape_configs:
    ...
    - job_name: 'node'
        scrape_interval: 10s
        static_configs:
          - targets: ['localhost:9100','192.168.1.100:9100']
    

    重启 Prometheus 服务:

    systemctl reload prometheus
    

    1.4 查看监控目标

    当启动 Prometheus 后,我们可以检查下它是否正确的抓取了配置的目标,可以在浏览器中访问 http://<host-ip>:9090/targets 来查看所有的抓取目标列表

    image.png
    如果我们配置的抓取本身的 prometheus 这个任务显示的绿色的 UP 状态,证明 Prometheus 已经正常抓取自身的监控指标了。

    如果在抓取过程中出现任何问题(DNS 解析失败、连接超时等等错误),抓取目标都会显示为 DOWN,同时还有一条错误消息,提供有关抓取失败的相关信息,可以帮助我们快速发现错误配置或不健康的目标。

    2 Grafana安装配置

    Grafana 用来展示 Prometheus 的监控指标

    2.1 下载

    到Grafana的官网进行下载
    grafana-enterprise-10.2.0.linux-amd64.tar.gz

    2.2 安装

    [root@centos opt]# cd /opt/src/
    [root@centos src]# tar -zxvf grafana-enterprise-10.2.0.linux-amd64.tar.gz -C /opt
    [root@centos src]# cd /opt/
    [root@centos opt]# mv grafana-10.2.0/ grafana
    [root@centos opt]# mkdir -pv /data/grafana/data
    [root@centos opt]# mkdir -pv /data/grafana/log
    [root@centos opt]# mkdir -pv /data/grafana/plugins
    [root@centos opt]# mkdir -pv /data/grafana/conf/provisioning
    [root@centos opt]# chown -R prometheus.prometheus /opt/grafana
    [root@centos opt]# chown -R prometheus.prometheus /data/grafana
    [root@centos opt]# chmod 777 /data/grafana/*
    

    2.3 配置

    修改 /opt/grafana/conf/defaults.ini 文件,配置为上面新建的数据目录。

    data = /data/grafana/data
    logs = /data/grafana/log
    plugins = /data/grafana/plugins
    provisioning = /data/grafana/conf/provisioning
    

    2.4 把 grafana-server 添加到 systemd

    新增 grafana-server.service文件,使用systemd来管理grafana服务

    vim /etc/systemd/system/grafana-server.service
    
    [Unit]
    Description=Grafana
    After=network.target
    
    [Service]
    User=prometheus
    Group=prometheus
    Type=notify
    ExecStart=/opt/grafana/bin/grafana-server \
        --config=/opt/grafana/conf/defaults.ini \
        --homepath=/opt/grafana
    Restart=on-failure
    
    [Install]
    WantedBy=multi-user.target
    

    启停并设置开机启动命令

    #重载配置:
    systemctl daemon-reload
    #设置开机启动
    systemctl enable grafana-server
    #启动服务
    systemctl start grafana-server
    #关闭服务
    systemctl stop grafana-server
    #查看服务状态
    systemctl status grafana-server
    

    2.5 打开 Grafana 的 web 页面

    在浏览器中访问 http://<host-ip>:3000
    默认的账号密码 admin/admin

    image.png

    2.6 添加数据源

    image.png

    2.7 导入Grafana模板,数据展示

    如要使用其他的模板,请到grafana的官网去查找 https://grafana.com/grafana/dashboards/

    image.png
    image.png image.png

    相关文章

      网友评论

          本文标题:Prometheus安装配置

          本文链接:https://www.haomeiwen.com/subject/vdgbidtx.html