Prometheus 安装与配置

本文涉及的产品
可观测可视化 Grafana 版,10个用户账号 1个月
简介: Prometheus 安装与配置

一、二进制包安装

我们可以到 Prometheus 二进制安装包下载页面,根据自己的操作系统选择下载对应的安装包。下面我们将以 Amazon Linux 2 作为演示。

我们以现在的时间为准,下载最新版本 v2.17.1

1.1、下载 Prometheus Server

wget https://github.com/prometheus/prometheus/releases/download/v2.17.1/prometheus-2.17.1.linux-amd64.tar.gz
 
tar xf prometheus-2.17.1.linux-amd64.tar.gz
mv prometheus-2.17.1.linux-amd64/prometheus /usr/local/bin/

1.2、配置启动文件

因为是生产环境,我们为其配置启动文件。

vim /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus Server
After=network.target
Documentation=https://prometheus.io/docs/introduction/overview/
 
[Service]
Type=simple
WorkingDirectory=/home/data/prometheus/
ExecStart=/usr/local/bin/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --web.read-timeout=5m \
  --web.max-connections=512 \
  --storage.tsdb.retention=15d \
  --storage.tsdb.path=/home/data/prometheus \
  --query.timeout=2m
 
Restart=on-failure
 
[Install]
WantedBy=multi-user.target

1.3、配置文件

Prometheus 通过在目标节点的 HTTP 端口上采集 metrics(遥测专用词,度量指标)来监控目标节点(以下会称为“采样目标”)。因为 Prometheus 也以相同的方式暴露自己的数据,所以他也可以采集和检查自己的健康状况。

我们把配置文件转移到标准目录/etc/prometheus/

mkdir /etc/prometheus
mv prometheus-2.17.1.linux-amd64/prometheus.yml /etc/prometheus

初始的配置文件比较简单,如下,自带了一个 job,来监控 prometheus server 的状态。

global:
  scrape_interval:     15s 
  evaluation_interval: 15s
 
alerting:
  alertmanagers:
  - static_configs:
    - targets:
 
rule_files:
 
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

1.4、启动

systemctl daemon-reload
systemctl start prometheus
systemctl enable prometheus

1.5、查看

启动好之后,prometheus 服务会监听在端口 9090,我们使用 IP + Port,即可查看 prometheus 简单的图像界面:

  1. 可以看出 Prometheus 二进制安装非常方便,没有依赖,自带查询 web 界面。
  2. 在生产环境中,我们可以将 Prometheus 添加到 init 配置里,或者使用 supervisord 作为服务自启动。

二、Docker 安装

首先确保你已安装了最新版本的 Docker, 如果没有安装请点击这里

下面我将以 Mac 版本的 Docker 作为演示。

2.1、安装

docker run \
    -p 9090:9090 \
    -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
    prom/prometheus

2.2、Docker 管理 prometheus

运行 docker ps 查看所有服务:

CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS              PORTS                      NAMES
e9ebc2435387        quay.io/prometheus/prometheus   "/bin/prometheus -..."   26 minutes ago      Up 26 minutes       0.0.0.0:9090->9090/tcp   prometheus

运行 docker start prometheus 启动服务

运行 docker stats prometheus 查看 prometheus 状态

运行 docker stop prometheus 停止服务

三、node_export 安装

node_exporter 主要用于 *NIX 系统监控, 用 Golang 编写。

3.1、二进制安装

下载地址:Download | Prometheus

wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
 
tar xf node_exporter-0.18.1.linux-amd64.tar.gz
 
mv node_exporter-0.18.1.linux-amd64/node_exporter /usr/local/bin/

3.2、配置启动文件

vim /etc/systemd/system/node_export.service
[Unit]
Description=Node Export
After=network.target
Documentation=https://prometheus.io/docs/guides/node-exporter/
 
[Service]
Type=simple
WorkingDirectory=/tmp/
ExecStart=/usr/local/bin/node_exporter 
 
Restart=on-failure
 
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start node_export
systemctl enable node_export

3.3、加入监控

我们手动加入 prometheus 监控,修改其配置文件,再尾部增加如下内容:

  - job_name: 'node_export'
    static_configs:
      - targets: 
        - localhost:9100
        - web1:9100
        - web2:9100
        - web3:9100
        - web4:9100

prometheus 服务器需要可以解析这些地址,我们可以修改其 /etc/hosts 文件。

docker run -d \
  --net="host" \
  --pid="host" \
  -v "/:/host:ro,rslave" \
  quay.io/prometheus/node-exporter \
  --path.rootfs=/host

4.1、下载

下载地址:Download Grafana | Grafana Labs

我推荐使用 rpm 包进行安装,这样很多依赖可以自动解决,而且也配置好了启动脚本。

wget https://dl.grafana.com/oss/release/grafana-6.7.2-1.x86_64.rpm
sudo yum install grafana-6.7.2-1.x86_64.rpm

4.2、启动

systemctl enable grafana-server
systemctl start grafana-server

4.3、访问

访问 IP + Port,grafana 默认的启动 port 是 3000,初始账号和密码都是 admin,下面是登陆之后的界面,我这里截图是已经配置好数据源的。

4.4、配置数据源

grafana 支持的数据源非常多,我们这里选择 prometheus。

因为我们的 grafana 和 prometheus 在同一台机器上面,地址填写 localhost:9090 即可。

4.5、导入模板

开始我们可能不会制作模板,我们先导入一个模板,只需要输入其编号即可。更多的官方 Dashboard 请参见:

Dashboards | Grafana Labs

4.6、查看效果



相关文章
|
4月前
|
Prometheus 监控 Kubernetes
Prometheus + Grafana安装
Prometheus + Grafana安装
|
4月前
|
Prometheus Cloud Native Java
微服务框架(二十三)Prometheus + Grafana 安装、配置及使用
此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。 本文为Prometheus + Grafana 安装、配置及使用 本系列文章中所使用的框架版本为Spring ...
|
3月前
|
Prometheus 监控 Cloud Native
使用 Prometheus 配置 SLO 监控和告警
使用 Prometheus 配置 SLO 监控和告警
|
3月前
|
存储 Prometheus 监控
Prometheus Alertmanager 生产配置趟过的坑总结
Prometheus Alertmanager 生产配置趟过的坑总结
|
4月前
|
Prometheus Kubernetes Cloud Native
kubernetes安装Prometheus
##### 安装 在目标集群上,执行如下命令: ```shell kubectl apply -f https://github.com/512team/dhorse/raw/main/conf/kubernetes-prometheus.yml
|
4月前
|
Prometheus Cloud Native Linux
Linux下安装prometheus & grafana
Linux下安装prometheus & grafana
83 0
|
5月前
|
Prometheus Cloud Native 数据安全/隐私保护
Prometheus实战篇:docker安装Prometheus
Docker搭建Prometheus监控系统
|
5月前
|
Prometheus Cloud Native Unix
prometheus|云原生|kubernetes内部安装prometheus
prometheus|云原生|kubernetes内部安装prometheus
63 0
|
5月前
|
Prometheus Cloud Native 关系型数据库
prometheus|云原生|prometheus项目安装postgres-exporter监视组件的部署简介
prometheus|云原生|prometheus项目安装postgres-exporter监视组件的部署简介
88 0
|
4月前
|
编解码 Prometheus 运维
Prometheus 的监控方法论
【1月更文挑战第24天】