如何在 CentOS 8 上安装 Grafana

什么是格拉法纳?

本文是关于如何在 CentOS 8 服务器上安装 Grafana 软件的分步指南。 Grafana 是一种流行的开源可视化和分析监控软件。 当连接到支持的数据源时,它会呈现图形、图表和警报。 它通常与 Prometheus 等时间序列数据库、MySQL 日志记录等 SQL 数据库和 Loki 等文档数据库一起使用。您还可以从官方库中安装数百个插件和仪表板。

安装

与往常一样,建议在安装任何新工具之前更新我们的服务器软件。 如果服务器有一段时间没有更新,可能需要几分钟来更新所有的包。 通过这个安装过程,我将以 root 用户身份登录,您应该也是。

[root@host ~]# dnf update
Last metadata expiration check: 0:54:47 ago on Fri Apr 17 09:40:56 2020.
Dependencies resolved.
Nothing to do.
Complete!
[root@host ~]#

[root@host ~]# yum update
Last metadata expiration check: 0:55:22 ago on Fri Apr 17 09:40:56 2020.
Dependencies resolved.
Nothing to do.
Complete!
[root@host ~]#

服务器更新后,我们将在 /etc/yum.repos.d/grafana.repo 中创建一个名为“grafana.repo”的新文件,以将 Grafana 存储库添加到我们的本地 YUM 存储库。

touch grafana.repo

接下来,我们将运行以下命令将存储库添加到我们的 /etc/yum.repos.d/grafana.repo 文件中。 该命令应如下所示。

