prometheus设置用户名密码

说明

在本指南中,您将用户名和散列密码存储在web.yml文件,使用该文件中的凭证对访问prometheus HTTP端点的用户进行身份验证根据参数–web.config.file启动prometheus.

散列密码

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

首先,生成一个bcrypt密码的哈希。为了生成散列密码,我们将使用python3-bcrypt。(安装python3环境)

让我们通过运行来安装它sudo dnf install python3-bcrypt.x86_64

下面是一个python脚本,它使用python3-bcrypt提示输入密码并对其进行哈希处理:

import getpass

import bcrypt

password = getpass.getpass("password: ")

hashed_password = bcrypt.hashpw(password.encode(“utf-8”), bcrypt.gensalt())

print(hashed_password.decode())

将脚本另存为gen-pass.py并运行它:

$ python3 gen-pass.py

这应该会提示您输入密码:

password:

$2b 12 12 12hNf2lSsxfm0.i4a.1kVpSOVyBCfIB51VRjgBUyv6kdnyTlgWj81Ay

在这个例子中,我使用“test”作为密码。

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

创建web.yml

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

basic_auth_users:

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

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

$ promtool check web-config web.yml

web.yml SUCCESS

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

启动普罗米修斯

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

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

测试

您可以使用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 metrics输出,类似于:

# 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值