Prometheus 教程

Linux 下安装 Prometheus

准备环境

下载 node_exporter 安装包

使用 wget 命令下载 node_exporter 安装包(https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gz),如下:

hxstrive@localhost:~$ wget https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gz
--2024-03-20 23:08:41--  https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gz
Resolving github.com (github.com)... 20.205.243.166
Connecting to github.com (github.com)|20.205.243.166|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/9524057/87c9e928-5869-11e8-9817-8e2ec2a2c679?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCODYLSA53PQK4ZA%2F20240320%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240320T150816Z&X-Amz-Expires=300&X-Amz-Signature=746c5f1f3208eb81989da73f6edcb0fbdff1ebbeae86b2ecb5acb8836d0e0109&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=9524057&response-content-disposition=attachment%3B%20filename%3Dnode_exporter-0.16.0.linux-amd64.tar.gz&response-content-type=application%2Foctet-stream [following]
--2024-03-20 23:08:41--  https://objects.githubusercontent.com/github-production-release-asset-2e65be/9524057/87c9e928-5869-11e8-9817-8e2ec2a2c679?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCODYLSA53PQK4ZA%2F20240320%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240320T150816Z&X-Amz-Expires=300&X-Amz-Signature=746c5f1f3208eb81989da73f6edcb0fbdff1ebbeae86b2ecb5acb8836d0e0109&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=9524057&response-content-disposition=attachment%3B%20filename%3Dnode_exporter-0.16.0.linux-amd64.tar.gz&response-content-type=application%2Foctet-stream
Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.111.133, 185.199.108.133, 185.199.109.133, ...
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.111.133|:443... failed: Connection refused.
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.108.133|:443... failed: Connection refused.
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.109.133|:443... failed: Connection refused.
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.110.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5276312 (5.0M) [application/octet-stream]
Saving to: ‘node_exporter-0.16.0.linux-amd64.tar.gz’

node_exporter-0.16.0.linux-amd64.tar.gz                        100%[====================================================================================================================================================>]   5.03M  15.7KB/s    in 7m 5s   

2024-03-20 23:16:51 (12.1 KB/s) - ‘node_exporter-0.16.0.linux-amd64.tar.gz’ saved [5276312/5276312]

下载 Go 语言安装包