cat <<EOF | sudo tee /etc/yum.repos.d/grafana.repo
[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
EOF

我们上面使用的“Baseurl”将决定我们要安装哪个版本的 Grafana。 在这种情况下,我们使用了“baseurl=https://packages.grafana.com/oss/rpm”,这是 Grafana 的开源版本。

如果我们想安装企业版,我们会相应地使用这个 baseurl:“baseurl=https://packages.grafana.com/enterprise/rpm”

接下来,我们需要运行另一个更新,将 Grafana 添加到 repo 并导入 GPG 密钥。 系统将询问您是否要从 Grafana 导入 GPG 密钥,输入“y”并继续。

[root@host ~]# dnf update
grafana                     1.1 kB/s | 488  B     00:00
grafana                     17 kB/s | 1.7 kB     00:00
Importing GPG key 0x24098CB6:
 Userid     : "Grafana <[email protected]>"
 Fingerprint: 4E40 DDF6 D76E 284A 4A67 80E4 8C8C 34C5 2409 8CB6
 From       : https://packages.grafana.com/gpg.key
Is this ok [y/N]: y
grafana                     2.4 MB/s | 2.5 MB     00:01
Last metadata expiration check: 0:00:01 ago on Fri Apr 17 14:56:02 2020.
Dependencies resolved.
Nothing to do.
Complete!
[root@host ~]#

现在,我们将运行以下命令之一来安装 Grafana:

[root@host ~]# dnf install grafana

或者

[root@host ~]# dnf install grafana-enterprise

当系统询问您是否要安装 Grafana 时,按“y”。 系统会再次询问您是否要从 Grafana 导入 GPG 密钥,点击“y”继续安装。

[root@host ~]# dnf install grafana
Last metadata expiration check: 0:02:30 ago on Fri Apr 17 14:56:02 2020.
Dependencies resolved.
============================================================================
 Package                    Architecture     Version             Repository           Size
============================================================================
Installing:
 grafana                     x86_64           6.7.2-1            grafana              61 M
Installing dependencies:
 libICE                      x86_64           1.0.9-15.el8       AppStream            74 k
 libSM                       x86_64           1.2.3-1.el8                  

Transaction Summary
============================================================================
Install  37 Packages

Total download size: 70 M
Installed size: 194 M
Is this ok [y/N]: y
Downloading Packages:
(1/37): libSM-1.2.3-1.el8.x86_64.rpm                    74 kB/s |  48 kB     00:00
(2/37): libICE-1.0.9-15.el8.x86_64.rpm                  93 kB/s |  74 kB     00:00
(3/37): libXau-1.0.8-13.el8.x86_64.rpm                  76 kB/s |  36 kB     00:00
(4/37): libX11-common-1.6.7-1.el8.noarch.rpm            176 kB/s | 157 kB     00:00

----------------------------------------------------------------------------
Total                                                   4.5 MB/s |  70 MB     00:15
warning: /var/cache/dnf/grafana-13e251e56a0c8862/packages/grafana-6.7.2-1.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 24098cb6: NOKEY
grafana                                                 13 kB/s | 1.7 kB     00:00
Importing GPG key 0x24098CB6:
 Userid     : "Grafana <[email protected]>"
 Fingerprint: 4E40 DDF6 D76E 284A 4A67 80E4 8C8C 34C5 2409 8CB6
 From       : https://packages.grafana.com/gpg.key
Is this ok [y/N]: y
Key imported successfully
Running transaction check

  Installing       : grafana-6.7.2-1.x86_64             37/37
  Running scriptlet: grafana-6.7.2-1.x86_64             37/37
### NOT starting on installation, please execute the following statements to configure grafana to start automatically using systemd
 sudo /bin/systemctl daemon-reload
 sudo /bin/systemctl enable grafana-server.service
### You can start grafana-server by executing
 sudo /bin/systemctl start grafana-server.service

Installed:
  grafana-6.7.2-1.x86_64                                  libICE-1.0.9-15.el8.x86_64
  libSM-1.2.3-1.el8.x86_64                                libX11-1.6.7-1.el8.x86_64
  libX11-common-1.6.7-1.el8.noarch                        libXau-1.0.8-
  xorg-x11-server-utils-7.7-27.el8.x86_64                 fontconfig-2.13.1-3.el8.x86_64
  fontpackages-filesystem-1.44-22.el8.noarch

Complete!
[root@host ~]#

正如您在上面的输出中看到的,Grafana 将不会 在系统启动时自动启动。 为了解决这个问题,我们将使用以下命令启动并检查 Grafana 服务的状态。 下面的最后一步是将 Grafana 配置为在必要时在系统启动时启动:

[root@host ~]# systemctl daemon-reload

[root@host ~]# systemctl start grafana-server

[root@host ~]# systemctl status grafana-server
● grafana-server.service - Grafana instance
   Loaded: loaded (/usr/lib/systemd/system/grafana-server.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2020-04-17 15:06:07 EDT; 12s ago
     Docs: https://docs.grafana.org
 Main PID: 4870 (grafana-server)
    Tasks: 9 (limit: 10686)
   Memory: 20.6M
   CGroup: /system.slice/grafana-server.service
           └─4870 /usr/sbin/grafana-server --config=/etc/grafana/grafana.ini --pidfile=/var/run/grafana/grafana-server.>

Apr 17 15:06:07 host.lwkbcentos2.com grafana-server[4870]: t=2020-04-17T15:06:07-0400 lvl=info msg="Initializing Tracin>
Apr 17 15:06:07 host.lwkbcentos2.com grafana-server[4870]: t=2020-04-17T15:06:07-0400 lvl=info msg="Initializing UsageS>
Apr 17 15:06:07 host.lwkbcentos2.com grafana-server[4870]: t=2020-04-17T15:06:07-0400 lvl=info msg="Initializing CleanU>
Apr 17 15:06:07 host.lwkbcentos2.com grafana-server[4870]: t=2020-04-17T15:06:07-0400 lvl=info msg="Initializing Notifi>
Apr 17 15:06:07 host.lwkbcentos2.com grafana-server[4870]: t=2020-04-17T15:06:07-0400 lvl=info msg="Initializing provis>
Apr 17 15:06:07 host.lwkbcentos2.com grafana-server[4870]: t=2020-04-17T15:06:07-0400 lvl=info msg="Backend rendering v>
Apr 17 15:06:07 host.lwkbcentos2.com grafana-server[4870]: t=2020-04-17T15:06:07-0400 lvl=warn msg="phantomJS is deprec>
Apr 17 15:06:07 host.lwkbcentos2.com grafana-server[4870]: t=2020-04-17T15:06:07-0400 lvl=info msg="Initializing Stream>
Apr 17 15:06:07 host.lwkbcentos2.com grafana-server[4870]: t=2020-04-17T15:06:07-0400 lvl=info msg="HTTP Server Listen">
Apr 17 15:06:07 host.lwkbcentos2.com systemd[1]: Started Grafana instance.

[root@host ~]# systemctl enable grafana-server.service
Synchronizing state of grafana-server.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/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.
[root@host ~]#

添加防火墙规则

由于 Grafana 在默认端口 3000 上运行,因此我们需要在防火墙中打开该端口。 这是访问 Grafana Web 界面所必需的。 要在 CentOS 服务器上打开端口 3000,请运行以下命令。

[root@host ~]# firewall-cmd --add-port=3000/tcp --permanent
success
[root@host ~]# firewall-cmd --reload
success
[root@host ~]#

让我们验证端口 3000 现在是否已打开。

[root@host ~]# firewall-cmd --list-all | grep 3000
  ports: 3000/tcp
[root@host ~]#

Grafana 文件位置

最后,Grafana 将文件安装到以下默认位置。

  • 默认环境 vars 文件位于 /etc/sysconfig/grafana-server
  • 默认二进制安装位于 /usr/sbin/grafana-server
  • 默认的 systemd 服务(如果 systemd 可用)名为 grafana-server.service
  • 默认的 init.d 脚本位于 /etc/init.d/grafana-server
  • 默认配置文件位置是 /etc/grafana/grafana.ini
  • 默认配置使用位于 /var/log/grafana/grafana.log 的日志文件
  • 默认配置指定使用位于 /var/lib/grafana/grafana.db 的 sqlite3 数据库
  • 主路径位于 /usr/share/grafana/

错误解决

如果您收到错误消息:
“Grafana-server 初始化失败:找不到配置默认值,请确保设置了主路径命令行参数或工作目录是主路径”
运行以下命令来设置正确的主路径。

[root@host ~]# grafana-server -homepath /usr/share/grafana/

如果您收到错误:此站点无法提供安全连接,请使用 vim 打开 grafana.ini 文件并找到以下选项 http_port = 3000 并取消注释该行。

[root@host ~]# vim /etc/grafana/grafana.ini

# The http port  to use
# http_port = 3000

change to 

# The http port  to use
 http_port = 3000

现在您可以通过链接 https://se.rv.er.ip:3000 访问 Grafana 用户界面。 我们现在可以登录以开始配置我们的第一个 Grafana 服务器。

Grafana 登录

下一步是登录 Grafana 仪表板。 默认用户名和密码为:

  • 用户:管理员
  • 密码:管理员

然后,单击“登录”按钮。

登录后,您将看到更改密码屏幕。

更新仪表板密码,然后单击“Save“ 按钮。

打开的屏幕将是 Grafana 仪表板。

04.grafana.dashboard.full

创建仪表板

接下来的步骤将帮助您开始配置 Grafana。

03.grafana.dashboard
  1. 点击 新仪表板.
  2. 然后,选择 添加查询. 这告诉 Grafana 使用 Random Walk 场景创建一个基本图形面板。
  3. 最后,通过单击“保存新仪表板Save 仪表盘” 屏幕上角的图标。

结论

就是这样! 您现在已经使用基本配置设置了 Grafana! 继续探索设置和构建的内容,然后通过添加另一个数据源来扩展其功能,或者探索 下一步 在格拉法纳。

今天就开始吧!

给我们打电话 800.580.4985,或打开 聊天 或与我们联系,与我们知识渊博的解决方案或经验丰富的托管顾问交谈,了解您今天如何利用这项技术!