Grafana(1)安装和配置

Ubuntu安装

首先需要安装依赖。

1
$ sudo apt-get install -y adduser libfontconfig1

如果使用的是Centos系统,则需要安装一个urw字体依赖。

1
$ sudo yum install urw-fonts

进入Grafana下载页面Download Grafana选择deb安装包,下载直接安装。

1
2
$ wget https://dl.grafana.com/oss/release/grafana_10.0.0_amd64.deb
$ sudo dpkg -i grafana_10.0.0_amd64.deb

启动服务

配置域名相关信息,需要正在/etc/grafana/grafana.ini文件中修改一些配置。

1
2
3
4
5
6
7
8
9
[server]
# Protocol (http, https, h2, socket)
protocol = http
domain = grafana.domain.com
root_url = %(protocol)s://%(domain)s:%(http_port)s/

[live]
allowed_origins = grafana.domain.com

1
2
3
$ sudo /bin/systemctl daemon-reload
$ sudo /bin/systemctl enable grafana-server.service
$ sudo /bin/systemctl start grafana-server.service

检查服务状态。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$  sudo /bin/systemctl status grafana-server.service
● grafana-server.service - Grafana instance
Loaded: loaded (/usr/lib/systemd/system/grafana-server.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-06-09 10:48:41 CST; 1 weeks 6 days ago
Docs: http://docs.grafana.org
Main PID: 3315 (grafana)
Tasks: 13 (limit: 4915)
CGroup: /system.slice/grafana-server.service
└─3315 /usr/share/grafana/bin/grafana server --config=/etc/grafana/grafana.ini --pidfile=/run/grafana/grafana-server.pid --packaging=deb cfg:default.paths.logs=/var/log/grafana cfg:default.paths.data=/var/lib/grafana cfg:default.paths.

Jun 22 13:45:16 VM-0-5-ubuntu grafana[3315]: logger=context userId=0 orgId=0 uname= t=2023-06-22T13:45:16.309927024+08:00 level=info msg="Request Completed" method=GET path=/favicon.ico status=302 remote_addr=127.0.0.1 time_ms=0 duration=391.531µJun 22 13:48:48 VM-0-5-ubuntu grafana[3315]: logger=cleanup t=2023-06-22T13:48:48.324101724+08:00 level=info msg="Completed cleanup jobs" duration=35.652862ms
Jun 22 13:48:49 VM-0-5-ubuntu grafana[3315]: logger=plugins.update.checker t=2023-06-22T13:48:49.317745125+08:00 level=info msg="Update check succeeded" duration=290.429755ms
Jun 22 13:48:49 VM-0-5-ubuntu grafana[3315]: logger=grafana.update.checker t=2023-06-22T13:48:49.621393118+08:00 level=info msg="Update check succeeded" duration=741.522769ms
Jun 22 13:58:48 VM-0-5-ubuntu grafana[3315]: logger=cleanup t=2023-06-22T13:58:48.323309543+08:00 level=info msg="Completed cleanup jobs" duration=35.628673ms
Jun 22 13:58:49 VM-0-5-ubuntu grafana[3315]: logger=grafana.update.checker t=2023-06-22T13:58:49.125999268+08:00 level=info msg="Update check succeeded" duration=246.277611ms
Jun 22 13:58:49 VM-0-5-ubuntu grafana[3315]: logger=plugins.update.checker t=2023-06-22T13:58:49.361911566+08:00 level=info msg="Update check succeeded" duration=334.347133ms
Jun 22 14:08:48 VM-0-5-ubuntu grafana[3315]: logger=cleanup t=2023-06-22T14:08:48.308940924+08:00 level=info msg="Completed cleanup jobs" duration=20.468025ms
Jun 22 14:08:49 VM-0-5-ubuntu grafana[3315]: logger=plugins.update.checker t=2023-06-22T14:08:49.318130025+08:00 level=info msg="Update check succeeded" duration=291.288081ms
Jun 22 14:08:50 VM-0-5-ubuntu grafana[3315]: logger=grafana.update.checker t=2023-06-22T14:08:50.228624628+08:00 level=info msg="Update check succeeded" duration=1.348194856s

grafana默认监听3000端口,检查端口状态。

1
2
3
4
$ netstat -nlpt | grep 3000
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp6 0 0 :::3000 :::* LISTEN -

如果出现启动错误,比如下面的错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ sudo /bin/systemctl status grafana-server.service
● grafana-server.service - Grafana instance
Loaded: loaded (/usr/lib/systemd/system/grafana-server.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2024-08-31 11:26:09 CST; 3min 11s ago
Docs: http://docs.grafana.org
Process: 10315 ExecStart=/usr/share/grafana/bin/grafana server --config=${CONF_FILE} --pidfile=${PID_FILE_DIR}/grafana-server.pid --packaging=deb cfg:default.paths.logs=${LOG_DIR} cfg:default.paths.data=${DATA_DIR} cfg:default.paths.plugins=${PLUGINS_DIR} cfg:default.paths.provisioning=${PROVISIONING_CFG_DIR} (code Main PID: 10315 (code=exited, status=1/FAILURE)

Aug 31 11:26:09 VM-0-9-ubuntu systemd[1]: grafana-server.service: Service hold-off time over, scheduling restart.
Aug 31 11:26:09 VM-0-9-ubuntu systemd[1]: grafana-server.service: Scheduled restart job, restart counter is at 19.
Aug 31 11:26:09 VM-0-9-ubuntu systemd[1]: Stopped Grafana instance.
Aug 31 11:26:09 VM-0-9-ubuntu systemd[1]: grafana-server.service: Start request repeated too quickly.
Aug 31 11:26:09 VM-0-9-ubuntu systemd[1]: grafana-server.service: Failed with result 'exit-code'.
Aug 31 11:26:09 VM-0-9-ubuntu systemd[1]: Failed to start Grafana instance.

可以去/var/log/grafana/grafana.log查看具体的启动日志。

默认启动后的admin账号和密码是adminadmin,第一次进入之后会要求修改账号密码。

配置nginx

配置nginx通过80端口转发到3000端口,同时还要转发websocket请求。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
listen 80;
server_name grafana.domain.com;
location / {
proxy_set_header Host $http_host;
proxy_pass http://localhost:3000/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /api/live/ws {
rewrite ^/(.*) /$1 break;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $http_host;
proxy_pass http://localhost:3000/;
}
}

这里一定要设置proxy_set_header Host $http_host;这个header,不然会出现访问错误。