云网牛站
所在位置:首页 > Linux云服务器 > 在Debian 10/9和Ubuntu 20.04/18.04上安装和配置Prometheus Server

在Debian 10/9和Ubuntu 20.04/18.04上安装和配置Prometheus Server

2020-01-23 15:16:31作者:彭即元稿源:云网牛站

本文介绍在Debian 10/9和Ubuntu 20.04/18.04 Linux上安装和配置Prometheus Server的方法,请按照以下步骤进行操作。

 

步骤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页面中查看:https://github.com/prometheus/prometheus/,安装wget:

sudo apt update

sudo apt -y install wget curl vim

参考:在Linux系统中安装Wget、Htop、Nano、Lynx、Fzf、Pydf的方法

然后下载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.15.2 (branch: HEAD, revision: d9613e5c466c6e9de548c4dae1b9aabf9aaf7c57)

build user:root@688433cf4ff7

build date:20200106-14:50:51

go version:go1.13.5

$ promtool --version

promtool, version 2.15.2 (branch: HEAD, revision: d9613e5c466c6e9de548c4dae1b9aabf9aaf7c57)

build user:root@688433cf4ff7

build date:20200106-14:50:51

go version:go1.13.5

将Prometheus配置模板移至/etc目录:

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

还将控制台和console_libraries移至/etc/prometheus目录:

sudo mv consoles/ console_libraries/ /etc/prometheus/

 

步骤4:在Debian 10/9、Ubuntu 20.04/18.04上配置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']

可以将文件编辑为默认设置并保存。

1、创建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

2、更改目录权限

将这些目录的所有权更改为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/

3、重新加载systemd守护程序并启动服务

sudo systemctl daemon-reload

sudo systemctl start prometheus

sudo systemctl enable prometheus

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

$ systemctl status prometheus

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

sudo ufw allow 9090/tcp

参考:Debian、Ubuntu、Linux Mint系统中的UFW防火墙入门教程

通过在Web浏览器中访问Prometheus服务器IP地址/DNS名称,确认可以连接到9090端口,如下:

在Debian 10/9和Ubuntu 20.04/18.04上安装和配置Prometheus Server

初步配置完成,接下来,就可以在Prometheus服务器上配置目标了,比如参考使用Prometheus和Grafana监控Linux服务器性能的方法

 

相关主题

使用Prometheus和Grafana监控Apache Web服务器的方法

精选文章
热门文章