考虑将公司的联邦集群(pull)换成remote_write(push)这种形式, 所以需要将Prometheus开放到公网,看了看认证相关的配置

也可以使用Nginx来反向代理,可以参考 Nginx开启基本http认证, 不过Prometheus原生带了basic auth 和ssl认证, 官网的说明https://prometheus.io/docs/guides/basic-auth/

开启web配置文件

1
./prometheus --web.config.file="web.yml"  --web.listen-address="0.0.0.0:9001"

生成密码

密码需要bcrypt加密,这里使用htpasswd工具生成

1
apt install apache2-utils
1
yum install httpd-tools
1
htpasswd -nB 'admin'

web配置文件

vim web.yml

1
2
basic_auth_users:
admin: $2y$05$UKSS18ztdsUNoEuXYScr2OE1TCMe1hWnmD6JuwUi/uPTJayHIakae

验证一下配置

1
promtool check web-config web.yml

remote write 访问

后面访问控制台页面或者调用接口都需要指定账号密码了

1
curl -u admin:xxxxx http://localhost:9090

其他Prometheus如果需要往这台写入, 需要在其他的Prometheus remote_write配置认证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
remote_write:
- url: "http://127.0.0.1:9090/api/v1/write"
basic_auth:
username: admin
password: xxxxx
remote_timeout: 30s
tls_config:
insecure_skip_verify: true
queue_config:
capacity: 500
max_shards: 1000
min_shards: 1
max_samples_per_send: 100
batch_send_deadline: 5s
min_backoff: 30ms
max_backoff: 100ms

联邦配置

如果此Prometheus需要通过联邦机制接入到其他Prometheus, 需要在对方的配置里面增加:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- job_name: 'federate-http'
scrape_interval: 30s
scrape_timeout: 30s
honor_labels: true
metrics_path: '/federate'
basic_auth:
username: 'admin'
password: 'xxxxx'
params:
'match[]':
- '{job=~".+"}'
file_sd_configs:
- files:
- /data/prometheus/conf.d/federate_http.yml
refresh_interval: 30s

启用了Basic Auth怎么健康检查

在Kubernetes环境下,配置如下:

1
2
# 生成base64的账号密码编码
echo -n "user:pass" |base64 -w0

在 Liveness 和 Readness 配置

1
2
3
4
httpGet: 
httpHeaders:
- name: Authorization
value: Basic dXNlcjpwYXNz