在Ubuntu 22.04|20.04|18.04上安装Prometheus服务器|在Ubuntu 22.04|20.04|18.04上安装Prometheus服务器|在Ubuntu 22.04|20.04|18.04上安装Prometheus服务器|在Ubuntu 22.04|20.04|18.04上安装Prometheus服务器|
  • 业务
  • 目标
  • 支持
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容

在Ubuntu 22.04|20.04|18.04上安装Prometheus服务器|

发表 admin at 2025年2月28日
类别
  • 未分类
标签
コアサーバーV2プランご契約でドメイン更新費用が永久無料

如何在 Ubuntu 22.04/20.04/18.04 Linux 系统上安装 Prometheus? Prometheus 是一款监控工具,旨在将实时指标记录在时间序列数据库中。它是一个开源软件项目,用 Go 编写。 Prometheus 指标是使用 HTTP 拉取来收集的,从而实现更高的性能和可扩展性。在本教程中,我们将讨论如何在 22.04|20.04|18.04 Linux 系统上安装 Prometheus Server。

使 Prometheus 成为完整监控工具的其他工具包括:

  • 导出器:这些库可帮助将第三方系统中的指标导出为 Prometheus 指标。
  • PromQL:Prometheus 查询语言,可让您过滤多维时间序列数据。

Grafana 是一种常用来可视化 Prometheus 轮询的数据以进行监控和分析的工具。它用于创建仪表板,其中的面板代表一段时间内的特定指标。

第1步:创建Prometheus系统组

我们首先创建 Prometheus 系统用户和组。

sudo groupadd --system prometheus

ID < 1000 的组是系统组。添加系统组后,创建 Prometheus 系统用户并分配创建的主要组。

sudo useradd -s /sbin/nologin --system -g prometheus prometheus

第 2 步:创建数据和配置目录

Prometheus 需要一个目录来存储其数据。我们将在/var/lib/prometheus下创建它。

sudo mkdir /var/lib/prometheus

Prometheus 主配置文件目录是/etc/prometheus/。它将有一些子目录:

for i in rules rules.d files_sd; do sudo mkdir -p /etc/prometheus/${i}; done

第 3 步:下载 Prometheus 文件

我们需要下载最新版本的 Prometheus 存档并将其解压以获取二进制文件。您可以从 Prometheus 版本 Github 页面查看版本。

安装 wget。

sudo apt update
sudo apt -y install wget curl vim

然后下载 Prometheus 的最新二进制存档。

mkdir -p /tmp/prometheus && cd /tmp/prometheus
curl -s https://api.github.com/repos/prometheus/prometheus/releases/latest | grep browser_download_url | grep linux-amd64 | cut -d '"' -f 4 | wget -qi -

提取文件:

tar xvf prometheus*.tar.gz
cd prometheus*/

将二进制文件移动到 /usr/local/bin/ 目录。

sudo mv prometheus promtool /usr/local/bin/

检查安装的版本:

$ prometheus --version
prometheus, version 2.46.0 (branch: HEAD, revision: cbb69e51423565ec40f46e74f4ff2dbb3b7fb4f0)
  build user:       root@42454fc0f41e
  build date:       20230725-12:31:24
  go version:       go1.20.6
  platform:         linux/amd64
  tags:             netgo,builtinassets,stringlabels

$ promtool --version
promtool, version 2.46.0 (branch: HEAD, revision: cbb69e51423565ec40f46e74f4ff2dbb3b7fb4f0)
  build user:       root@42454fc0f41e
  build date:       20230725-12:31:24
  go version:       go1.20.6
  platform:         linux/amd64
  tags:             netgo,builtinassets,stringlabels

将 Prometheus 配置模板移动到 /etc 目录。

sudo mv prometheus.yml /etc/prometheus/prometheus.yml

同时将 console 和 console_libraries 移动到 /etc/prometheus 目录:

sudo mv consoles/ console_libraries/ /etc/prometheus/
cd $HOME

步骤 4:在 Ubuntu 上配置 Prometheus

创建或编辑 Prometheus 的配置文件 - /etc/prometheus/prometheus.yml。

sudo 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 systemd 服务单元文件

为了能够使用 systemd 管理 Prometheus 服务,您需要显式定义此单元文件。

sudo tee /etc/systemd/system/prometheus.service<<EOF
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/docs/introduction/overview/
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=prometheus
Group=prometheus
ExecReload=/bin/kill -HUP \$MAINPID
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 \
  --web.listen-address=0.0.0.0:9090 \
  --web.external-url=

