Prometheus 是一款开源的监控系统,本身自带支持http basic authentication简单认证 和https TLS加密访问认证。以下是basic_auth配置示例:
1. 生成auth secret
import getpass
import bcrypt
password = getpass.getpass("password: ")
hashed_password = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt())
print(hashed_password.decode())
执行python代码,输入密码后生成密文secret
2. 创建web config file
需要创建basic auth配置文件:web-config.yml,Prometheus启动时会加载该认证配置。配置文件内容为用户名密码,多个用户可换行隔开。
basic_auth_users:
admin: <secret>
3. 启动参数
需要使用 --web.config.file 选项指定加载web-config.yml配置文件。
4. 访问测试
使用认证之后,访问服务如果不带上登录密码,会提示:Unauthorized
认证密码可以base64编码之后放到header头里面:
echo "admin:secret"|base64
curl -H "Authorization: Basic YWRtaW46c2VjcmV0Cg==" http://localhost:909/
需要注意的是,如果Prometheus采集了自身的监控指标,需要在promehtues.yml中对应的job加上basic_auth配置:
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
- job_name: 'prometheus'
basic_auth:
username: 'admin'
password: '<secret>'
static_configs:
- targets: ["localhost:9090"]
5. 更安全的认证
如果希望更安全一点,就尝试官方的TLS配置吧
tls_server_config:
# Certificate and key files for server to use to authenticate to client.
cert_file: </path/to/ca.crt>
key_file: </path/to/ca.key>
6. grafana数据源配置
启用了basic_auth访问认证的Prometheus实例,在grafana上配置数据源的时候,需要勾选Basic Auth选项,填写用户名密码
最后,pushgateway、alertmanager组件也可以配置basic auth,配置方式基本类似,这里就不展开~