Prometheus: 为 Prometheus Node Exporter 添加 TLS 加密

本文档属于从 Linux 检索指标指南的一部分。您可以在此处查看完整指南:如何使用 Node Exporter 在 Prometheus 中检索 Linux 服务器指标

👋 欢迎来到 Stackhero 文档!

Stackhero 提供即用型 Prometheus 云 解决方案,具有多种优势,包括:

  • 包含 Alert Manager,可发送警报到 SlackMattermostPagerDuty 等。
  • 专用电子邮件服务器发送无限制电子邮件警报
  • Blackbox 用于探测 HTTPICMPTCP 等。
  • 使用在线配置文件编辑器进行简单配置
  • 只需点击即可轻松更新
  • 专用私有 VM提供的最佳性能和强大安全性

节省时间简化生活:只需5 分钟即可试用 Stackhero 的 Prometheus 云托管 解决方案!

默认情况下,Node Exporter 不加密通信。这意味着包括之前定义的密码在内的凭据是以明文传输的。为了保护通信,您可以按如下方式启用 TLS 加密。

运行以下命令以创建 TLS 证书和密钥:

# 创建 TLS 证书
cd /tmp
sudo openssl req -new -newkey rsa:2048 -days 3650 -nodes -x509 \
  -keyout /etc/prometheus_node_exporter/tlsCertificate.key \
  -out /etc/prometheus_node_exporter/tlsCertificate.crt \
  -subj "/CN=`hostname`" \
  -addext "subjectAltName = DNS:`hostname`"
sudo chmod 600 /etc/prometheus_node_exporter/*
sudo chown -R node_exporter:node_exporter /etc/prometheus_node_exporter

将以下行附加到配置文件中以添加 TLS 设置,然后重启 Node Exporter:

sudo cat << 'EOF' >> /etc/prometheus_node_exporter/configuration.yml
tls_server_config:
  cert_file: /etc/prometheus_node_exporter/tlsCertificate.crt
  key_file: /etc/prometheus_node_exporter/tlsCertificate.key

EOF

# 重启 Prometheus Node Exporter
sudo systemctl restart node_exporter

您可以通过以下命令通过 HTTPS 连接来验证 TLS 是否处于活动状态:

curl -k -u prometheus:${password} https://localhost:9100/metrics

请注意,此方法不使用 CA 证书,因此您需要将 "-k" 选项传递给 cURL 以绕过证书验证。