SyslogIdentifier=prometheus
Restart=always

[Install]
WantedBy=multi-user.target
EOF

更改目录权限。

将这些目录的所有权更改为 Prometheus 用户和组。

for i in rules rules.d files_sd; do sudo chown -R prometheus:prometheus /etc/prometheus/${i}; done
for i in rules rules.d files_sd; do sudo chmod -R 775 /etc/prometheus/${i}; done
sudo chown -R prometheus:prometheus /var/lib/prometheus/

重新加载 systemd 守护进程并启动服务:

sudo systemctl daemon-reload
sudo systemctl start prometheus
sudo systemctl enable prometheus

使用 systemctl status prometheus 命令检查状态:

$ systemctl status prometheus
● prometheus.service - Prometheus
     Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2023-08-16 10:19:07 UTC; 7s ago
       Docs: https://prometheus.io/docs/introduction/overview/
   Main PID: 21473 (prometheus)
      Tasks: 7 (limit: 4523)
     Memory: 14.7M
        CPU: 95ms
     CGroup: /system.slice/prometheus.service
             └─21473 /usr/local/bin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus --web.console.templates=/etc/prometheus/consoles --web.console.li>

Aug 16 10:19:07 jammy prometheus[21473]: ts=2023-08-16T10:19:07.465Z caller=head.go:676 level=info component=tsdb msg="On-disk memory mappable chunks replay completed" duration=14.924µs
Aug 16 10:19:07 jammy prometheus[21473]: ts=2023-08-16T10:19:07.465Z caller=head.go:684 level=info component=tsdb msg="Replaying WAL, this may take a while"
Aug 16 10:19:07 jammy prometheus[21473]: ts=2023-08-16T10:19:07.466Z caller=head.go:755 level=info component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0
Aug 16 10:19:07 jammy prometheus[21473]: ts=2023-08-16T10:19:07.466Z caller=head.go:792 level=info component=tsdb msg="WAL replay completed" checkpoint_replay_duration=79.833µs wal_replay_duration=>
Aug 16 10:19:07 jammy prometheus[21473]: ts=2023-08-16T10:19:07.469Z caller=main.go:1047 level=info fs_type=EXT4_SUPER_MAGIC
Aug 16 10:19:07 jammy prometheus[21473]: ts=2023-08-16T10:19:07.469Z caller=main.go:1050 level=info msg="TSDB started"
Aug 16 10:19:07 jammy prometheus[21473]: ts=2023-08-16T10:19:07.469Z caller=main.go:1231 level=info msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
Aug 16 10:19:07 jammy prometheus[21473]: ts=2023-08-16T10:19:07.470Z caller=main.go:1268 level=info msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml totalDurati>
Aug 16 10:19:07 jammy prometheus[21473]: ts=2023-08-16T10:19:07.471Z caller=main.go:1011 level=info msg="Server is ready to receive web requests."
Aug 16 10:19:07 jammy prometheus[21473]: ts=2023-08-16T10:19:07.471Z caller=manager.go:1009 level=info component="rule manager" msg="Starting rule manager..."

如果您的服务器正在运行防火墙服务,则需要打开端口9090。

sudo ufw allow 9090/tcp

通过在网络浏览器中访问 Prometheus 服务器 IP 地址/DNS 名称,确认您可以连接到端口 9090。

使用密码保护 Web 控制台

请参阅以下链接中的指南来配置 Prometheus 基本 http 身份验证:

  • 使用基本密码身份验证保护 Prometheus 服务器

接下来,我们将介绍在要监控的节点上安装导出器以及在 Prometheus 服务器上配置目标,以便我们可以废弃指标并使用 Grafana 进行可视化。

Prometheus 监控指南

  • 使用 Prometheus 和 Grafana 监控 Ceph 集群
  • 使用 Prometheus 和 Grafana 监控 Apache Web 服务器
  • 如何在 5 分钟内使用 Prometheus 和 Grafana 监控 Linux 服务器性能
  • 如何使用 Prometheus 和 Grafana 监控 BIND DNS 服务器
  • 如何在 5 分钟内使用 Prometheus 和 Grafana 监控 Redis 服务器
  • 五分钟内使用 Prometheus 监控 MySQL/MariaDB
  • 使用 Prometheus 监控 Etcd 集群
©2015-2025 Norria support@alaica.com