为什么我会选择普罗米修斯(Prometheus)?Prometheus 是按照 Google SRE 运维之道的理念构建的,具有实用性和前瞻性。同时也是基于 Go 语言开发的,性能好,安装部署也简单,甚至跨平台(包括 arm 平台)。作为对服务基础和业务监控,Prometheus 是一个非常好的选择。
咱们这里引用官方的术语:
Prometheus 是一个开源系统监控和警报工具包,它可以收集系统信息,并将其发送到一个或多个监控中心。Prometheus 将其指标收集并存储为时间序列数据,即指标信息与记录它的时间戳一起存储,以及称为标签的可选键值对。
这里引入官网的图,说明 Prometheus 的架构及其一些生态系统组件:
安装方式有很多,二进制包或者 Docker 都可以,这里我们选择二进制包。
这里俺用的是 Ubuntu 20.04,别问我为啥,主要是我内存最大的机器就是这台了(24G 内存),只不过,它是 arm64 架构的,所以下面的教程是运行在 arm64 架构的服务器上面的,当然,你也可以用本教程在 amd64 架构下安装,包括腾讯云 KVM 以及轻量应用服务器,只有一些细微的区别,咱们下面会讲到。
你可以去官网,或者 GitHub 的发布页面下载安装包,这里我下载的是 GitHub 仓库的包。
wget https://github.com/prometheus/prometheus/releases/download/v2.31.0/prometheus-2.31.0.linux-amd64.tar.gz
tar xfz prometheus-2.31.0.linux-amd64.tar.gz
sudo cp prometheus-2.31.0.linux-amd64/prometheus /usr/local/bin/
sudo cp prometheus-2.31.0.linux-amd64/promtool /usr/local/bin/
wget https://github.com/prometheus/prometheus/releases/download/v2.31.0/prometheus-2.31.0.linux-arm64.tar.gz
tar xfz prometheus-2.31.0.linux-arm64.tar.gz
sudo cp prometheus-2.31.0.linux-arm64/prometheus /usr/local/bin/
sudo cp prometheus-2.31.0.linux-arm64/promtool /usr/local/bin/
最主要的差异,就是要下载不同的包,后面的配置几乎相同
prometheus --version
执行命令,出现如下图所示,就成功了!
我们在刚才解压后的文件夹下面,可以找到一个子目录 prometheus
,然后可以找到一个配置文件 prometheus.yml
。咱们现在需要把 prometheus.yml
这个初始配置文件复制到 /etc/prometheus
目录下,然后简单配置就可以启动啦。当然,你也可以按照自己的需求来配置,具体可以参考官方的配置文档。
sudo mkdir -p /etc/prometheus
sudo cp prometheus.yml /etc/prometheus/
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
我们可以看到端口是 9090
,你可以按需求改为其它的。
接下来咱们启动看看
prometheus --config.file "/etc/prometheus/prometheus.yml"
prometool
工具来检查你的配置文件。promtool check config "/etc/prometheus/prometheus.yml"
如果输出如下提示,说明没问题了!
Checking /etc/prometheus/prometheus.yml
SUCCESS: 0 rule files found
service
文件sudo vim /etc/systemd/system/prometheus.service
[Unit]
Description=prometheus
[Service]
User=root
ExecStart=prometheus --config.file "/etc/prometheus/prometheus.yml"
Restart=on-abort
[Install]
WantedBy=multi-user.target
sudo systemctl enable prometheus
sudo systemctl start prometheus
这样便大功告成啦!
如果你的服务器是 HTTPS 的,那么需要配置 HTTPS 的证书和私钥,这里俺使用的是 Let's Encrypt 的证书,可以去官网下载。具体如何操作就不说了,如果你不会,应该去学习如何使用 Nginx,并学会配置 HTTPS 证书。
这里我放出我的配置吧,你可以根据你的需求,参考使用:
location /
{
proxy_pass http://127.0.0.1:9090;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
add_header Cache-Control no-cache;
}
Prometheus 启动监听在本地回环地址 localhost:9090 ,所以公网是无法直接访问的,我也不太建议你开放防火墙,这样能带来一定的安全保护。
这时候咱们访问域名,就能看到页面啦!
这里提示,我这不是生产环境,只是拿来练手学习,所以无所谓,生产环境不建议这么做!可以通过 ssh 端口转发方式实现远程访问。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有