Grafana是一种开源数据可视化和监控工具,可与来自以下来源的复杂数据集成普罗米修斯, InfluxDB, Graphite, and 弹性搜索。 Grafana 允许您为数据创建警报、通知和临时过滤器,同时还可以通过内置共享功能更轻松地与团队成员协作。
在本教程中,您将安装 Grafana 并使用SSL证书 and an Nginx 反向代理。设置 Grafana 后,您可以选择通过 GitHub 配置用户身份验证,以便您更好地组织团队权限。
要学习本教程,您将需要:
- 按照以下步骤设置一台 Ubuntu 22.04 服务器Ubuntu 22.04 初始服务器设置指南,包括非 root 用户
sudo
权限和防火墙配置ufw
.
- A fully registered domain name. This tutorial uses
your_domain
throughout. You can purchase a domain name on Namecheap, get one for free on Freenom, or use the domain registrar of your choice.
- The following DNS records set up for your server. You can follow the How To Add Domains article for details on how to add them if you are using DigitalOcean.
- An A record with
your_domain
pointing to your server’s public IP address.
- An A record with
www.your_domain
pointing to your server’s public IP address.
- Nginx 设置如下如何在 Ubuntu 22.04 上安装 Nginx教程,包括服务器块为您的域名。
- 配置了 Let's Encrypt 的 Nginx 服务器块,您可以通过以下方式进行设置如何在 Ubuntu 22.04 上使用 Let’s Encrypt 保护 Nginx.
- 可选地,设置GitHub身份验证,您需要一个与组织关联的 GitHub 帐户.
在第一步中,您将把 Grafana 安装到 Ubuntu 22.04 服务器上。您可以通过以下方式安装 Grafana直接从其官方网站下载或者通过APT 存储库。由于 APT 存储库可以更轻松地安装和管理 Grafana 的更新,因此您将在本教程中使用该方法。
Download the Grafana GPG key with wget, then pipe the output to gpg
. This will convert the GPG key from base64 to binary format. Then pipe the output to tee
to store the key in the /usr/share/keyrings/grafana.gpg
file.
-
wget -q -O - https://packages.grafana.com/gpg.key | gpg --dearmor | sudo tee /usr/share/keyrings/grafana.gpg > /dev/null
在此命令中,选项-q
关闭状态更新消息wget
, and -O
输出您下载到终端的文件。这两个选项可确保仅对下载文件的内容进行管道传输。这> /dev/null
出于安全原因,选项将隐藏终端的输出。
接下来,将 Grafana 存储库添加到您的 APT 源:
-
echo "deb [signed-by=/usr/share/keyrings/grafana.gpg] https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
刷新 APT 缓存以更新包列表:
您现在可以继续安装:
安装 Grafana 后,使用systemctl
启动 Grafana 服务器:
-
sudosystemctl 启动 grafana-server
接下来,通过检查服务的状态来验证 Grafana 是否正在运行:
-
sudosystemctl 状态 grafana 服务器
您将收到与此类似的输出:
Output
● grafana-server.service - Grafana instance
Loaded: loaded (/lib/systemd/system/grafana-server.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2022-09-27 14:42:15 UTC; 6s ago
Docs: http://docs.grafana.org
Main PID: 4132 (grafana-server)
Tasks: 7 (limit: 515)
...
This output contains information about Grafana’s process, including its status, Main Process Identifier (PID), and more. active (running)
shows that the process is running correctly.
最后,启用服务在启动时自动启动 Grafana:
您将收到以下输出:
Output
Synchronizing state of grafana-server.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable grafana-server
Created symlink /etc/systemd/system/multi-user.target.wants/grafana-server.service → /usr/lib/systemd/system/grafana-server.service.
这证实了systemd
已创建自动启动 Grafana 所需的符号链接。
Grafana 现已安装并可供使用。接下来,您将使用反向代理和 SSL 证书来保护与 Grafana 的连接。
使用 SSL 证书将通过加密与 Grafana 的连接来确保您的数据安全。但是,要使用此连接,您首先需要将 Nginx 重新配置为 Grafana 的反向代理。
打开您在使用 Let’s Encrypt 设置 Nginx 服务器块时创建的 Nginx 配置文件先决条件。您可以使用任何文本编辑器,但在本教程中我们将使用nano
:
-
sudo nano /etc/nginx/sites-available/your_domain
找到以下块:
/etc/nginx/sites-available/your_domain
...
location / {
try_files $uri $uri/ =404;
}
...
因为您已经将 Nginx 配置为通过 SSL 进行通信,并且由于到您服务器的所有 Web 流量都已通过 Nginx,所以您只需告诉 Nginx 将所有请求转发到在端口上运行的 Grafana3000
默认情况下。
删除现有的try_files
在此行location block
并将其替换为以下选项:
/etc/nginx/sites-available/your_domain
...
location / {
proxy_set_header Host $http_host;
proxy_pass http://localhost:3000;
}
...
这会将代理映射到适当的端口,并在标头中传递服务器名称。
另外,为了格拉法纳现场要使 WebSocket 连接正常工作,请在server
部分:
/etc/nginx/sites-available/your_domain
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
...
然后添加以下内容location
服务器块的部分:
/etc/nginx/sites-available/your_domain
server {
...
location /api/live {
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;
}
...
标头Upgrade
and Connection
不会从客户端发送到代理服务器。因此,为了让代理服务器了解客户端将协议更改为 WebSocket 的意图,应该显式传递这些标头。
最终配置将如下所示:
/etc/nginx/sites-available/your_domain
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
...
root /var/www/your_domain/html;
index index.html index.htm index.nginx-debian.html;
server_name your_domain www.your_domain;
location / {
proxy_set_header Host $http_host;
proxy_pass http://localhost:3000;
}
location /api/live {
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;
}
...
}
Note:如果您使用了certbot
工具来设置 Let’s Encrypt SSL 证书,那么您的配置文件可能包含由 Certbot 添加的其他行和服务器块。
完成后,按保存并关闭文件CTRL+X
, Y
, 进而ENTER
如果你正在使用nano
.
现在,测试新设置以确保一切配置正确:
您将收到以下输出:
Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
最后,通过重新加载 Nginx 来激活更改:
You can now access the default Grafana login screen by pointing your web browser to https://your_domain
. If you’re unable to reach Grafana, verify that your firewall is set to allow traffic on port 443
and then re-trace the previous instructions.
通过加密与 Grafana 的连接,您现在可以实施额外的安全措施,首先是更改 Grafana 的默认管理凭据。
由于默认情况下每个 Grafana 安装都使用相同的管理凭据,因此最佳实践是尽快更改您的登录信息。在此步骤中,您将更新凭据以提高安全性。
Start by navigating to https://your_domain
from your web browser. This will bring up the default login screen where you’ll see the Grafana logo, a form asking you to enter an Email or username and Password, a Log in button, and a Forgot your password? link.
Enter admin
进入两个电子邮件或用户名 and Password字段,然后单击Log in button.
在下一个屏幕上,系统会要求您通过更改默认密码来提高帐户的安全性:
输入您要开始使用的密码新密码 and 确认新密码 fields.
从这里,您可以单击Submit保存新信息或按Skip跳过此步骤。如果跳过,下次登录时系统会提示您更改密码。
为了提高 Grafana 设置的安全性,请单击Submit。您将前往欢迎来到格拉法纳仪表板:
现在,您已通过更改默认凭据来保护您的帐户。接下来,您将更改 Grafana 配置,以便没有人可以在未经您许可的情况下创建新的 Grafana 帐户。
Grafana 提供的选项允许访问者为自己创建用户帐户并预览仪表板而无需注册。当 Grafana 无法通过互联网访问或正在处理服务状态等公开数据时,您可能需要允许这些功能。然而,当在线使用 Grafana 处理敏感数据时,匿名访问可能会出现安全问题。要解决此问题,您需要对 Grafana 配置进行一些更改。
首先打开 Grafana 的主配置文件进行编辑:
-
sudo nano/etc/grafana/grafana.ini
找到以下内容allow_sign_up
指令下的[users]
标题:
/etc/grafana/grafana.ini
...
[users]
# disable user signup / registration
;allow_sign_up = true
...
启用该指令true
adds a Sign Up按钮到登录屏幕,允许用户自行注册并访问 Grafana。
禁用该指令false
删除Sign Up按钮并增强 Grafana 的安全性和隐私性。
通过删除来取消注释该指令;
在行的开头,然后将选项设置为false
:
/etc/grafana/grafana.ini
...
[users]
# disable user signup / registration
allow_sign_up = false
...
接下来,找到以下内容enabled
指令下的[auth.anonymous]
标题:
/etc/grafana/grafana.ini
...
[auth.anonymous]
# enable anonymous access
;enabled = false
...
Setting enabled
to true
允许非注册用户访问您的仪表板;将此选项设置为false
仅限注册用户访问仪表板。
通过删除来取消注释该指令;
在行的开头,然后将选项设置为false
.
/etc/grafana/grafana.ini
...
[auth.anonymous]
# enable anonymous access
enabled = false
...
保存文件并退出文本编辑器。
要激活更改,请重新启动 Grafana:
-
sudosystemctl 重新启动 grafana-server
通过检查 Grafana 的服务状态来验证一切正常:
-
sudosystemctl 状态 grafana 服务器
和以前一样,输出将报告 Grafana 是active (running)
.
Now, point your web browser to https://your_domain
. To return to the Sign Up screen, bring your cursor to your avatar in the lower left of the screen and click on the Sign out option that appears.
退出后,请确认没有Sign Up按钮,并且如果不输入登录凭据,您将无法登录。
至此,Grafana 已完全配置并可供使用。接下来,您可以通过 GitHub 进行身份验证来简化组织的登录过程。
对于登录的替代方法,您可以将 Grafana 配置为通过 GitHub 进行身份验证,这为授权 GitHub 组织的所有成员提供登录访问权限。当您希望允许多个开发人员协作和访问指标而无需创建 Grafana 特定的凭据时,这尤其有用。
首先登录与您的组织关联的 GitHub 帐户,然后导航到您的GitHub 个人资料页面.
通过单击切换设置上下文切换到另一个帐户屏幕顶部的链接,然后在下拉菜单中选择您的组织。这将切换上下文个人设置 to 组织设置.
在下一个屏幕上,您将看到您的组织简介您可以在其中更改设置,例如您的组织显示名称, 组织Email和组织URL.
因为 Grafana 使用OAuth— 授予远程第三方访问本地资源的开放标准 — 要通过 GitHub 对用户进行身份验证,您需要创建一个新的GitHub 中的 OAuth 应用程序.
点击OAuth 应用程序链接下开发者设置在屏幕的左下侧。
如果您在 GitHub 上还没有与您的组织关联的任何 OAuth 应用程序,系统会告诉您有没有组织拥有的应用程序。否则,您将看到已连接到您的帐户的 OAuth 应用程序的列表。
点击注册申请按钮继续。
在下一个屏幕上,填写有关 Grafana 安装的以下详细信息:
-
应用名称:这可以帮助您区分不同的 OAuth 应用程序。
-
Homepage URL: This tells GitHub where to find Grafana. Type
https://your_domain
into this field, replacing your_domain
with your domain.
-
应用说明:这提供了 OAuth 应用程序用途的描述。
-
Application callback URL: This is the address where users will be sent once successfully authenticated. For Grafana, this field must be set to
https://your_domain/login/github
.
请记住,通过 GitHub 登录的 Grafana 用户将看到您在前三个字段中输入的值,因此请务必输入有意义且适当的内容。
完成后,表格将如下所示:
点击注册申请 button.
您现在将被重定向到一个页面,其中包含有关新 OAuth 应用程序的一般信息,包括客户ID。然后单击生成新的客户端密钥按钮获取新的客户秘密。记下这两个值,因为您需要将它们添加到 Grafana 的主配置文件中才能完成设置。
Warning:请务必保留您的客户ID and 客户秘密在安全且非公共的位置,因为它们可以用作攻击的基础。
创建 GitHub OAuth 应用程序后,您现在可以重新配置 Grafana 以使用 GitHub 进行身份验证。
要完成 Grafana 设置的 GitHub 身份验证,您现在将对 Grafana 配置文件进行一些更改。
首先,打开 Grafana 主配置文件:
-
sudo nano/etc/grafana/grafana.ini
找到[auth.github]
标题,并通过删除;
除以下内容外,在每一行的开头,本教程中不会更改这些内容:
;allowed_domains =
;team_ids =
;role_attribute_path =
;role_attribute_strict = false
;allow_assign_grafana_admin = false
接下来,进行以下更改:
- Set
enabled
and allow_sign_up
to true
。这将启用 GitHub 身份验证并允许允许组织的成员自己创建帐户。请注意,此设置与allow_sign_up
下的财产[users]
你改变了Step 4.
- Set
client_id
and client_secret
为您在创建 GitHub OAuth 应用程序时获得的值。
- Set
allowed_organizations
添加到您的组织名称,以确保只有您组织的成员才能注册并登录 Grafana。
完整的配置将如下所示:
/etc/grafana/grafana.ini
...
[auth.github]
enabled = true
allow_sign_up = true
client_id = your_client_id_from_github
client_secret = your_client_secret_from_github
scopes = user:email,read:org
auth_url = https://github.com/login/oauth/authorize
token_url = https://github.com/login/oauth/access_token
api_url = https://api.github.com/user
;allowed_domains =
;team_ids =
allowed_organizations = your_organization_name
;role_attribute_path =
;role_attribute_strict = false
;allow_assign_grafana_admin = false
...
您现在已经告诉 Grafana 它需要了解的有关 GitHub 的所有信息。要完成设置,您需要启用反向代理后面的重定向。这是通过设置一个来完成的root_url
下的值[server]
标题。
/etc/grafana/grafana.ini
...
[server]
root_url = https://your_domain
...
保存您的配置并关闭文件。
然后,重新启动 Grafana 以激活更改:
-
sudosystemctl 重新启动 grafana-server
最后,验证服务是否已启动并正在运行:
-
sudosystemctl 状态 grafana 服务器
输出将表明该服务是active (running)
.
Now, test your new authentication system by navigating to https://your_domain
. If you are already logged in to Grafana, hover your mouse over the avatar log in the lower left-hand corner of the screen, and click on Sign out in the secondary menu that appears next to your name.
在登录页面上,您将在原始页面下看到一个新部分Log in按钮,其中包括使用 GitHub 登录带有 GitHub 徽标的按钮。
单击使用 GitHub 登录按钮将重定向到 GitHub,您将在其中登录您的 GitHub 帐户并确认您的意图授权Grafana.
Click the green Authorize your_github_organization button.
Note:确保您的 GitHub 帐户是您批准的组织的成员,并且您的 Grafana 电子邮件地址与您的 GitHub 电子邮件地址匹配。如果您尝试使用不属于您批准的组织成员的 GitHub 帐户进行身份验证,您将收到登录失败消息告诉你用户不是所需组织之一的成员.
您现在将使用现有的 Grafana 帐户登录。如果您登录的用户尚不存在 Grafana 帐户,Grafana 将使用以下命令创建一个新用户帐户Viewer权限,确保新用户只能使用现有的仪表板。
要更改新用户的默认权限,请打开主 Grafana 配置文件进行编辑。
-
sudo nano/etc/grafana/grafana.ini
找到auto_assign_org_role
指令下的[users]
标题,并通过删除来取消注释设置;
在该行的开头。
将指令设置为以下值之一:
-
Viewer
:只能使用现有的仪表板。
-
Editor
:可以使用、修改和添加仪表板。
-
Admin
: 有权做任何事情。
本教程将自动分配设置为Viewer
:
/etc/grafana/grafana.ini
...
[users]
...
auto_assign_org_role = Viewer
...
保存更改后,关闭文件并重新启动 Grafana:
-
sudosystemctl 重新启动 grafana-server
检查服务的状态:
-
sudosystemctl 状态 grafana 服务器
像以前一样,状态将显示为active (running)
.
此时,您已完全配置 Grafana,以允许 GitHub 组织的成员注册和使用您的 Grafana 安装。
在本教程中,您安装、配置并保护了 Grafana,还了解了如何允许组织成员通过 GitHub 进行身份验证。
要扩展您当前的 Grafana 安装,请参阅官方和社区构建的列表仪表板 and plugins。要了解有关使用 Grafana 的更多信息,请参阅Grafana 官方文档,或查看我们的其他监控教程.