尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

Prometheus_basic_auth

Prometheus_basic_auth
📅 发布时间:2026/6/19 21:27:54

安装好Prometheus后发现我们打开地址便可访问无安全性,此时我们想到官方指南中的basic_auth。

参考指南:Securing Prometheus API and UI endpoints using basic auth | Prometheus

Prometheus配置基本授权

假设您希望要求访问 Prometheus 实例的所有用户提供用户名和密码。对于此示例,请用作用户名并选择您想要的任何密码。

生成密码

$ htpasswd -nBC 12 '' | tr -d ':\n'   
New password: 
Re-type new password:
$2b$12$hNf2lSsxfm0.i4a.1kVpSOVyBCfIB51VRjgBUyv6kdnyTlgWj81Ay

在此示例中,我使用“test”作为密码。

将该密码保存在某个地方,我们将在接下来的步骤中使用它!

创建web.yml

让我们创建一个web.yml文件, 内容如下:

basic_auth_users:admin: $2b$12$hNf2lSsxfm0.i4a.1kVpSOVyBCfIB51VRjgBUyv6kdnyTlgWj81Ay

您可以使用以下命令验证该文件 promtool check web-config web.yml

$ promtool check web-config web.yml
web.yml SUCCESS

您可以向文件添加多个用户。

启动 Prometheus

您可以使用 Web 配置文件启动 prometheus,如下所示:

$ prometheus --web.config.file=web.yml

修改启动配置文件,添加参数 --web.config.file=web.yml 如systemctl文件所示:

$ vim prometheus.yml
[Unit]
Description="prometheus"
Documentation=https://prometheus.io/
After=network.target[Service]
Type=simple
ExecStart=/opt/prometheus/prometheus  --config.file=/opt/prometheus/prometheus.yml --web.config.file=/opt/prometheus/web.yml --storage.tsdb.path=/opt/data --web.enable-lifecycle --enable-feature=remote-write-receiver --query.lookback-delta=2m 
Restart=on-failure
SuccessExitStatus=0
LimitNOFILE=65536
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=prometheus[Install]
WantedBy=multi-user.target

测试

您可以使用 cURL 与您的设置进行交互。尝试以下请求:

curl --head http://localhost:9090/graph

这将返回响应,因为您未能提供有效的用户名和密码。401 Unauthorized

要使用基本身份验证(例如端点)成功访问 Prometheus 端点,请使用标志提供正确的用户名,并在出现提示时提供密码:/metrics -u

curl -u admin http://localhost:9090/metrics
Enter host password for user 'admin':

这应该返回 Prometheus 指标输出,它应该如下所示:

# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0.0001343
go_gc_duration_seconds{quantile="0.25"} 0.0002032
go_gc_duration_seconds{quantile="0.5"} 0.0004485
...

Node_exporter基本授权

因安全需要,现在对 node_exporter 进行配置以支持 TLS 和 Basic Auth。

Node_exporter 1.0 以上版本才支持 TLS 和 Basic Auth

Node_exporter 配置

准备工作

prometheus/node_exporter

下载安装

$ wget https://github.com/prometheus/node_exporter/releases/download/v1.9.1/node_exporter-1.9.1.linux-amd64.tar.gz
$ tar xf node_exporter-1.9.1.linux-amd64.tar.gz
$ mv node_exporter-1.9.1.linux-amd64 /usr/local/node_exporter

生成TLS证书

$ openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout node_exporter.key -out node_exporter.crt -subj "/C=CN/ST=Beijing/L=Beijing/O=Moelove.info/CN=localhost"
Generating a RSA private key
...................+++++
.........................................................................................................................................................................................................................................................................+++++
writing new private key to 'node_exporter.key'
-----$ ll
total 16
drwxr-xr-x  2 root root 4096 Dec  1 14:58 ./
drwx------ 27 root root 4096 Dec  1 14:58 ../
-rw-r--r--  1 root root 1310 Dec  1 14:58 node_exporter.crt
-rw-------  1 root root 1704 Dec  1 14:58 node_exporter.key

通过上面的步骤,我们得到了 node_exporter.crt 和node_exporter.key 这两个文件。

basic auth 认证生成

