思路:
将密码认证添加到 TiDB 的 Prometheus 实例中,可以按照以下步骤操作:
步骤一:生成 bcrypt 哈希密码
-
安装 python3-bcrypt(假设你使用的是 Debian 系统):
sudo apt install python3-bcrypt
-
创建并运行 gen-pass.py 脚本生成密码哈希:
创建 gen-pass.py 脚本并运行它:
import getpass import bcrypt password = getpass.getpass("password: ") hashed_password = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()) print(hashed_password.decode())
运行脚本:
python3 gen-pass.py
输入你的密码(例如 “test”)后,脚本会输出类似于
$2b$12$hNf2lSsxfm0.i4a.1kVpSOVyBCfIB51VRjgBUyv6kdnyTlgWj81Ay
的哈希值。
步骤二:创建 web.yml 文件
-
创建 web.yml 文件,内容如下(使用生成的哈希密码):
basic_auth_users: admin: $2b$12$hNf2lSsxfm0.i4a.1kVpSOVyBCfIB51VRjgBUyv6kdnyTlgWj81Ay
-
验证 web.yml 文件:
promtool check web-config web.yml
如果验证成功,会输出
web.yml SUCCESS
。
步骤三:配置 Prometheus
-
编辑 prometheus.yml 文件(位于 TiDB 的 Prometheus 配置目录中),添加以下内容来加载 web.yml 文件:
global: scrape_interval: 15s evaluation_interval: 15s scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] # 添加 web 配置 (如果报错 直接在启动脚本中添加) web_config: file: /path/to/web.yml
确保
file
路径指向正确的 web.yml 文件位置。 -
重启 Prometheus:
tiup cluster restart <cluster-name> -R prometheus tiup cluster start <cluster-name> -R prometheus tiup cluster stop <cluster-name> -R prometheus
步骤四:测试认证
-
使用 curl 进行测试:
尝试未认证访问(应返回 401 Unauthorized):
curl --head http://localhost:9090/graph
使用基本认证访问(输入正确的用户名和密码):
curl -u admin http://localhost:9090/metrics
输入密码后,你应能看到 Prometheus 的指标输出。
参考资料: