Grafana是Grafana Labs开发的开源、多平台数据可视化平台,Grafana 提供交互式数据可视化 Web 应用程序,其中包括图表、图形和警报。使用 Grafana,您可以查询、可视化、设置警报以及探索 TSDB 的指标、日志和跟踪。它是一个强大的工具,可将时间序列数据库 (TSDB) 数据转换为图表和可视化。
在 Grafana 中,您可以通过“数据源”添加时间序列数据库数据,Grafana 支持多种数据源,例如 Prometheus、InfluxDB、PostgreSQL、Loki、Jaeger、Graphite、Google Cloud Monitoring、AWS CloudWatch、Azure Monitor 等。
在本教程中,我将安装 Grafana 开源分析和可视化 Web 应用程序,并使用 Nginx 作为反向代理。然后,我将使用 Node Exporter 安装和配置 Prometheus 开源系统监控以收集系统指标。最后,将 Prometheus 作为数据源添加到 Grafana 中,并设置用于系统监控的仪表板。
本文,将在 Rocky Linux 9 服务器上安装所有这些软件包。
先决条件
首先,您必须满足以下要求才能完成本指南:
-
Rocky Linux 9 服务器:您可以使用多个服务器或单个服务器进行实验。以下是您必须了解的安装类型: -
单服务器:grafana、prometheus 和 node_exporter 在单个服务器上。 -
两台服务器:server1 上的grafana、server3 上的prometheus 和node_exporter。 -
三台服务器:server1 上的grafana、server2 上的prometheus 和server3 上的node_exporter。 -
具有 sudo/root 管理员权限的非 root 用户。
安装 Grafana
Grafana 是一个多平台应用程序,可以安装在 Windows、Linux 和 macOS 等多个操作系统上。对于 Linux 系统,Grafana 为基于 Debian 的操作系统(Debian 和 Ubuntu)和基于 RHEL 的操作系统(RHEL、CentOS、Fedora、RockyLinux、AlmaLinux)提供存储库。
在此步骤中,您将设置 Grafana 存储库并在 Rocky Linux 9 服务器上安装 Grafana 9.3。
在设置 Grafana 存储库之前,请运行以下命令将默认加密策略后端设置为“SHA1”并重新启动服务器以应用更改。
sudo update-crypto-policies --set DEFAULT:SHA1
sudo restart
再次登录后,使用以下 nano 编辑器命令创建一个新的存储库文件“/etc/yum.repos.d/grafana.repo”。
sudo nano /etc/yum.repos.d/grafana.repo
将以下 Grafana 存储库添加到文件中。
[grafana]
name=grafana
baseurl=https://rpm.grafana.com
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://rpm.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
保存文件并退出编辑器。
现在运行以下命令来验证系统上的列表存储库,您应该看到 Grafana 存储库已添加。
sudo dnf repolist

添加 Grafana 存储库后,您现在可以通过下面的 dnf 命令安装 Grafana 软件包。
sudo dnf install grafana
出现提示时,输入 y 确认安装,然后按 ENTER。

系统还会提示您确认 Grafana 存储库的 gpg 密钥,输入 y 并按 ENTER 将 gpg 密钥添加到您的系统。

安装 Grafana 后,运行以下 systemctl 命令来重新加载 systemd 权限。
sudo systemctl daemon-reload
然后,启动并启用 Grafana 服务“grafana-server”,Grafana 服务现在应该已启用并正在运行。
sudo systemctl start grafana-server
sudo systemctl enable grafana-server

通过以下命令验证“grafana-server”服务以确保该服务正在运行。
sudo systemctl status grafana-server
以下输出确认 grafana-server 已启用并正在运行,grafana-server 现在应该在启动时自动运行。

配置Grafana
现在 Grafana 服务器已安装并运行,您将通过配置文件“/etc/grafana/grafana.ini”设置 Grafana 的运行方式。在此示例中,您将仅使用本地域 grafana.hwdomain.io 在本地主机上运行 grafana。
使用以下 nano 编辑器命令 打开 Grafana 配置文件“ /etc/grafana/grafana.ini” 。
sudo nano /etc/grafana/grafana.ini
取消注释下面的一些行并更改默认值,如下所示。
[server]
# The IP address to bind to, empty will bind to all interfaces
http_addr = localhost
# The http port to use
http_port = 3000
# The public facing domain name used to access grafana from a browser
domain = grafana.hwdomain.io
通过此配置,您将 Grafana 配置为仅使用默认 TCP 端口 3000 在本地主机上运行,您将使用本地域 grafana.hwdomain.io 运行 Grafana,该域由反向代理处理。
完成后保存文件并关闭编辑器。
现在通过 systemctl 命令 重新启动“ grafana-server ”服务以应用新的更改。
sudo systemctl restart grafana-server
Grafana 已配置、启动并运行。接下来,您将安装 Nginx 并将其配置为 Grafana 服务器的反向代理。
将 Nginx 设置为反向代理
在此步骤中,您将安装 Nginx Web 服务器并将其配置为 Grafana 服务器的反向代理。在安装 Nginx 之前,请确保您有解析到您的服务器的域名或本地域并已生成 SSL 证书。
运行下面的 dnf 命令将 Nginx Web 服务器安装到您的系统。
sudo dnf install nginx
当提示确认时输入 y,然后按 ENTER 继续。

安装 Nginx 后,使用以下 nano 编辑器命令 创建一个新的 Nginx 服务器块文件“ /etc/nginx/conf.d/grafana.conf ”。
sudo nano /etc/nginx/conf.d/grafana.conf
将以下行添加到文件中,并更改 SSL 证书的详细域名和路径。
# this is required to proxy Grafana Live WebSocket connections.
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name grafana.hwdomain.io;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
listen 443 ssl http2;
server_name grafana.hwdomain.io;
root /usr/share/nginx/html;
index index.html index.htm;
ssl_certificate /etc/letsencrypt/live/grafana.hwdomain.io/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/grafana.hwdomain.io/privkey.pem;
access_log /var/log/nginx/grafana-access.log;
error_log /var/log/nginx/grafana-error.log;
location / {
proxy_set_header Host $http_host;
proxy_pass http://localhost:3000/;
}
# Proxy Grafana Live WebSocket connections.
location /api/live {
rewrite ^/(.*) /$1 break;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $http_host;
proxy_pass http://localhost:3000/;
}
}
通过此配置,您将运行 Nginx 作为在 localhost:3000 上运行的 grafana 服务器的反向代理。您还可以通过安全的 SSL/HTTPS 连接来保护 grafana。此外,您还将为 grafana WebSocket 连接设置反向代理。
完成后保存文件并退出编辑器。
接下来,运行以下命令来验证 Nginx 配置并确保您的配置正确。
sudo nginx -t
如果您有正确的配置,您应该会收到输出消息,例如“test successful – syntax ok”。
现在运行以下 systemctl 命令来启动并启用 Nginx。
sudo systemctl start nginx
sudo systemctl enable nginx

Nginx 服务现在应该正在运行并启用。通过下面的 systemctl 命令验证 Nginx 服务。
sudo systemctl status nginx
以下输出确认 Nginx 服务正在运行并且已启用。Nginx 服务应该在启动时自动运行。

随着 Nginx 服务在 HTTP 和 HTTPS 协议上启动并运行,您将设置防火墙以打开这两种服务。
运行以下firewall-cmd命令将HTTP和HTTPS服务添加到firewalld。
sudo firewall-cmd --add-service={http,https} --permanent
之后,重新加载firewalld以应用更改并验证firewalld上启用的服务列表。
sudo firewall-cmd --reload
sudo firewall-cmd --list-services
您应该收到以下输出 – HTTP 和 HTTPS 服务已添加到防火墙。

