文章目录

  • Prometheus 基于用户名密码访问
    • 1. `Node Export`端配置密码
    • 2. 在被监控端这里生成密码
    • 3. 在node_exporter中新增配置文件
    • 4. node_exporter启动时引用该配置
    • 5. 访问http://192.168.2.44:9100/metrics需要账户密码验证了
    • 6. 配置Prometheus启用用户名密码访问
    • 7. 重启Prometheus后可以看到数据可以正常采集了
  • `Prometheus` 对 `Node_Exporter` TLS加密认证
    • 1. 启用 TLS ,准备证书
    • 2. node_exporter新增`config.yml`配置引用TLS
    • 3. 启动node_exporter,验证TLS链接
    • 4. 配置 Prometheus 使用 TLS
    • 将剩下的节点也使用htpps方式
  • `Prometheus`自己 TLS加密认证
    • 1. 在Prometheus目录下新增`config.yml`文件
    • 2. 修改prometheus.yml文件
    • 3. 修改启动服务脚本`prometheus.service`(引用刚才创建的config.yml)
    • 4. 重启Prometheus,验证加密保护
    • 5. 使用TLS加密后Grafana获取不到数据
  • Prometheus 仅使用 Basic Auth(避免踩Grafana坑)

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: adminpassword: 123456static_configs:- targets: ['192.168.2.44:9100']

7. 重启Prometheus后可以看到数据可以正常采集了

PrometheusNode_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.crtnode_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.crtkey_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.htmlcurl performs SSL certificate verification by default, using a "bundle"of Certificate Authority (CA) public keys (CA certs). If the defaultbundle file isn't adequate, you can specify an alternate fileusing the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented inthe bundle, the certificate verification probably failed due to aproblem with the certificate (it might be expired, or the name mightnot match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, usethe -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  CurrentDload  Upload   Total   Spent    Left  Speed0     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: httpstls_config:ca_file: node_exporter.crtinsecure_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.crtkey_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.crtkey_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: adminpassword: 123456scheme: httpstls_config:ca_file: node_exporter.crtinsecure_skip_verify: truestatic_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-failureExecStart=/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: adminpassword: 123456
# 将TLS内容注释掉
#    scheme: https
#    tls_config:
#      ca_file: node_exporter.crt
#      insecure_skip_verify: truestatic_configs:- targets: ['192.168.2.45:9090']
  • 重启prometheus,后grafana 勾选Basic auth添加数据源,给出账户密码

二、Prometheus TLS加密认证和基于 basic_auth 用户名密码访问相关推荐

  1. 实验二 简单网络命令和wireshark捕获FTP用户名密码

    实验二 简单网络命令和wireshark捕获FTP用户名密码 预备知识 Wireshark(前称Ethereal)是一个网络封包分析软件.网络封包分析软件的功能是撷取网络封包,并尽可能显示出最为详细的 ...

  2. DM8的TLS加密认证配置相关

    1.为什么要使用SSL/TLS数字证书?   安装了SSL/TLS证书之后,可以保证客户端到服务器端之间的安全通信,数字证书采用非对称加密方式.虽然经过对称加密方式后的数据也无法被破译,但在使用了数字 ...

  3. gRPC 的 SSL/TLS 加密认证

    文章目录 简介 生成自签证证书 服务端 客户端 单向认证 双向认证 基于 Token 的单向认证 基于 Token 的双向认证 简介 传输层安全性协议(Transport Layer Security ...

  4. [WCF安全系列]认证与凭证:用户名/密码认证与Windows认证

    如果要给认证下一个定义,我个人的倾向这样的定义:认证是确定被认证方的真实身份和他或她申明(Claim)的身份是否相符的行为.认证方需要被认证方提供相应的身份证明材料,以鉴定本身的身份是否与声称的身份相 ...

  5. 基于嵌入式的密码访问的门锁系统

    目录 第一部分 功能描述 1 第二部分 设计方案 1 一. mylock 程序 1 二. lock_guard 监控程序 3 第三部分 安全设计描述 4 一. 抗干扰设计 4 2.睡眠避干扰 4 据此 ...

  6. [网络安全自学篇] 十二.Wireshark安装入门及抓取网站用户名密码(一)

    这是作者的系列网络安全自学教程,主要是关于网安工具和实践操作的在线笔记,特分享出来与博友共勉,希望您们喜欢,一起进步.前文分享了虚拟机VMware+Kali安装入门及Sqlmap基本用法,这篇文章将分 ...

  7. 基于QQ用户名密码方式爬取邮件数据

    由于一个爬虫项目需要爬取QQ邮箱的邮件数据,给定QQ的邮箱账户和密码,模拟浏览器登录爬取邮件文件. 首先通过使用Fiddler抓取QQ邮箱登录的HTTP请求包,很显然其密码是通过某种变换而来,不像有的 ...

  8. 这就是你日日夜夜想要的docker!!!---------TLS加密远程连接Docker

    文章目录 一.Docker 存在的安全问题 1.Docker 自身漏洞 2.Docker 源码问题 3.Docker 架构缺陷与安全机制 二.TLS加密通讯协议 1.TLS介绍 2.CA证书 三.配置 ...

  9. 【kafka专栏】安全认证之SASL_PLAINTEXT用户名密码方式

    文章目录 一.服务端配置 1.1.修改`server.properties` 1.2.配置用户名密码 1.3.修改启动脚本 二.模拟客户端shell脚本调整 2.1. 脚本调整 2.2.脚本测试 三. ...

最新文章

  1. gitlab开启https加密 and 全站https
  2. 论手残党画交互原型的正确姿势
  3. [ASP.NET Core 3框架揭秘] 文件系统[1]:抽象的“文件系统”
  4. 【云栖直播】精彩推荐第3期:个性化推荐系统搭建实践
  5. java ztree json_java递归实现ztree树结构数据展示
  6. 联想 android 5.1 root权限,联想a520一键root权限获取教程(图文)
  7. 解析IPostBackEventHandler和WebForm的事件机制
  8. 铅笔道区块链实验班_你们抢着要的道地药材,必须用上区块链了
  9. numpy数据类型dtype转换
  10. 数据挖掘:模型选择——线性回归
  11. HDFS-windows下测试hdfs命令
  12. eXtremeComponents指南
  13. 实战技巧:网站死链检测及处理方法!
  14. 网站建设之需要改进的地方
  15. 云计算未来 — 云原生
  16. 【官方文档】Fluent Bit 数据管道之输入插件(Tail)
  17. 连接主机名失败但可以连接主机ip,能用ip地址连接却无法用主机名连接,看过来一站解决
  18. Redis数据库的使用
  19. DRAM(动态随机储存器)的内部工作原理简述
  20. javaFx屏幕截图工具

热门文章

  1. 华为HCIE RS笔记-20 OSPF的V-Link
  2. Android 控件右上角角标的实现方案
  3. 2022年,人工智能和数据发展呈现哪五大趋势?
  4. matlab中多元线性回归regress函数精确剖析(附实例代码)
  5. 联想R720使用kali安装NVIDIA显卡驱动
  6. 学习笔记-B/S - Exploits
  7. TCP与UDP协议,socket套接字编程,通信相关操作
  8. 男人一生三块田,你不耕别人替你耕...
  9. 但总觉得明白了一点点什么
  10. 我的世界服务器自定义怪物怎么用,我的世界完全自定义怪物教程攻略