Prometheus(普罗米修斯)是由前google员工2015年正式发布的开源监控系统,采用Go语言开发。它不仅有一个很酷的名字,同时它有Google与k8s的强力支持,开源社区异常火爆。
Prometheus 的一些主要功能包括:
- 多维数据模型,时序数据由指标名称和键/值对标识
- PromQL,一种灵活的查询语言,可利用这种维度
- 不依赖分布式存储;单个服务器节点是自治的
- 时间序列收集通过 HTTP 上的拉取模型进行
- 通过中间网关支持推送时间序列
- 通过服务发现或静态配置发现目标
- 支持多种图形和仪表板模式
在 Ubuntu 上安装 Prometheus
Prometheus 在Ubuntu默认的存储库上可用;
apt-cache policy prometheus
prometheus:
Installed: (none)
Candidate: 2.15.2+ds-2
Version table:
2.15.2+ds-2 500
500 http://ke.archive.ubuntu.com/ubuntu focal/universe amd64 Packages
但是,您可能已经注意到,默认存储库提供的 Prometheus 发行版不是最新的。版本 2.51.1 是 Prometheus 的当前稳定版本。
使用预编译的二进制文件安装 Prometheus
为确保您安装的是 Prometheus 的最新版本,请使用预编译的二进制文件,该二进制文件可直接从 Prometheus 下载部分下载。
先决条件
在 Ubuntu 20.04 上使用预编译的二进制文件继续安装 Prometheus 之前;
创建 Prometheus 系统用户和组
执行以下命令,创建 Prometheus 系统用户和组;
useradd -M -r -s /bin/false prometheus
创建 Prometheus 目录
接下来,您需要创建将用于存储 Prometheus 配置文件和其他数据的目录。
mkdir /etc/prometheus /var/lib/prometheus
下载 Prometheus Binary
接下来,导航到 Prometheus 下载部分并获取最新版本的 Prometheus。您只需使用 wget 下载适用于 Linux 的 Prometheus 二进制文件,如下所示;
wget https://github.com/prometheus/prometheus/releases/download/v2.18.1/prometheus-2.18.1.linux-amd64.tar.gz
通过计算 SHA256 哈希来验证下载的二进制文件的完整性。
sha256sum prometheus-2.18.1.linux-amd64.tar.gz
5fcc35b78bd0a1b84afae6de94248a4bea3cdb4daf0d54a37b5491cb86b014d7 prometheus-2.18.1.linux-amd64.tar.gz
将生成的哈希值与下载页面上提供的哈希值进行比较。确保它们匹配。
在 Ubuntu 上安装 Prometheus
提取下载的 Prometheus 二进制文件;
tar xzf prometheus-2.18.1.linux-amd64.tar.gz
将解压的 Prometheus 存档文件夹下的 prometheus
和 promtool
二进制文件复制到目录/usr/local/bin
。
cp prometheus-2.18.1.linux-amd64/{prometheus,promtool} /usr/local/bin/
复制后,将这些二进制文件的用户和组所有权设置为 prometheus
。
chown prometheus:prometheus /usr/local/bin/{prometheus,promtool}
接下来,将consoles
和 console_libraries
目录复制到 Prometheus 配置目录/etc/prometheus
。
cp -r prometheus-2.18.1.linux-amd64/{consoles,console_libraries} /etc/prometheus/
创建 Prometheus 配置文件
提取的存档文件夹中提供了示例 Prometheus 配置文件。为了使我们的工作更轻松,只需将其复制到 Prometheus 配置目录即可。
cp prometheus-2.18.1.linux-amd64/prometheus.yml /etc/prometheus/
修改配置文件以满足您的需求。在这种情况下,我们只使用默认值。
vim /etc/prometheus/prometheus.yml
# 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).
# 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 服务器公开的时间序列数据。该作业包含一个静态配置的目标,即端口 9090 上的 localhost。
接下来,将 Prometheus 配置目录 /etc/prometheus 的用户和组所有权设置为 prometheus。
chown -R prometheus:prometheus /etc/prometheus
完成后,同样地将 Prometheus 数据目录 /var/lib/prometheus/ 的用户和组所有权设置为 prometheus。
chown prometheus:prometheus /var/lib/prometheus
运行 Prometheus
至少,普罗米修斯现在已经准备好了,可以运行了。但是,此时我们没有 Prometheus 服务配置文件,因此,我们可以如下所示运行它;
prometheus --config.file=/etc/prometheus/prometheus.yml
...
level=info ts=2020-05-29T08:32:47.317Z caller=main.go:678 msg="Starting TSDB …"
level=info ts=2020-05-29T08:32:47.320Z caller=head.go:575 component=tsdb msg="Replaying WAL, this may take awhile"
level=info ts=2020-05-29T08:32:47.320Z caller=web.go:523 component=web msg="Start listening for connections" address=0.0.0.0:9090
...
level=info ts=2020-05-29T08:32:47.326Z caller=main.go:695 msg="TSDB started"
level=info ts=2020-05-29T08:32:47.326Z caller=main.go:799 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
level=info ts=2020-05-29T08:32:47.731Z caller=main.go:827 msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml
level=info ts=2020-05-29T08:32:47.731Z caller=main.go:646 msg="Server is ready to receive web requests."
从 Web 界面访问 Prometheus
如果 Prometheus 端口正在运行,请打开防火墙 (UFW) 上的 Prometheus 端口。默认情况下,它侦听 TCP 端口 9090。
ufw allow 9090/tcp
Prometheus 现在已准备好接收 Web 请求。您可以使用地址 从浏览器访问它。http://server-IP-or-Hostname:9090
要检查节点的状态,请导航到 Status > Targets (目标)。
要查看抓取的指标,请导航到 http://<server_IP>:9090/metrics
要检查内存统计信息(例如,可用内存),请选择查询并单击“执行”,然后在控制台选项卡上查看结果。go_memstats_frees_total
创建 Prometheus systemd 服务文件
要将 Prometheus 作为服务运行,您可以创建 systemd 服务配置文件,如下所示;
vim /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus Time Series Collection and Processing Server
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries
[Install]
WantedBy=multi-user.target
接下来,重新加载 systemd 配置文件并启动并启用 Prometheus 以在系统启动时运行。
systemctl daemon-reload
systemctl enable --now prometheus
检查 Prometheus 服务的状态;
systemctl status prometheus
● prometheus.service - Prometheus Time Series Collection and Processing Server
Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2020-05-29 08:56:32 UTC; 18s ago
Main PID: 105588 (prometheus)
Tasks: 6 (limit: 2281)
Memory: 17.1M
CGroup: /system.slice/prometheus.service
└─105588 /usr/local/bin/prometheus --config.file /etc/prometheus/prometheus.yml --storage.tsdb.path /var/lib/prometheus/ --web.console.templates=/etc/promethe>
May 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.655Z caller=main.go:678 msg="Starting TSDB ..."
May 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.657Z caller=head.go:575 component=tsdb msg="Replaying WAL, this may tak>
May 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.657Z caller=web.go:523 component=web msg="Start listening for connectio>
May 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.660Z caller=head.go:624 component=tsdb msg="WAL segment loaded" segment>
May 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.660Z caller=head.go:627 component=tsdb msg="WAL replay completed" durat>
May 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.662Z caller=main.go:694 fs_type=EXT4_SUPER_MAGIC
May 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.662Z caller=main.go:695 msg="TSDB started"
May 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.662Z caller=main.go:799 msg="Loading configuration file" filename=/etc/>
May 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.710Z caller=main.go:827 msg="Completed loading of configuration file" f>
May 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.711Z caller=main.go:646 msg="Server is ready to receive web requests."