配置firewalld后,您将能够访问grafana和Nginx反向代理安装。
打开Web浏览器并访问grafana安装的域名(即:https: //grafana.hwdomain.io/),您应该会看到grafana登录页面。
使用默认用户/密码“ admin ”登录。

登录后,系统会要求您更改默认密码。输入 grafana 安装的新密码,然后按“提交”确认。

您现在应该获得如下图所示的 grafana 仪表板。

至此,您已经完成了Rocky Linux 9服务器上grafana的安装和配置。您还可以使用 Nginx 反向代理运行 grafana,并通过 HTTPS 保护 grafana 安装并更改 grafana 的默认管理员用户。
接下来,您将安装和配置 Prometheus 和 node_exporter 以收集系统指标,并将 Prometheus 添加为 grafana 的数据源。
安装 Prometheus 和 Node Exporter
Prometheus 是一个开源系统监控和警报工具包。它是收集和查询指标数据的强大工具。Prometheus 的工作原理是通过在指标端点上发送 HTTP 请求,定期从应用程序服务和主机中提取(抓取)实时指标,然后将其压缩并存储在时间序列数据库中。
node_exporter 是最流行的普罗米修斯抓取工具之一,用于提取操作系统的指标。node_exporter 旨在监视主机系统,它公开了各种与硬件和内核相关的指标。
现在,您将在 Rocky Linux 9 服务器上安装 prometheus 和 node_exporter。
使用以下 nano 编辑器命令 创建新的存储库文件“/etc/yum.repos.d/prometheus.repo” 。
sudo nano /etc/yum.repos.d/prometheus.repo
将以下行添加到该文件中,该文件是 Packagecloud.io 提供的 prometheus 存储库。
[prometheus]
name=prometheus
baseurl=https://packagecloud.io/prometheus-rpm/release/el/$releasever/$basearch
repo_gpgcheck=1
enabled=1
gpgkey=https://packagecloud.io/prometheus-rpm/release/gpgkey
https://raw.githubusercontent.com/lest/prometheus-rpm/master/RPM-GPG-KEY-prometheus-rpm
gpgcheck=1
metadata_expire=300
完成后保存并关闭文件。
您现在可以通过下面的 dnf 命令验证系统上可用存储库的列表。
sudo dnf repolist
在下面的输出中,确认 prometheus 存储库已添加到您的系统中。

现在通过以下 dnf 命令安装 prometheus 和 node_exporter 软件包。
sudo dnf install prometheus2 node_exporter
出现提示时,输入 y 进行确认,然后按 ENTER。prometheus 和 node_exporter 安装将开始。

另外,当提示添加 GPG 密钥时,输入 y 进行确认。

prometheus和node_exporter安装完成后,运行以下systemctl命令启动并启用bot服务、prometheus和node_exporter服务。
sudo systemctl start prometheus node_exporter
sudo systemctl enable prometheus node_exporter
现在使用以下命令验证 promethues 和 node_exporter 服务。
sudo systemctl status prometheus node_exporter
在下面的输出中,确认 prometheus 和 node_exporter 服务正在运行并启用。这两个服务都会在启动时自动运行。

prometheus 默认在端口 9090 上运行,而 node-exporter 在默认端口 9100 上运行。现在,您将端口 9090 和 9100 添加到 firewalld。
运行以下firewall-cmd命令将prometheus和node_exporter端口添加到firewalld。然后,重新加载firewalld以应用新的更改。
sudo firewall-cmd --add-port={9090/tcp,9100/tcp} --permanent
sudo firewall-cmd --reload
配置firewalld后,您可以访问prometheus和node_exporter安装。但是,接下来您将进一步配置 prometheus 和 node_exporter。
配置 Prometheus 和 Node Exporter
安装 prometheus 和 node_exporter 后,您现在将使用以下配置设置这两个服务:
-
启用普罗米修斯的基本身份验证。 -
在 prometheus Web 界面上启用 HTTPS/SSL。 -
为目标机器设置u scrape_config。
为 Prometheus 启用 basic_auth 和 HTTPS/SSL
在默认安装中,prometheus 没有密码身份验证,并在 HTTP 协议上运行。为了确保安装安全,您需要在 prometheus 安装上设置 basic_auth 并启用 HTTPS。
在 prometheus 上设置 basic_auth 之前,请运行以下 dnf 命令来安装“httpd-tools”包。该软件包提供了可用于生成 bcrypt 密码的命令“htpasswd”。
sudo dnf install httpd-tools -y
接下来,运行以下命令为您的 prometheus 安装生成密码。在此示例中,您将使用用户“promadmin”作为 prometheus basic_auth 的默认用户。
htpasswd -nB promadmin
出现提示时,输入将用于 prometheus basic_auth的密码并重复该密码。
在下面的输出中,您可以看到生成的 bcrypt 密码。复制用户名和生成的密码。

现在使用以下 nano 编辑器命令创建一个新的 prometheus 配置文件“ /etc/prometheus/web.yml ”。此配置将在 Prometheus 安装上启用 HTTPS/SSL 和 basic_auth。
sudo nano /etc/prometheus/web.yml
将以下行添加到文件中,并确保更改 SSL 证书的路径以及普罗米修斯将使用的用户名和密码。
sued for your prometheus.
# tls certificates
tls_server_config:
cert_file: fullchain.pem
key_file: privkey.pem
# basic_auth
basic_auth_users:
promadmin: $2y$05$.OhemZb5HtMYsHSBdrH4/.74FKCL2NbD2I41FI5L/VO.Uy6c2dQ7i
“tls_server_config ”部分允许您启用安全的 HTTPS/SSL 连接,“ basic_auth_users ”将在您的 prometheus 安装上启用基本密码身份验证。
完成后保存文件并退出编辑器。
接下来,使用以下 nano 编辑器命令 打开文件“/etc/default/prometheus” 。
sudo nano /etc/default/prometheus
将以下行添加到“ PROMETHEUS_OPTS ”部分。
--web.config.file=/etc/prometheus/web.yml
选项“ –web.config.file”将指定配置或 prometheus Web 界面。在此示例中,配置为 ‘ */etc/prometheus/web.yml’*。
完成后保存文件并退出编辑器。
为目标机器设置 scrape_config
启用 prometheus basic_auth 和 HTTPS/SSL 后,接下来将目标计算机添加到 promethues ‘scrape_config’ 部分。
使用以下 nano 编辑器命令 打开 promethues 配置文件“/etc/prometheus/prometheus.yml” 。
sudo nano /etc/prometheus/prometheus.yml
在 scrape_config 部分,使用以下行更改默认配置。
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
# add settings for certificate and authentication
scheme: https
tls_config:
cert_file: /etc/prometheus/server.crt
key_file: /etc/prometheus/server.key
# if using self-signed certificate, set [true]
insecure_skip_verify: true
basic_auth:
username: 'promadmin'
password: 'password'
static_configs:
# if using a valid certificate, set the same hostname in the certificate
- targets: ["localhost:9090"]
- job_name: "node_exporter"
static_configs:
- targets: ["192.168.5.120:9100"]
通过此配置,您将设置两个名为“prometheus”和“node_exporter”的作业。请务必更改“promethues”作业中的 basic_auth 部分。
-
在“ prometheus ”作业中,您将启用 HTTPS 安全连接并启用“basic_auth”进行身份验证。在“prometheus”作业中,目标主机是“localhost:9090”,即 prometheus 服务器。 -
在“ node_exporter ”作业中,目标是在端口“9100”上运行的node_exporter 服务本身。
现在运行以下 systemctl 命令来重新启动 prometheus 和 node_exporter 服务并应用新的更改。
sudo systemctl restart prometheus node_exporter
此时,您已通过 SSL/HTTPS 启用基本身份验证和保护,使 Prometheus 进入下一步。您还添加了两个用于收集指标的抓取,即“ prometheus ”和“ node_exporter ”。
要验证您的安装,请打开 Web 浏览器并访问端口为“9090”的服务器 IP 地址(即:https: //192.168.5.120:9090 /)。
现在,系统将提示您进行基本身份验证,输入用户“ promadmin ”和您创建的密码。

登录后,您应该拥有 promethues 仪表板。输入查询,例如“ node_memory_Active_bytes ”,然后单击“执行”。您应该从已执行的查询中获取指标。
查询“ node_memory_Active_bytes ”由“ node_exporter ”提供,用于检查活动内存。

接下来,单击“状态”菜单并选择“目标”。您应该创建了两个不同的作业,即“ prometheus ”和“ node_exporter ”。“ prometheus ”和“ node_exporter ”作业状态均已启动。

配置了 prometheus 和 node_exporter 后,接下来将 prometheus 作为数据源添加到 grafana。
添加 Prometheus 作为 Grafana 的数据源
Grafana 支持多种数据源,例如 MySQL、PostgreSQL、Influxdb、Graphite、Prometheus 等。在此步骤中,您将添加 promethues 作为 grafana 的数据源。
在 grafana 仪表板上,单击“配置”菜单并选择“数据源”。

现在单击“添加数据源”以添加新数据源。

选择您要添加的数据源类型。这个例子是“普罗米修斯”。

现在输入 prometheus 配置的详细信息 – ‘ prometheus ‘ scrape_config。请务必启用“基本身份验证”和“跳过 TLS 验证”(如果您使用的是自签名证书)。

向下滚动到底部页面并单击“保存并测试”进行确认。如果您有诸如“数据源正在工作”之类的输出消息,那么您就可以开始并将 prometheus 数据源添加到 grafana 了。

设置仪表板监控
将 Prometheus 添加到 Grafana 作为数据源后,您可以创建新的仪表板来监控系统。您可以为每个单元手动创建仪表板,也可以从 Grafana 仪表板存储库导入 Grafana 仪表板的一些示例。
在此步骤中,您将通过将可用仪表板在线导入 Grafana 来设置 Grafana 仪表板。
单击“仪表板”菜单并选择“导入”。

您可以从Grafana Dashboard 存储库 https://grafana.com/grafana/dashboards中找到仪表板的示例 。此示例使用 ID 为“ 15172 ”的 Grafana 仪表板。
输入要导入的仪表板 ID“ 15172 ”,然后单击“加载”。

现在,输入新仪表板的名称并选择“ Prometheus ”作为数据源。单击“导入”进行确认。

在下面的屏幕截图中,确认已创建带有用于监控系统的 prometheus 数据源的新 grafana 仪表板。

原文始发于微信公众号(运维漫谈):如何在 Rocky Linux 9 上安装 Grafana 和 Prometheus?
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/220237.html