使用 wget 命令下载 go 语言安装包(https://dl.google.com/go/go1.21.8.linux-amd64.tar.gz),如下:

hxstrive@localhost:~$ wget https://dl.google.com/go/go1.21.8.linux-amd64.tar.gz
--2024-03-20 22:34:19--  https://dl.google.com/go/go1.21.8.linux-amd64.tar.gz
Resolving dl.google.com (dl.google.com)... 120.253.253.225
Connecting to dl.google.com (dl.google.com)|120.253.253.225|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 66647874 (64M) [application/x-gzip]
Saving to: ‘go1.21.8.linux-amd64.tar.gz’

go1.21.8.linux-amd64.tar.gz                                    100%[====================================================================================================================================================>]  63.56M  29.1MB/s    in 2.2s    

2024-03-20 22:34:21 (29.1 MB/s) - ‘go1.21.8.linux-amd64.tar.gz’ saved [66647874/66647874]

下载 Prometheus 安装包

使用 wget 命令下载 prometheus 安装包(https://github.com/prometheus/prometheus/releases/download/v2.3.2/prometheus-2.3.2.linux-amd64.tar.gz),如下:

hxstrive@localhost:~$ wget https://github.com/prometheus/prometheus/releases/download/v2.3.2/prometheus-2.3.2.linux-amd64.tar.gz
--2024-03-20 20:58:40--  https://github.com/prometheus/prometheus/releases/download/v2.3.2/prometheus-2.3.2.linux-amd64.tar.gz
Resolving github.com (github.com)... 20.205.243.166
Connecting to github.com (github.com)|20.205.243.166|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/6838921/1b223630-85f6-11e8-8690-ed86652bcdc7?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCODYLSA53PQK4ZA%2F20240320%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240320T125841Z&X-Amz-Expires=300&X-Amz-Signature=63630c0be7464bc413d62aee697a9484b667810f07cd0ba6ef2951197c9acbb8&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=6838921&response-content-disposition=attachment%3B%20filename%3Dprometheus-2.3.2.linux-amd64.tar.gz&response-content-type=application%2Foctet-stream [following]
--2024-03-20 20:58:41--  https://objects.githubusercontent.com/github-production-release-asset-2e65be/6838921/1b223630-85f6-11e8-8690-ed86652bcdc7?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCODYLSA53PQK4ZA%2F20240320%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240320T125841Z&X-Amz-Expires=300&X-Amz-Signature=63630c0be7464bc413d62aee697a9484b667810f07cd0ba6ef2951197c9acbb8&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=6838921&response-content-disposition=attachment%3B%20filename%3Dprometheus-2.3.2.linux-amd64.tar.gz&response-content-type=application%2Foctet-stream
Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.111.133, 185.199.110.133, 185.199.109.133, ...
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.111.133|:443... failed: Connection refused.
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.110.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 27404353 (26M) [application/octet-stream]
Saving to: ‘prometheus-2.3.2.linux-amd64.tar.gz’

prometheus-2.3.2.linux-amd64.tar.gz           100%[==============================================================================================>]  26.13M  10.8KB/s    in 28m 33s 

2024-03-20 21:27:37 (15.6 KB/s) - ‘prometheus-2.3.2.linux-amd64.tar.gz’ saved [27404353/27404353]

[1]+  Exit 6                  wget https://objects.githubusercontent.com/github-production-release-asset-2e65be/6838921/617e9570-06ba-4b75-a732-3f9533223331?X-Amz-Algorithm=AWS4-HMAC-SHA256

安装 Go 语言

(1)使用 tar 命令解压 Go 语言安装包:

# 解压 go 语言到 /usr/local 目录
hxstrive@localhost:~$ sudo tar -C /usr/local -xzf go1.21.8.linux-amd64.tar.gz
[sudo] password for hxstrive: 

# 查看 go 语言目录
hxstrive@localhost:~$ ll /usr/local/go/
total 80
drwxr-xr-x 10 root root  4096  3月  1 02:18 ./
drwxr-xr-x 12 root root  4096  3月 20 22:42 ../
drwxr-xr-x  2 root root  4096  3月  1 02:18 api/
drwxr-xr-x  2 root root  4096  3月  1 02:18 bin/
-rw-r--r--  1 root root    52  3月  1 02:18 codereview.cfg
-rw-r--r--  1 root root  1337  3月  1 02:18 CONTRIBUTING.md
drwxr-xr-x  2 root root  4096  3月  1 02:18 doc/
-rw-r--r--  1 root root   505  3月  1 02:18 go.env
drwxr-xr-x  3 root root  4096  3月  1 02:18 lib/
-rw-r--r--  1 root root  1479  3月  1 02:18 LICENSE
drwxr-xr-x  8 root root  4096  3月  1 02:18 misc/
-rw-r--r--  1 root root  1303  3月  1 02:18 PATENTS
drwxr-xr-x  4 root root  4096  3月  1 02:18 pkg/
-rw-r--r--  1 root root  1455  3月  1 02:18 README.md
-rw-r--r--  1 root root   419  3月  1 02:18 SECURITY.md
drwxr-xr-x 52 root root  4096  3月  1 02:18 src/
drwxr-xr-x 26 root root 12288  3月  1 02:18 test/
-rw-r--r--  1 root root    35  3月  1 02:18 VERSION

echo "export PATH=$PATH:/usr/local/go/bin" >>  /etc/profile
source  /etc/profile

(2)将 Go 添加到环境变量,添加到当前用户的 .profile 文件:

hxstrive@localhost:~$ echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.profile

(3)查看 .profile 的内容:

hxstrive@localhost:~$ cat ~/.profile 
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
	. "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/usr/local/go/bin

(4)使用 source 重新加载 /etc/profile 文件:

hxstrive@localhost:~$ source ~/.profile

(5)验证 go 安装是否正确:

hxstrive@localhost:~$ go version
go version go1.21.8 linux/amd64

上述成功输出了 go 的版本信息。

安装 node_exporter

(1)解压 node_exporter 安装包:

# 解压到 /usr/local 目录
hxstrive@localhost:~$ sudo tar -zxf node_exporter-0.16.0.linux-amd64.tar.gz -C /usr/local/
[sudo] password for hxstrive: 

# 查看目录内容
hxstrive@localhost:~$ ls /usr/local/node_exporter-0.16.0.linux-amd64/
LICENSE  node_exporter  NOTICE

(2)重命名目录,方便之后运行:

hxstrive@localhost:~$ sudo mv /usr/local/node_exporter-0.16.0.linux-amd64/ /usr/local/node_exporter
hxstrive@localhost:~$ ls /usr/local/node_exporter/
LICENSE  node_exporter  NOTICE

(3)后台运行 node_exporter:

hxstrive@localhost:~$ /usr/local/node_exporter/node_exporter &
[1] 66058
hxstrive@localhost:~$ INFO[0000] Starting node_exporter (version=0.16.0, branch=HEAD, revision=d42bd70f4363dced6b77d8fc311ea57b63387e4f)  source="node_exporter.go:82"
INFO[0000] Build context (go=go1.9.6, user=root@a67a9bc13a69, date=20180515-15:52:42)  source="node_exporter.go:83"
...
INFO[0000]  - zfs                                        source="node_exporter.go:97"
INFO[0000] Listening on :9100                            source="node_exporter.go:111"

(4)使用浏览器访问 http://localhost:9100 地址,验证服务是否启动成功:

Linux 下安装 Prometheus

如果你能看见上面界面,说明安装成功了。

安装 Prometheus

(1)解压 Prometheus 安装包:

# 解压文件
hxstrive@localhost:~$ sudo tar -C /usr/local -zxf prometheus-2.3.2.linux-amd64.tar.gz 

# 查看解压后的内容
hxstrive@localhost:~$ ls /usr/local/prometheus-2.3.2.linux-amd64/
console_libraries  consoles  LICENSE  NOTICE  prometheus  prometheus.yml  promtool

(2)建立 prometheus 的软链接:

# 创建软链接
hxstrive@localhost:~$ sudo ln -sv /usr/local/prometheus-2.3.2.linux-amd64/ /usr/local/Prometheus
'/usr/local/Prometheus' -> '/usr/local/prometheus-2.3.2.linux-amd64/'

# 查看软链接目录内容
hxstrive@localhost:~$ ls /usr/local/Prometheus
console_libraries  consoles  LICENSE  NOTICE  prometheus  prometheus.yml  promtool

编辑 prometheus.yml 配置

(1)使用 vim 编辑 prometheus.yml 配置文件:

hxstrive@localhost:~$ vim /usr/local/Prometheus/prometheus.yml

(2)配置内容如下:

# 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']
  # node_exporter
  - job_name: 'node_exporter'
    static_configs:
    - targets: ['localhost:9100']

上面配置,监听地址为本机 9090 端口 “localhost:9090”。并且还将 node_exporter 添加在配置末尾,监听本机 9100 端口。

后台运行 Prometheus

使用 & 命令在后台运行 prometheus,命令如下:

hxstrive@localhost:~$ sudo /usr/local/Prometheus/prometheus --config.file=/usr/local/Prometheus/prometheus.yml &
[2] 66610
hxstrive@localhost:~$ level=info ts=2024-03-20T15:27:25.018223319Z caller=main.go:222 msg="Starting Prometheus" version="(version=2.3.2, branch=HEAD, revision=71af5e29e815795e9dd14742ee7725682fa14b7b)"
level=info ts=2024-03-20T15:27:25.018329967Z caller=main.go:223 build_context="(go=go1.10.3, user=root@5258e0bd9cc1, date=20180712-14:02:52)"
level=info ts=2024-03-20T15:27:25.018369154Z caller=main.go:224 host_details="(Linux 5.13.0-37-generic #42-Ubuntu SMP Tue Mar 15 14:34:06 UTC 2022 x86_64 localhost (none))"
level=info ts=2024-03-20T15:27:25.018395238Z caller=main.go:225 fd_limits="(soft=1024, hard=1048576)"
level=info ts=2024-03-20T15:27:25.019214846Z caller=main.go:533 msg="Starting TSDB ..."
level=info ts=2024-03-20T15:27:25.019285698Z caller=web.go:415 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2024-03-20T15:27:25.042628666Z caller=main.go:543 msg="TSDB started"
level=info ts=2024-03-20T15:27:25.042796839Z caller=main.go:603 msg="Loading configuration file" filename=/usr/local/Prometheus/prometheus.yml
level=info ts=2024-03-20T15:27:25.045132656Z caller=main.go:629 msg="Completed loading of configuration file" filename=/usr/local/Prometheus/prometheus.yml
level=info ts=2024-03-20T15:27:25.045187237Z caller=main.go:502 msg="Server is ready to receive web requests."
...

然后,使用浏览器访问 http://localhost:9090 地址,如果能看见下面界面,则安装成功:

Linux 下安装 Prometheus

Linux 下安装 Prometheus

说说我的看法
全部评论(
没有评论
关于
本网站专注于 Java、数据库(MySQL、Oracle)、Linux、软件架构及大数据等多领域技术知识分享。涵盖丰富的原创与精选技术文章,助力技术传播与交流。无论是技术新手渴望入门,还是资深开发者寻求进阶,这里都能为您提供深度见解与实用经验,让复杂编码变得轻松易懂,携手共赴技术提升新高度。如有侵权,请来信告知:hxstrive@outlook.com
公众号