Prometheus_basic_auth

news/2025/9/26 16:58:26/文章来源:https://www.cnblogs.com/aistack/p/19113821

安装好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 即可。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/918554.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

dify二次开发之数据库表设计

Dify 数据库表结构文档 概述 Dify 项目使用 SQLAlchemy 作为 ORM 框架,数据库表结构定义在 [api/models](file:///Users/chunlin/Desktop/dify_redevelop/dify-1.8.1/api/models) 目录下的 Python 文件中。本文档将详…

美国股票市场数据API的完整对接指南,包含NYSE、NASDAQ等主要交易所的实时行情、历史数据、公司信息等核心功能

一、接口概览 1.1 支持交易所交易所代码 交易所名称 覆盖股票数量NYSE 纽约证券交易所 2800+NASDAQ 纳斯达克交易所 3300+AMEX 美国证券交易所 500+1.2 数据特性实时行情:毫秒级延迟 历史数据:支持最长20年历史K线 基…

深度学习(十):逻辑回归的代价函数 - 教程

深度学习(十):逻辑回归的代价函数 - 教程2025-09-26 16:51 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: bl…

wordpress知识管理系统seo wordpress 插件

第1题:【 单选题】 执行以下脚本后舞台上的角色将 ?( ) A:先克隆自身,克隆体出现后被删除 B:先克隆自身,克隆体出现后删除本体 C:克隆出自身后本体与克隆体同时被删除 D:克隆出自身后本体与克隆体被不会被删除 【正确答案】: A 【试题解析】 : 第2题:【 单选题】…

Spring Boot启动报错:Failed to configure a DataSource 全面解析与解决方案 - 教程

Spring Boot启动报错:Failed to configure a DataSource 全面解析与解决方案 - 教程pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; f…

深入理解 CSS 浮动:从原理到实战应用​ - space

深入理解 CSS 浮动:从原理到实战应用 ** 在 CSS 布局的发展历程中,浮动(Float)曾是实现页面布局的核心技术之一。尽管如今 Flexbox 和 Grid 布局已成为主流,但浮动依然在特定场景中发挥着重要作用,尤其是在处理图…

Winform程序中将datagridview导出到excel (推荐)

使用DEEPSEEK推荐的导出方法,好的很! 用前要用nuget 安装 ClosedXML 插件包using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.T…

第二章Pycharm和Jupiter

第二章Pycharm和Jupiter 一.Pycharm 在python console里的操作如下:右侧会像matlab一样显示变量二.Jupyter 1.下载Jupyter所需要的包 先进入环境(激活) conda activate pytorchnow conda install nb_conda2.进入Jup…

[吾爱原创] 【小众应用】鼠标键盘操作可视化设备v1.1 可用于教育培训/演示/远程辅助等

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

微服务基础3-服务保护与分布式事务 - 详解

微服务基础3-服务保护与分布式事务 - 详解pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "…

网站首页列表布局设计淘宝关键词指数查询

使用微软的com组件Microsoft.office.Interop.Excel读写Excel文件虽然可用,但是列多、行多的时候速度很慢,之前测试过Sylvan.Data.Excel包的用法,如果只是读取Excel文件内容的话,还可以使用ExcelDataReader包,后者是C#开…

使用parted命令扩容vm内磁盘分区大小

假如已经在VMware的设置里扩容了磁盘大小,接下来需要在系统内调整大小,按如下操作: 0、查看磁盘情况: root@ubuntu:~# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sda 8:0 0 160G 0 disk ├─s…

pyinstaller

pyinstaller --onefile --windowed -i binglish.ico --add-data "binglish.ico;." --hidden-import "pystray._win32" binglish.py 使用 PyInstaller 工具将 Python 脚本 binglish.py 打包成一个独…

wordpress 子网站重命名东莞毛织厂家东莞网站建设

🎈 作者:互联网-小啊宇 🎈 简介: CSDN 运维领域创作者、阿里云专家博主。目前从事 Kubernetes运维相关工作,擅长Linux系统运维、开源监控软件维护、Kubernetes容器技术、CI/CD持续集成、自动化运维、开源软件部署维护…

帮建网站wordpress微信个人支付

介绍: Python推导式是一种简洁、高效的创建列表、字典或集合的方法。它使用一种类似于数学公式的语法,通过一个表达式和一个循环来生成一个新的数据结构。 以下是一些常见的Python推导式: 列表推导式(List Comprehension&#xf…

国外网站配色平凉哪有做网站的

【为什么要用多线程?】 传统的图形用户界面应用程序都仅仅有一个运行线程,而且一次仅仅运行一个操作。假设用户从用户界面中调用一个比較耗时的操作,当该操作正在运行时,用户界面一般会冻结而不再响应。这个问题能够用事件处理和多…

吴江区建设局网站中装建设公司待遇好吗

容器分为三类,顺序容器,关联容器和适配器。顺序容器又分为连续的容器(vector,array),顺序容器中的离散容器(list,slist,forward_list),离连形的de…

pc网站 手机网站 微信长春公司做网站

笔记整理:刘尧锟,天津大学硕士链接:https://dl.acm.org/doi/pdf/10.1145/3404835.3462900动机面对大量的机器学习(ML)方法,为给定的数据集和任务选择合适的方法是一个挑战。一般来说,ML方法或数…

wordpress 双语站点自己使用原生php做网站性能

目录 1.题目描述 一 2.解题想法图解 2.1直接解 2.2巧解 3.题目描述二 3.1.1思路1 3.1.2 思路2 4.结语 1.题目描述 一 实现现一个函数,可以左旋字符串中的k个字符。 例如: ABCD左旋一个字符得到BCDA ABCD左旋两个字符得到CDAB 2.解题想法图解 2.…