文章目录
Prometheus 基于用户名密码访问
参考博客:https://blog.csdn.net/qq_34556414/article/details/113106095
Export采集指标的地址谁都可以访问,这里可以使用基础认证使用用户名密码方式去采集被监控端,也就是访问接口使用用户名密码提高安全性
1. Node Export
端配置密码
Basic Auth 支持配置多个用户名密码的。
2. 在被监控端这里生成密码
# 没有https-tools则 yum install -y httpd-tools
[root@VM_2-44 ~]# rpm -qa|grep httpd-tools
httpd-tools-2.4.6-97.el7.centos.x86_64
[root@VM_2-44 ~]# htpasswd -nBC 12 '' | tr -d ':\n'
New password: # 这里设置密码为123456
Re-type new password:
$2y$12$6yR84yKSqoYv3B2D70QAOuqggT0QvdpMp1wUNfLwBo63oLYWc1AYy
[root@VM_2-44 ~]#
3. 在node_exporter中新增配置文件
[root@VM_2-44 /usr/local/node_exporter]# ls
LICENSE node_exporter NOTICE
[root@VM_2-44 /usr/local/node_exporter]# vim node_exporter
# 设置用户名为admin(可修改),密码为123456
[root@VM_2-44 /usr/local/node_exporter]# vim config.yml
basic_auth_users:
# 当前设置的用户名为admin, 可以设置多个
admin: $2y$12$6yR84yKSqoYv3B2D70QAOuqggT0QvdpMp1wUNfLwBo63oLYWc1AYy
4. node_exporter启动时引用该配置
/usr/local/node_exporter/node_exporter --web.config=/usr/local/node_exporter/config.yml
# 上文一、中我们把node_exporter添加为服务了,修改node_exporter.service
[root@VM_2-44 /usr/local/node_exporter]# vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target
[Service]
ExecStart=/usr/local/node_exporter/node_exporter --web.config=/usr/local/node_exporter/config.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target
[root@VM_2-44 /usr/local/node_exporter]#
# 重启加载,并重启
systemctl daemon-reload
systemctl restart node_exporter.service
5. 访问http://192.168.2.44:9100/metrics需要账户密码验证了
-
现在普罗米修斯没有配置,可以看到采集不了数据了
-
需要密码才能访问
6. 配置Prometheus启用用户名密码访问
- job_name: 'linux'
basic_auth:
username: admin
password: 123456
static_configs:
- targets: ['192.168.2.44:9100']
7. 重启Prometheus后可以看到数据可以正常采集了
Prometheus
对 Node_Exporter
TLS加密认证
在 Promethues 的监控体系中,社区中一直存在的一个观点是,Metrics 不包含过于私密的信息。所以你可以看到,大多数的 /metrics 接口都是直接暴露出来的,没什么特别的安全措施。
但随着 Prometheus 在生产中的大量应用,安全问题变得更加重要。
大家最先想到的解决办法就是,为 Prometheus 与监控目标之间的连接启用 TLS。 但由于各类 exporter 并不原生支持 TLS 连接,所以通常情况下我们会选择配合反向代理来完成。
这种方式能满足需求,但未免复杂了些。近期 Prometheus 对其安全模型做了修改 , 从 Node Exporter 开始到后续其他的组件,都将支持 TLS 和 basic auth, 同时也列出了最新的安全基准(默认情况下都支持 TLS v1.2 及以上)
注意:Node Exporter v1.0.0 版本以上才支持。
1. 启用 TLS ,准备证书
# openssl生成证书
[root@VM_2-45 /usr/local/prometheus]# openssl req -new -newkey rsa:2048 -days 3650 -nodes -x509 -keyout \
node_exporter.key -out node_exporter.crt \
-subj "/C=CN/ST=Beijing/L=Beijing/O=Moelove.info/CN=localhost"
[root@VM_2-45 /usr/local/prometheus]# ll node_exporter.*
-rw-r--r-- 1 root root 1289 3月 12 09:53 node_exporter.crt
-rw-r--r-- 1 root root 1704 3月 12 09:53 node_exporter.key
[root@VM_2-45 /usr/local/prometheus]#
2. node_exporter新增config.yml
配置引用TLS
- 复制前面
/usr/local/prometheus
目录下生成的node_exporter.crt
和node_exporter.key
这两个文件到node_exporter目录下
[root@VM_2-45 /usr/local/prometheus]# cp node_exporter.* ../node_exporter/
[root@VM_2-45 /usr/local/prometheus]# cd ../node_exporter
[root@VM_2-45 /usr/local/node_exporter]# ls
LICENSE node_exporter node_exporter.crt node_exporter.key NOTICE
[root@VM_2-45 /usr/local/node_exporter]#
# 创建config.yml
[root@VM_2-45 /usr/local/node_exporter]# vim config.yml
tls_server_config: # TLS加密
cert_file: node_exporter.crt
key_file: node_exporter.key
- 使用 --web.config 将配置文件传递给 Node Exporter
[root@VM_2-45 /usr/local/node_exporter]# cat /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target
[Service]
# 使用 --web.config 将配置文件传递给 Node Exporter
ExecStart=/usr/local/node_exporter/node_exporter --web.config=/usr/local/node_exporter/config.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target
[root@VM_2-45 /usr/local/node_exporter]#
3. 启动node_exporter,验证TLS链接
[root@VM_2-45 /usr/local/node_exporter]# systemctl daemon-reload
[root@VM_2-45 /usr/local/node_exporter]# systemctl start node_exporter.service
# 验证TLS链接
# 发现无法使用http连接了,可以使用https连接
[root@VM_2-45 /usr/local/node_exporter]# curl http://localhost:9100/metrics
[root@VM_2-45 /usr/local/node_exporter]# curl https://localhost:9100/metrics
curl: (60) Peer's certificate issuer has been marked as not trusted by the user.
More details here: http://curl.haxx.se/docs/sslcerts.html
curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.
[root@VM_2-45 /usr/local/node_exporter]#
# 使用--cacert参数,将证书传递给curl 再次验证https(或者通过 -k 参数来忽略证书检查)
[root@VM_2-45 ~]# curl --cacert node_exporter.crt https://localhost:9100/metrics |head -n 10
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0.000116614
go_gc_duration_seconds{quantile="0.25"} 0.000124826
go_gc_duration_seconds{quantile="0.5"} 0.000197253
go_gc_duration_seconds{quantile="0.75"} 0.000307498
go_gc_duration_seconds{quantile="1"} 0.000307498
go_gc_duration_seconds_sum 0.000746191
go_gc_duration_seconds_count 4
# HELP go_goroutines Number of goroutines that currently exist.
4. 配置 Prometheus 使用 TLS
- 192.168.2.45的node_exporter使用TLS验证后,发现Prometheus无法通过http获取到数据了
- 修改下
prometheus.yml
配置文件,让 Prometheus 可以抓取 Node Exporter 暴露的 metrics
scheme: https
tls_config:
ca_file: node_exporter.crt
insecure_skip_verify: true # 跳过不安全认证
- https认证成功了
将剩下的节点也使用htpps方式
- 将Prometheus的证书都复制到别的节点
[root@VM_2-45 /usr/local/prometheus]# scp node_exporter.* 192.168.2.43:/usr/local/node_exporter
node_exporter.crt 100% 1289 1.7MB/s 00:00
node_exporter.key 100% 1704 1.9MB/s 00:00
[root@VM_2-45 /usr/local/prometheus]# scp node_exporter.* 192.168.2.44:/usr/local/node_exporter
node_exporter.crt 100% 1289 1.8MB/s 00:00
node_exporter.key 100% 1704 2.2MB/s 00:00
[root@VM_2-45 /usr/local/prometheus]# scp node_exporter.* 121.196.147.41:/usr/local/node_exporter
- node_exporter节点修改
config.yml
现在的效果是基于用户名密码、https双重保险啦
[root@aliyun node_exporter]# cat config.yml
basic_auth_users:
admin: $2y$12$6yR84yKSqoYv3B2D70QAOuqggT0QvdpMp1wUNfLwBo63oLYWc1AYy
tls_server_config:
cert_file: node_exporter.crt
key_file: node_exporter.key
Prometheus
自己 TLS加密认证
完成以上操作仅仅是完成了对node_exproter的TLS加密等措施,Prometheus本身仍然是http连接直接就可以访问
- 目标:
Prometheus
通过https加密,及使用账号密码才能登录
1. 在Prometheus目录下新增config.yml
文件
# 新增 config.yml 文件,使用TLS及basic_auth
[root@VM_2-45 /usr/local/prometheus]# vim config.yml
basic_auth_users:
admin: $2y$12$6yR84yKSqoYv3B2D70QAOuqggT0QvdpMp1wUNfLwBo63oLYWc1AYy
tls_server_config:
cert_file: node_exporter.crt
key_file: node_exporter.key
[root@VM_2-45 /usr/local/prometheus]# ll
总用量 168008
-rw-r--r-- 1 root root 168 3月 12 13:31 config.yml
drwxr-xr-x 2 3434 3434 38 2月 18 00:11 console_libraries
drwxr-xr-x 2 3434 3434 173 2月 18 00:11 consoles
drwxr-xr-x 7 root root 172 3月 12 13:33 data
-rw-r--r-- 1 3434 3434 11357 2月 18 00:11 LICENSE
-rw-r--r-- 1 root root 1289 3月 12 10:48 node_exporter.crt
-rw-r--r-- 1 root root 1704 3月 12 10:48 node_exporter.key
-rw-r--r-- 1 3434 3434 3420 2月 18 00:11 NOTICE
-rwxr-xr-x 1 3434 3434 91044140 2月 17 22:19 prometheus
-rw-r--r-- 1 root root 1093 3月 12 13:11 prometheus.yml
-rwxr-xr-x 1 3434 3434 80948693 2月 17 22:21 promtool
drwxr-xr-x 2 root root 38 3月 11 14:27 rules
drwxr-xr-x 2 root root 22 3月 12 09:56 sd_config
-rwxr-xr-x 1 root root 240 3月 10 15:24 start.sh
-rw-r--r-- 1 root root 187 3月 12 10:58 targets.json
-rw-r--r-- 1 root root 183 3月 11 16:30 targets.yml
[root@VM_2-45 /usr/local/prometheus]#
2. 修改prometheus.yml文件
# 将prometheus标签下加入认证,不然prometheus标签下的仍然是HTTP方式
- job_name: 'prometheus'
basic_auth:
username: admin
password: 123456
scheme: https
tls_config:
ca_file: node_exporter.crt
insecure_skip_verify: true
static_configs:
- targets: ['192.168.2.45:9090']
3. 修改启动服务脚本prometheus.service
(引用刚才创建的config.yml)
# --web.config.file=config.yml
[root@VM_2-45 /usr/local/prometheus]# ./prometheus --config.file=prometheus.yml --web.config.file=config.yml --web.listen-address=:9090 --web.enable-lifecycle
# 顺便把服务启动项修改下
[root@VM_2-45 /usr/local/prometheus]# vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=https://prometheus.io
[Service]
Restart=on-failure
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --web.config.file=/usr/local/prometheus/config.yml --web.listen-address=:9090 --web.enable-lifecycle
[Install]
WantedBy=multi-user.target
4. 重启Prometheus,验证加密保护
[root@VM_2-45 /usr/local/prometheus]# systemctl restart prometheus && systemctl status prometheus.service
- 重新打开已加密
5. 使用TLS加密后Grafana获取不到数据
- 测试后必须要http
注:尝试用nginx做反向代理,将http请求转发到https但是grafana验证通过,但是就是获取不到数据,待研究,付nginx配置
server{
listen 9091;
server_name 192.168.2.45;
location / {
proxy_pass https://192.168.2.45:9090;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
}
}
Prometheus 仅使用 Basic Auth(避免踩Grafana坑)
1. 修改config.yml,不使用TLS
[root@VM_2-45 /usr/local/prometheus]# cat config.yml
basic_auth_users:
admin: $2y$12$6yR84yKSqoYv3B2D70QAOuqggT0QvdpMp1wUNfLwBo63oLYWc1AYy
# 不使用TLS加密,仅使用basic_auth验证
#tls_server_config:
# cert_file: node_exporter.crt
# key_file: node_exporter.key
[root@VM_2-45 /usr/local/prometheus]#
2. 修改prometheus.yml,不使用TLS
scrape_configs:
- job_name: 'prometheus'
basic_auth:
username: admin
password: 123456
# 将TLS内容注释掉
# scheme: https
# tls_config:
# ca_file: node_exporter.crt
# insecure_skip_verify: true
static_configs:
- targets: ['192.168.2.45:9090']
- 重启prometheus,后grafana 勾选Basic auth添加数据源,给出账户密码