安装好Prometheus后发现我们打开地址便可访问无安全性,此时我们想到官方指南中的basic_auth。
参考指南:Securing Prometheus API and UI endpoints using basic auth | Prometheus
Prometheus配置基本授权
假设您希望要求访问 Prometheus 实例的所有用户提供用户名和密码。对于此示例,请用作用户名并选择您想要的任何密码。
生成密码
$ htpasswd -nBC 12 '' | tr -d ':\n'
New password:
Re-type new password:
$2b$12$hNf2lSsxfm0.i4a.1kVpSOVyBCfIB51VRjgBUyv6kdnyTlgWj81Ay
在此示例中,我使用“test”作为密码。
将该密码保存在某个地方,我们将在接下来的步骤中使用它!
创建web.yml
让我们创建一个web.yml文件, 内容如下:
basic_auth_users:admin: $2b$12$hNf2lSsxfm0.i4a.1kVpSOVyBCfIB51VRjgBUyv6kdnyTlgWj81Ay
您可以使用以下命令验证该文件 promtool check web-config web.yml
$ promtool check web-config web.yml
web.yml SUCCESS
您可以向文件添加多个用户。
启动 Prometheus
您可以使用 Web 配置文件启动 prometheus,如下所示:
$ prometheus --web.config.file=web.yml
修改启动配置文件,添加参数 --web.config.file=web.yml
如systemctl文件所示:
$ vim prometheus.yml
[Unit]
Description="prometheus"
Documentation=https://prometheus.io/
After=network.target[Service]
Type=simple
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --web.config.file=/opt/prometheus/web.yml --storage.tsdb.path=/opt/data --web.enable-lifecycle --enable-feature=remote-write-receiver --query.lookback-delta=2m
Restart=on-failure
SuccessExitStatus=0
LimitNOFILE=65536
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=prometheus[Install]
WantedBy=multi-user.target
测试
您可以使用 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 指标输出,它应该如下所示:
# 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
...
Node_exporter基本授权
因安全需要,现在对 node_exporter 进行配置以支持 TLS 和 Basic Auth。
Node_exporter 1.0 以上版本才支持 TLS 和 Basic Auth
Node_exporter 配置
准备工作
prometheus/node_exporter
下载安装
$ wget https://github.com/prometheus/node_exporter/releases/download/v1.9.1/node_exporter-1.9.1.linux-amd64.tar.gz
$ tar xf node_exporter-1.9.1.linux-amd64.tar.gz
$ mv node_exporter-1.9.1.linux-amd64 /usr/local/node_exporter
生成TLS证书
$ openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout node_exporter.key -out node_exporter.crt -subj "/C=CN/ST=Beijing/L=Beijing/O=Moelove.info/CN=localhost"
Generating a RSA private key
...................+++++
.........................................................................................................................................................................................................................................................................+++++
writing new private key to 'node_exporter.key'
-----$ ll
total 16
drwxr-xr-x 2 root root 4096 Dec 1 14:58 ./
drwx------ 27 root root 4096 Dec 1 14:58 ../
-rw-r--r-- 1 root root 1310 Dec 1 14:58 node_exporter.crt
-rw------- 1 root root 1704 Dec 1 14:58 node_exporter.key
通过上面的步骤,我们得到了 node_exporter.crt 和node_exporter.key 这两个文件。
basic auth 认证生成
安装 htpasswd 来生成密码 hash
$ apt install apache2-utils -y #Ubuntu$ yum install httpd-tools -y #centos
在 Node_exporter 目录下执行
$ htpasswd -nBC 12 '' | tr -d ':\n'
New password:
Re-type new password:
$2b$12$hNf2lSsxfm0.i4a.1kVpSOVyBCfIB51VRjgBUyv6kdnyTlgWj81Ay
在此示例中,我使用“test”作为密码。
修改配置
将前面生成的 node_exporter.crt 和node_exporter.key 文件复制到 Node_exporter 解压目录下。
$ cp /usr/local/node_exporter/node_exporter.* .
$ ll
total 19352
drwxr-xr-x 2 root root 4096 Dec 1 15:12 ./
drwxr-xr-x 5 root root 4096 Dec 1 15:07 ../
-rw-r--r-- 1 3434 3434 11357 Nov 30 03:05 LICENSE
-rw-r--r-- 1 3434 3434 463 Nov 30 03:05 NOTICE
-rwxr-xr-x 1 3434 3434 19779640 Nov 30 02:59 node_exporter*
-rw-r--r-- 1 root root 1310 Dec 1 15:12 node_exporter.crt
-rw------- 1 root root 1704 Dec 1 15:12 node_exporter.key
编写配置文件,并保存为 config.yaml
tls_server_config:cert_file: node_exporter.crtkey_file: node_exporter.key
basic_auth_users:# 当前设置的用户名为 root , 可以设置多个root: $2b$12$hNf2lSsxfm0.i4a.1kVpSOVyBCfIB51VRjgBUyv6kdnyTlgWj81Ay
启动
nohup ./node_exporter --web.listen-address=:9100 --web.config.file=config.yaml &
验证
$ curl http://localhost:39100/metrics
Client sent an HTTP request to an HTTPS server.
root@zabbix:/opt/node_exporter# curl https://localhost:39100/metrics
curl: (60) SSL certificate problem: self signed certificate
More details here: https://curl.haxx.se/docs/sslcerts.htmlcurl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.
可以看到不能直接访问了,下面带上证书及用户密码再次测试
$ curl -u prometheus -s --cacert node_exporter.crt https://localhost:39100/metrics |grep node_exporter_build_info
Enter host password for user 'prometheus':
# HELP node_exporter_build_info A metric with a constant '1' value labeled by version, revision, branch, and goversion from which node_exporter was built.
# TYPE node_exporter_build_info gauge
node_exporter_build_info{branch="HEAD",goversion="go1.19.3",revision="1b48970ffcf5630534fb00bb0687d73c66d1c959",version="1.5.0"} 1
Prometheus 配置
下载最新版解压,并将前面生成的 node_exporter.crt 和node_exporter.key 文件复制到该目录下。在 prometheus.yml 加入如下内容:
global:scrape_interval: 15s evaluation_interval: 15s scrape_configs:- job_name: 'prometheus'static_configs:- targets: ['localhost:9090']- job_name: 'node_exporter'scheme: httpstls_config:ca_file: node_exporter.crtinsecure_skip_verify: truebasic_auth:username: rootpassword: teststatic_configs:- targets: ['localhost:9100']
启动 Prometheus 即可。