Linux下安装和启动Prometheus

方法 / 步骤

1.下载Prometheus并安装

下载地址:https://prometheus.io/download/
下载并解压:

prometheus-2.10.0.linux-amd64.tar.gz

# tar -zxvf prometheus-2.10.0.linux-amd64.tar.gz

可以本地下载安后,上传到linux服务(如果下载比较慢,博主另一篇文章有相应的解决方案)

在linux服务下创建一个文件夹 ,命名为:prometheus,输入命令按回车键即可

mkdir prometheus-2.10.0    

上传本地下载好的prometheus压缩包到新建的文件夹里面,并解压

在这里插入图片描述

进入目录:

cd /usr/local/prometheus-2.10.0

在这里插入图片描述

修改配置文件:

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).
#external_labels:
#monitor: 'my-monitor'
# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093




# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
#rule_files:
  # - "first_rules.yml"
  # - "second_rules.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']

启动prometheus服务:

./prometheus --config.file=prometheus.yml &

在这里插入图片描述

IP+端口就可以访问了(IP指的是linux服务的ip)

在这里插入图片描述

2.部署node_exporter

Node_exporter收集机器的系统数据,这里采用prometheus官方提供的exporter,除node_exporter外,官方还提供consul,memcached,haproxy,mysqld等exporter,具体可查看官网。

这里部署node节点服务并且接入到prometheus服务中。

  • 下载离线安装包:
    https://github.com/prometheus/node_exporter/releases/tag/v1.0.0
    在这里插入图片描述
  • 解压到服务器的prometheus 并且启动
    在这里插入图片描述
./node_exporter
或者后台启动:
./node_exporter &

在这里插入图片描述
启动成功

将node_exporter 的服务接入到prometheus

在prometheus中的 prometheus.yml添加如下配置:

#添加node节点    
  - job_name: "node"
    static_configs:
    - targets: ["127.0.0.1:9100"]

在这里插入图片描述

重启prometheus;
在这里插入图片描述
指标数据已经接入!

为 Prometheus 添加 HTTP Basic Auth

Prometheus 的 Node Exporter 并没有提供任何认证支持。不过,借助 Nginx 作为反向代理服务器,我们可以很容易地为 Node Exporter 添加 HTTP Basic Auth 功能。

首先,启动 Node Exporter,监听 9090 端口。

然后,在 /etc/nginx (可能你的 Nginx 配置目录在其他路径,请做相应修改)目录下,使用 apache2-utils 提供的 htpasswd 工具创建一个用户文件,需要填入用户名和密码:

$ htpasswd -c .htpasswd yuankun
New password: 
Re-type new password: 
Adding password for user yuankun

接下来,在 Nginx 配置文件中添加下面的配置,这里我们使用 19090 作为代理端口:

http {
  server {
    listen 0.0.0.0:19090;
    location / {
      proxy_pass http://localhost:9090/;

      auth_basic "Prometheus";
      auth_basic_user_file ".htpasswd";
    }
  }

保存配置文件,然后重新载入 Nginx 服务。此时在浏览器中访问 server:19090,浏览器会要求你输入用户名和密码。

最后一步是修改 prometheus.yml 文件,将我们的 Node Exporter 服务添加进去:

 job_name: 'node-exporter'
    static_configs:
      - targets: ['your-ip:9090']
    basic_auth:
      username: yuankun
      password: your-password

参考资料 & 致谢

【1】linux服务安装启动prometheus
【2】为 Prometheus 添加 HTTP Basic Auth
【3】prometheus学习系列四: Prometheus详述
【4】Prometheus监控安装及使用(一)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OxYGC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值