安装 htpasswd 来生成密码 hash

$ apt install apache2-utils -y           #Ubuntu$ yum install httpd-tools -y             #centos

在 Node_exporter 目录下执行

$ htpasswd -nBC 12 '' | tr -d ':\n'   
New password: 
Re-type new password:
$2b$12$hNf2lSsxfm0.i4a.1kVpSOVyBCfIB51VRjgBUyv6kdnyTlgWj81Ay

在此示例中,我使用“test”作为密码。

修改配置

将前面生成的 node_exporter.crt 和node_exporter.key 文件复制到 Node_exporter 解压目录下。

$ cp /usr/local/node_exporter/node_exporter.* .
$ ll
total 19352
drwxr-xr-x 2 root root     4096 Dec  1 15:12 ./
drwxr-xr-x 5 root root     4096 Dec  1 15:07 ../
-rw-r--r-- 1 3434 3434    11357 Nov 30 03:05 LICENSE
-rw-r--r-- 1 3434 3434      463 Nov 30 03:05 NOTICE
-rwxr-xr-x 1 3434 3434 19779640 Nov 30 02:59 node_exporter*
-rw-r--r-- 1 root root     1310 Dec  1 15:12 node_exporter.crt
-rw------- 1 root root     1704 Dec  1 15:12 node_exporter.key

编写配置文件,并保存为 config.yaml

tls_server_config:cert_file: node_exporter.crtkey_file: node_exporter.key
basic_auth_users:# 当前设置的用户名为 root , 可以设置多个root: $2b$12$hNf2lSsxfm0.i4a.1kVpSOVyBCfIB51VRjgBUyv6kdnyTlgWj81Ay

启动

nohup ./node_exporter --web.listen-address=:9100 --web.config.file=config.yaml &

验证

$ curl http://localhost:39100/metrics 
Client sent an HTTP request to an HTTPS server.
root@zabbix:/opt/node_exporter# curl https://localhost:39100/metrics 
curl: (60) SSL certificate problem: self signed certificate
More details here: https://curl.haxx.se/docs/sslcerts.htmlcurl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.

可以看到不能直接访问了,下面带上证书及用户密码再次测试

$ curl -u prometheus -s  --cacert node_exporter.crt https://localhost:39100/metrics |grep node_exporter_build_info
Enter host password for user 'prometheus':
# HELP node_exporter_build_info A metric with a constant '1' value labeled by version, revision, branch, and goversion from which node_exporter was built.
# TYPE node_exporter_build_info gauge
node_exporter_build_info{branch="HEAD",goversion="go1.19.3",revision="1b48970ffcf5630534fb00bb0687d73c66d1c959",version="1.5.0"} 1

Prometheus 配置

下载最新版解压,并将前面生成的 node_exporter.crt 和node_exporter.key 文件复制到该目录下。在 prometheus.yml 加入如下内容:

global:scrape_interval:     15s evaluation_interval: 15s scrape_configs:- job_name: 'prometheus'static_configs:- targets: ['localhost:9090']- job_name: 'node_exporter'scheme: httpstls_config:ca_file: node_exporter.crtinsecure_skip_verify: truebasic_auth:username: rootpassword: teststatic_configs:- targets: ['localhost:9100']

启动 Prometheus 即可。

相关新闻

  • JAVA变量
  • Winform程序中将datagridview导出到excel (推荐)
  • 第二章Pycharm和Jupiter

最新新闻

  • QMCDecode解决方案:解锁QQ音乐加密格式,实现音频文件自由播放
  • SCMP报考条件详解——学历和工作经验要求 - 众智商学院课程中心
  • DeepSeek V4硬件适配实录:昇腾910B与H100双轨训练逻辑
  • SAP BOM查询实战:从正查到反查的完整指南
  • 【2026年6月】热水离心泵厂家推荐指南 - 多才菠萝
  • Python图片压缩方法全解:从入门到进阶

日新闻

  • 5分钟掌握Python进化算法:Geatpy高性能优化工具完全指南
  • Microchip 24AA044 EEPROM选型与应用全指南:从参数解析到实战编程
  • 华为的鸿蒙到底有多牛?为什么称作遥遥领先?

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号