Prometheus : 入门

Prometheus 是一个开源的监控系统。支持灵活的查询语言(PromQL),采用 http 协议的 pull 模式拉取数据等特色使 Prometheus 即简单易懂又功能强大。node

Prometheus 的主要特色

  • 多维度数据模型linux

  • 灵活的查询语言ubuntu

  • 不依赖分布式存储,单个服务器节点是自主的vim

  • 经过 pull 方式采集时序数据浏览器

  • 能够经过中间网关进行时序列数据推送服务器

  • 经过服务发现或者静态配置来发现目标服务对象架构

  • 支持多种界面展现方案,好比 grafana 等分布式

Prometheus 由 server, client, push gateway, exporter, alertmanager 等核心组件构成。Prometheus server 主要用于抓取和存储数据。Client libraries 能够用来链接 server 并进行查询等操做。Push gateway  用于批量,短时间的监控数据的汇总节点,主要用于业务数据汇报等。不一样的 exporter 用于不一样场景下的数据收集,如收集主机信息的 node_exporter,收集 MongoDB 信息的 MongoDB exporter 等等。下图是 Prometheus 官方提供的架构图:工具

从这个架构图,咱们能够看出它的运行逻辑大概是这样的:
Prometheus server 按期从数据源拉取数据,而后将数据持久化到磁盘。Prometheus 能够配置 rules,而后定时查询数据,当条件触发的时候,会将 alert 推送到配置的 Alertmanager。Alertmanager 收到警告的时候,能够根据配置,聚合,去重,降噪,最后发送警告。同时还能够使用 API, Prometheus Console 或者 Grafana 查询和聚合数据。spa

本文将介绍在 ubuntu 16.04 系统中安装 Prometheus Server,并配置它从一台主机上拉取监控信息,而后经过 Prometheus Server 提供的简易 UI 查询数据。

在 Ubuntu 16.04 中安装 Prometheus Server

请从 Prometheus 官方下载 linux 版的二进制压缩包。注意在下载前要选择操做系统为 linux。
执行下面的命令把 prometheus server 安装到 /usr/local/share/prometheus 目录:

$ tar -xf prometheus-1.7.2.linux-amd64.tar.gz
$ sudo mv prometheus-1.7.2.linux-amd64 /usr/local/share/prometheus

理论上来讲这样就算是安装完成了,可是不管如何这都太简陋了。由于每次启动 Prometheus server 都须要手动执行命令:

$ /usr/local/share/prometheus/prometheus -config.file=/usr/local/share/prometheus/prometheus.yml

这实在是太不方便了!应该把它配置成服务,用 systemd 来管理。

先建立一个名为 prometheus 的用户:

$ sudo adduser prometheus

把目录 /usr/local/share/prometheus/ 的全部者设置为 prometheus 用户:

$ sudo chown -R prometheus:prometheus /usr/local/share/prometheus/

而后建立文件 /etc/systemd/system/prometheus.service,内容以下:

[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network.target

[Service]
User=prometheus
Restart=on-failure
WorkingDirectory=/usr/local/share/prometheus/
ExecStart=/usr/local/share/prometheus/prometheus \
          -config.file=/usr/local/share/prometheus/prometheus.yml

[Install]
WantedBy=multi-user.target

好了,如今能够经过 systemd 来控制 Prometheus 服务了,先启动服务:

$ sudo systemctl daemon-reload
$ sudo systemctl start prometheus

再把服务配置为开机时启动:

$ sudo systemctl enable prometheus

检查一下服务的状态:

$ sudo systemctl status prometheus

到此为止 Prometheus Server 已经开始运行了。接下来咱们就能够收集数据了。

使用 Node Exporter 收集主机信息

数据收集的任务由不一样的 exporter 来完成,若是要收集 linux 主机的信息,能够使用 node exporter。而后由 Prometheus Server 从 node exporter 上拉取信息。接下来咱们介绍如何安装并配置 node exporter。
请从 Prometheus 官方下载 node exporter 的二进制压缩包。执行下面的命令把 node exporter 安装到 /usr/local/share/ 目录:

$ tar -xf node_exporter-0.14.0.linux-amd64.tar.gz
$ sudo cp node_exporter-0.14.0.linux-amd64/node_exporter /usr/local/sbin/

一样的咱们把 node exporter 也配置成经过 systemd 管理。建立文件 /etc/systemd/system/node-exporter.service,内容以下:

[Unit]
Description=Prometheus Node Exporter
After=network.target

[Service]
ExecStart=/usr/local/sbin/node_exporter
User=nobody

[Install]
WantedBy=multi-user.target

执行下面的命令设置为开机启动并启动服务:

$ sudo systemctl daemon-reload
$ sudo systemctl enable node-exporter
$ sudo systemctl start node-exporter

node exporter 默认监听 9100 端口,让咱们检查一下端口的监听状况:

$ ss -tunl

Node exporter 已经能够收集主机上的信息了,接下来咱们还须要配置 Prometheus Server 从 node exporter 那里拉取数据。

配置 Prometheus 从 Node Exproter 拉取数据

Prometheus Server 能够从不一样的 exporter 上拉取数据,对于上面的 node exporter 咱们能够利用 Prometheus 的 static_configs 来拉取 node exporter 的数据。编辑 Prometheus server 的配置文件:

$ sudo vim /usr/local/share/prometheus/prometheus.yml

在 scrape_configs 中添加一个 名称为 node 的 static_configs:

- job_name: "node"
    static_configs:
      - targets: ["127.0.0.1:9100"]

注意,要把上面的 IP 地址替换为运行 node exporter 的主机的 IP。

保存文件而后重启 prometheus 服务!重启后 prometheus 服务会每隔 15s 从 node exporter 上拉取一次数据。

查询数据

Prometheus Server 提供了简易的 WebUI 能够进数据查询并展现,它默认监听的端口为 9090。接下来咱们进行一次简单的查询来验证本文安装配置的系统。
在浏览器中访问 Prometheus Server 的 9090 端口:

在下拉菜单中选择 "node_memory_Buffers",而后点击 "Execute" 按钮:

查询出来的结果略微有些粗犷,连单位都没带。请选择 "Graph" 标签页:

经过图表查看查询结果就好多了!

总结

Prometheus 是当下比较流行的开源监控工具,这里只是简单的介绍了安装过程及一个最基本的用例。可是不难看出 Prometheus 虽然支持灵活的查询语言,可是自身只支持简单的展现能力。若是要友好的展现 Prometheus 的查询结果,还须要使用更专业的展现工具 Grafana。

相关文章
相关标签/搜索