在Ubuntu 20.04/18.04| Debian 10/9上安装Prometheus Server

时间:2020-02-23 14:45:30  来源:igfitidea点击:

如何在Ubuntu 20.04/18.04和Debian 10/9 Linux上安装ProMetheus?
Prometheus是一种监控工具,用于在时间序列数据库中记录实时度量。
它是一个亮相软件项目,写入Go。
使用HTTP提取收集Prometheus度量标准,允许更高的性能和可扩展性。
在本教程中,我们将讨论如何在Debian&Ubuntu Linux系统上安装Prometheus Server。

Grafana是一个常用于可视化Prometheus调查的数据的工具,用于监控和分析。
它用于创建带有表示特定度量的面板的仪表板在一定时间段内表示特定度量。

第1步:创建Prometheus系统组

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

sudo groupadd --system prometheus

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

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

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

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

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

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.15.2 (branch: HEAD, revision: d9613e5c466c6e9de548c4dae1b9aabf9aaf7c57)
  build user:       Hyman@theitroad
  build date:       20170106-14:50:51
  go version:       go1.13.5
$promtool --version
promtool, version 2.15.2 (branch: HEAD, revision: d9613e5c466c6e9de548c4dae1b9aabf9aaf7c57)
  build user:       Hyman@theitroad
  build date:       20170106-14:50:51
  go version:       go1.13.5

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

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

还将控制台和Console_Libraries移动到/etc/propetheus目录:

sudo mv consoles/console_libraries//etc/prometheus/

第4步:在Debian/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 Sun 2017-01-19 14:36:08 UTC; 14s ago
     Docs: https://prometheus.io/docs/introduction/overview/
 Main PID: 1397 (prometheus)
    Tasks: 7 (limit: 2377)
   Memory: 21.7M
   CGroup: /system.slice/prometheus.service
           └─1397 /usr/local/bin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus --web.console.templates
Jan 19 14:36:08 deb10 prometheus[1397]: level=info ts=2017-01-19T14:36:08.959Z caller=main.go:334 vm_limits="(soft=unlimited, hard=unlimited)"
Jan 19 14:36:08 deb10 prometheus[1397]: level=info ts=2017-01-19T14:36:08.960Z caller=main.go:648 msg="Starting TSDB ..."
Jan 19 14:36:08 deb10 prometheus[1397]: level=info ts=2017-01-19T14:36:08.964Z caller=head.go:584 component=tsdb msg="replaying WAL, this Jan take awhil
Jan 19 14:36:08 deb10 prometheus[1397]: level=info ts=2017-01-19T14:36:08.964Z caller=web.go:506 component=web msg="Start listening for connections" add
Jan 19 14:36:08 deb10 prometheus[1397]: level=info ts=2017-01-19T14:36:08.965Z caller=head.go:632 component=tsdb msg="WAL segment loaded" segment=0 maxS
Jan 19 14:36:08 deb10 prometheus[1397]: level=info ts=2017-01-19T14:36:08.966Z caller=main.go:663 fs_type=EXT4_SUPER_MAGIC
Jan 19 14:36:08 deb10 prometheus[1397]: level=info ts=2017-01-19T14:36:08.966Z caller=main.go:664 msg="TSDB started"
Jan 19 14:36:08 deb10 prometheus[1397]: level=info ts=2017-01-19T14:36:08.966Z caller=main.go:734 msg="Loading configuration file" filename=/etc/prometh
Jan 19 14:36:08 deb10 prometheus[1397]: level=info ts=2017-01-19T14:36:08.967Z caller=main.go:762 msg="Completed loading of configuration file" filename
Jan 19 14:36:08 deb10 prometheus[1397]: level=info ts=2017-01-19T14:36:08.967Z caller=main.go:617 msg="Server is ready to receive web requests."

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

sudo ufw allow 9090/tcp

确认我们可以通过访问Web浏览器中的Prometheus Server IP地址/DNS名称来连接到端口9090。

接下来,我们将介绍在要监视的节点上安装导出商,以便在Prometheus服务器上配置目标,以便我们可以用Grafana可视化。