Prometheus系列(3)之基本认证

目标

登录Prometheus的9090端口页面的时候,需要输入用户名和密码,才能进入Prometheus页面。

设置密码

Prometheus配置密码不能是明文,必须经过bcrypt程序对密码进行Hash处理。

gen-pass.py

vim 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())

安装bcrypt模块

pip3 install bcrypt

设置密码

python3 gen-pass.py

web.yml

sudo vim /etc/prometheus/web.yml

内容如下:

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

promtool验证密码

promtool check web-config /etc/prometheus/web.yml
/etc/prometheus/web.yml SUCCESS

文件夹权限

sudo chown -R prometheus:prometheus /etc/prometheus

修改Sys V配置

sudo vim /etc/systemd/system/prometheus.service

添加一行内容如下:

--web.config.file=/etc/prometheus/web.yml

整个文件内容如下:

[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
    --config.file /etc/prometheus/prometheus.yml \
    --storage.tsdb.path /var/lib/prometheus/ \
    --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries \
    --web.config.file=/etc/prometheus/web.yml

[Install]
WantedBy=multi-user.target

重启Prometheus

sudo systemctl daemon-reload
sudo systemctl restart prometheus

测试

Prometheus需要基本认证
这里就可以看出,需要基本认证了。

总结

这就是Prometheus的基本认证,这里主要使用到bcrypt进行密码hashing,然后,进行Prometheus配置。

参考: