prometheus-tls加密


前言

记录一下prometheus-tls加密的开启


一、tls加密的引入

在basic_auth加密的基础上加上tls加密,密码密钥双保险。

二、使用步骤

使用basic-auth的基础环境上进行试验,基础环境的搭建具体可以参考basic_auth的配置文章

1.生成密钥文件

安装工具包并生成加密后密码

openssl req -new -newkey rsa:2048 -days 3650 -nodes -x509 -keyout prom-test.key -out prom-test.crt -subj "/C=CN/ST=Beijing/L=Beijing/O=Moelove.info/CN=localhost"

2.将tls密钥文件写入config.yml文件内

准备配置文件,新增tls配置

cat > ./config.yml<<eof
basic_auth_users:
  # 当前设置的用户名为admin, 可以设置多个
  admin: $2y$12$mMnPuKlOQ97ff4NjDsQTMukAtRS/ILpjxjEQrCN0vefs0CBLe/hi6
tls_server_config:    # TLS加密
  cert_file: prom-test.crt
  key_file: prom-test.key

配置之后将key、crt文件拷贝至config.yml文件相同目录

4.修改prometheus配置

修改配置,增加basic_auth配置

scrape_configs:
  - job_name: 'prometheus'
    basic_auth:
      username: admin
      password: 123456
	  #新增tls配置
    scheme: https
    tls_config:
      ca_file: prom-test.crt    #crt文件名
      insecure_skip_verify: true  # 跳过不安全认证
    static_configs:
    - targets: ['prometheus:9090']

5.启动服务

5.1 service模式

修改/usr/lib/systemd/system/prometheus.service文件,在ExecStart后面追加–web.config.file=/xx/xx/xx/config.yml

例:

cat /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

  [Install]
  WantedBy=multi-user.target

5.2 docker模式

修改镜像,在服务启动脚本命令内增加–web.config.file配置,

例:

CMD        [ "--config.file=/etc/prometheus/prometheus.yml", \
             "--storage.tsdb.path=/prometheus", \
             "--web.console.libraries=/usr/share/prometheus/console_libraries", \
             "--web.console.templates=/usr/share/prometheus/consoles", \
             "--web.config.file=/etc/prometheus/config.yml" ]

镜像制作完毕之后,在启动容器时,将config.yml、key、crt文件传递到/etc/prometheus/目录即可

  prometheus:
    image: prometheus:1
    volumes:
      - type: bind
        source: ./prometheus/prom-test.crt
        target: /etc/prometheus/prom-test.crt
        read_only: true
      - type: bind
        source: ./prometheus/prom-test.key
        target: /etc/prometheus/prom-test.key
        read_only: true
      - type: bind
        source: ./xxx/xxx/config.yml
        target: /etc/prometheus/config.yml
        read_only: true

六.访问测试

6.1 测试http协议

使用http协议访问

当使用http协议访问9090时,会提示
Client sent an HTTP request to an HTTPS server.

6.2 测试https协议

使用https协议访问

当使用https协议访问9090时,会出现登录弹窗
在这里插入图片描述
输入账号密码登录之后检查target
在这里插入图片描述
本机器target正常

七.对接granafa

数据源配置处修改http-url,新增勾选"TLS Client Auth"、“Skip TLS Verify”

7.1 配置Basic Auth Details

修改http-url,将http协议修改为https协议

在这里插入图片描述

7.2 TLS/SSL Auth Details

填写ServerName,将生成的crt、key文件内密钥信息填写入Client Cert、Client Key文本框内

在这里插入图片描述

7.3 测试查看配置可用性

在这里插入图片描述


总结

tls使用相较basic_auth多了一个密钥的配置,使用上没有差异

参考

二、Prometheus TLS加密认证和基于 basic_auth 用户名密码访问:link

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值