【前情提要】 PromQL 入门了,小林能查出 CPU、内存、磁盘的使用率了。老周说:“现在你能查数据了,但数据是一堆数字,没人愿意看。你得把它变成图。”小林打开 Grafana 官网,准备装 Grafana。但他不知道,数据源配置、Dashboard 导入、Panel 查询,每一个都可能让他折腾。

一、老周的审美第二击

早上到工位,小林打开 Prometheus 的查询框,得意地敲了一串 PromQL:

1 - avg(rate(node_cpu_seconds_total{mode="idle"}[1m]))

结果:

{} 0.0234

“周哥,你看,CPU 使用率 2.34%。”

老周看了一眼,沉默了三秒。

“你知道这玩意儿像什么吗?”

“像什么?”

“像医院的心电图——全是数字,只有医生能看懂。”老周说,“你老板要看服务器状态,你给他看这个?”

小林想了想:“那给他看什么?”

“图。”老周说,“老板要的不是数字,是红绿黄。绿色正常,黄色警告,红色告警。一眼看懂。”

“那用什么画?”

“Grafana。”老周说,“Prometheus 负责存数据,Grafana 负责把数据变成图。你以后做监控,Prometheus 是后台,Grafana 是前台。老板只看前台。”

小林打开 Grafana 官网。

二、安装 Grafana

“Ubuntu 上装 Grafana,最标准的方式是加官方源。”老周说,“比二进制简单,而且自带 systemd 服务。”

“那跟 apt 装一样?”

“对,但要先加 Grafana 的官方源。”老周说,“你跟着敲。”

小林先装依赖:

sudo apt update
sudo apt install -y apt-transport-https software-properties-common wget

“这几个是啥?”

“apt-transport-https 让 apt 支持 HTTPS 源,software-properties-common 提供 add-apt-repository 命令,wget 下载工具。”老周说,“基础依赖,先装上。”

然后加 Grafana 的 GPG 密钥:

sudo mkdir -p /etc/apt/keyrings/
wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/grafana.gpg > /dev/null

“GPG 密钥是啥?”

“软件源的签名验证。”老周说,“你从 Grafana 源下载软件,得验证这个源是不是官方的。GPG 密钥就是‘官方盖章’。没有它,apt 不敢装。”

然后加源:

echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" | sudo tee /etc/apt/sources.list.d/grafana.list

“signed-by 指定密钥路径,stable main 表示稳定版主仓库。”老周说,“现在更新源,装 Grafana。”

sudo apt update
sudo apt install -y grafana

安装完成。

“启动。”

sudo systemctl start grafana-server
sudo systemctl enable grafana-server
sudo systemctl status grafana-server

输出:

● grafana-server.service - Grafana instance
     Loaded: loaded (/lib/systemd/system/grafana-server.service; enabled; vendor preset: enabled)
     Active: active (running) since ...

“active (running),成了。”小林说。

“验证端口。”

sudo ss -lntp | grep 3000

输出:

LISTEN 0 4096 *:3000 *:* users:(("grafana-server",pid=1234,fd=7))

“3000 端口。”老周说,“打开浏览器,访问 http://localhost:3000。”

小林在 Ubuntu 里打开 Firefox,输入地址。

Grafana 的登录页面出来了。

“默认用户名和密码都是 admin。”老周说,“第一次登录会让你改密码。你改一个,反正是虚拟机。”

小林登录,改了密码,进入了 Grafana 首页。

三、添加数据源:让 Grafana 连上 Prometheus

“Grafana 装好了,但它现在是‘瞎子’。”老周说,“它不知道数据在哪。你得告诉它,数据在 Prometheus。”

“怎么告诉?”

“添加数据源。”老周说,“左侧菜单 → Connections → Data sources → Add data source。”

小林点进去,看到一列数据源类型:Prometheus、MySQL、PostgreSQL、Elasticsearch……

“选 Prometheus。”

点进去,配置页面出来了。

“URL 填什么?”

“http://localhost:9090。”老周说,“Prometheus 的地址。Grafana 和 Prometheus 在同一台机器上,用 localhost 就行。”

小林填了 URL,其他默认。

“点 Save & test。”

小林点了一下。页面提示:

Data source is working

“成了。”小林说,“Grafana 能连上 Prometheus 了。”

“这就是数据源。”老周说,“Grafana 不存数据,它只负责查询和展示。数据在 Prometheus 里,Grafana 通过数据源去查。”

小林记下:

  • Grafana 默认端口:3000
  • 默认用户名/密码:admin / admin
  • 数据源 URL:http://localhost:9090
  • 添加数据源后,点 Save & test 验证

四、创建第一个 Dashboard

“数据源配好了,现在建 Dashboard。”老周说,“左侧菜单 → Dashboards → New → New dashboard。”

小林点进去,看到一个空白的 Dashboard。

“点 Add visualization。”

弹出一个 Panel 编辑界面。上面是数据源选择,下面是查询框。

“选 Prometheus 数据源。”老周说,“然后查询框里敲 PromQL。”

小林敲:

1 - avg(rate(node_cpu_seconds_total{mode="idle"}[1m]))

“点 Run query。”

图表出来了——一条曲线,在 2% 左右波动。

“看到了吗?”老周说,“这就是 CPU 使用率。你给它起个名字。”

小林在 Panel 标题里写:“CPU 使用率”。

“保存 Dashboard。”

老周让小林点右上角 Save dashboard,命名 我的服务器监控。

“现在你有一个 Dashboard 了。”老周说,“但只有一个 Panel。你再加几个。”

五、加 Panel:内存、磁盘、网络

“加内存 Panel。”老周说。

小林点 Add → Visualization,敲:

1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes

标题写:“内存使用率”。

“加磁盘 Panel。”

1 - node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"}

标题写:“根分区使用率”。

“加网络 Panel。”

rate(node_network_receive_bytes_total[1m])

标题写:“网络接收速率”。

“再加一个系统负载。”

node_load1

标题写:“1 分钟负载”。

“保存。”

小林保存 Dashboard,现在有五个 Panel:CPU、内存、磁盘、网络、负载。

“看着还行。”老周说,“但你这个 Dashboard 是手搓的,不够专业。Grafana 有现成的模板,你直接导入。”

六、导入 Node Exporter Full 仪表盘

“Grafana 社区有大量现成的 Dashboard 模板。”老周说,“最出名的是 Node Exporter Full,ID 是 1860。它把 node_exporter 的所有指标都做成图,你直接导入就能用。”

“怎么导入?”

“左侧菜单 → Dashboards → New → Import。”

小林点进去,看到一个输入框:“Import via grafana.com”。

“输入 1860,点 Load。”

页面加载出来,显示 Dashboard 名称、数据源选择。

“数据源选 Prometheus。”老周说,“点 Import。”

几秒钟后,一个完整的 Dashboard 出现了。

CPU、内存、磁盘、网络、文件系统、进程、负载……几十个 Panel,每个都有图。

“这……”小林看呆了,“这也太全了吧。”

“这就是社区的力量。”老周说,“你不用从零画,直接导入。你以后工作中,80% 的 Dashboard 都是导入的,不是手画的。”

“那 1860 这个 ID 怎么记?”

“不用记。”老周说,“你搜 Node Exporter Full,第一个就是。Grafana 官网有 Dashboard 市场,你以后需要什么,先去搜,搜不到再自己画。”

小林记下:

  • Node Exporter Full Dashboard ID:1860
  • 导入路径:Dashboards → New → Import → 输入 ID
  • 数据源选 Prometheus
  • 社区模板覆盖 80% 常见场景

七、Dashboard 的变量:$instance

“你看这个 Dashboard,左上角有个下拉框。”老周说。

小林看了一眼,有个 instance 下拉框,里面是 localhost:9100。

“这是变量。”老周说,“你以后监控多台机器,这个下拉框能切换。每个 Panel 的查询里,用 $instance 引用这个变量。”

“那怎么配?”

“Dashboard 设置 → Variables → New variable。”

小林点进去,看到配置项:

Name: instance
Type: Query
Data source: Prometheus
Query: label_values(up, instance)

“label_values(up, instance) 表示从 up 指标里提取 instance 标签的所有值。”老周说,“你保存,刷新 Dashboard,下拉框就有了。”

“那 Panel 里怎么用?”

“在 PromQL 里写 $instance。比如:”

1 - node_memory_MemAvailable_bytes{instance="$instance"} / node_memory_MemTotal_bytes{instance="$instance"}

“这样你切换下拉框,Panel 就跟着变。”老周说,“变量是 Grafana 做多机监控的核心。 你以后监控十台机器,不用建十个 Dashboard,一个 Dashboard 加变量就够了。”

小林记下:

  • 变量类型:Query
  • 查询:label_values(up, instance)
  • Panel 里用 $instance 引用
  • 多机监控用变量,不用建多个 Dashboard

八、排错挑战:Grafana 连不上 Prometheus

下午,小林想试试 Grafana 的告警功能。他打开数据源配置,点了一下 Save & test。

提示:

Error reading Prometheus: Post "http://localhost:9090/api/v1/query": dial tcp [::1]:9090: connect: connection refused

“啥?”小林懵了,“刚才还能用啊。”

他检查 Prometheus:

systemctl status prometheus

输出:

Active: active (running)

“在跑啊。”

他检查端口:

sudo ss -lntp | grep 9090

输出:

LISTEN 0 4096 *:9090 *:* users:(("prometheus",pid=5678,fd=7))

“9090 也在监听啊。”

他喊来老周。

老周看了一眼报错:“dial tcp [::1]:9090: connect: connection refused。注意 [::1],这是 IPv6 的 localhost。”

“那咋了?”

“Prometheus 可能只监听了 IPv4 的 0.0.0.0:9090,没监听 IPv6 的 [::]:9090。”老周说,“Grafana 用 localhost 时,优先解析成 [::1],连不上,就报错。”

“那怎么改?”

“两个办法。”老周说,“第一,把数据源 URL 改成 http://127.0.0.1:9090,强制用 IPv4。第二,让 Prometheus 监听 IPv6。”

小林选了第一个:

http://127.0.0.1:9090

再次 Save & test:

Data source is working

“成了。”小林说。

排错思路总结:

  1. Grafana 报 connection refused,先检查 Prometheus 是否在运行。
  2. systemctl status prometheus 确认服务状态。
  3. ss -lntp | grep 9090 确认端口监听。
  4. 如果都在,检查报错里的 IP 版本:[::1] 是 IPv6,127.0.0.1 是 IPv4。
  5. 把数据源 URL 从 localhost 改成 127.0.0.1,强制 IPv4。
  6. 或者修改 Prometheus 启动参数,监听 [::]:9090。

“记住,”老周说,“localhost 在 IPv4 和 IPv6 环境下,解析结果可能不同。 你以后遇到 connection refused,先看报错里的 IP,是 127.0.0.1 还是 [::1]。用 127.0.0.1 最稳。”

九、练习

  1. 练习一:用官方源安装 Grafana,启动并验证 3000 端口监听。在浏览器访问 http://localhost:3000,用 admin/admin 登录,修改密码。
  2. 练习二:添加 Prometheus 数据源,URL 填 http://127.0.0.1:9090,点 Save & test 确认成功。创建一个 Dashboard,添加 CPU 使用率、内存使用率、根分区使用率三个 Panel。
  3. 练习三:导入 Node Exporter Full Dashboard(ID: 1860),数据源选 Prometheus。刷新页面,确认所有 Panel 都有数据。在 Dashboard 设置里添加 instance 变量,查询 label_values(up, instance),保存后确认下拉框可用。

参考答案

练习一:

sudo apt update
sudo apt install -y apt-transport-https software-properties-common wget
sudo mkdir -p /etc/apt/keyrings/
wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/grafana.gpg > /dev/null
echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" | sudo tee /etc/apt/sources.list.d/grafana.list
sudo apt update
sudo apt install -y grafana
sudo systemctl start grafana-server
sudo systemctl enable grafana-server
sudo systemctl status grafana-server
sudo ss -lntp | grep 3000

浏览器访问 http://localhost:3000,登录 admin/admin,改密码。

练习二:

数据源 URL:http://127.0.0.1:9090。

CPU Panel:

1 - avg(rate(node_cpu_seconds_total{mode="idle"}[1m]))

内存 Panel:

1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes

根分区 Panel:

1 - node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"}

练习三:

Dashboards → New → Import → 输入 1860 → Load → 数据源选 Prometheus → Import。

Dashboard 设置 → Variables → New variable:

Name: instance
Type: Query
Data source: Prometheus
Query: label_values(up, instance)

保存,刷新 Dashboard。

十、下集预告

Grafana 装好了,Dashboard 也导入了,CPU、内存、磁盘、网络都能看了。老周说:“现在你能看到数据了,但你不能 24 小时盯着屏幕。你得让 Prometheus 自己判断,什么时候该告警。”小林打开 Prometheus 的告警规则配置,准备写第一条规则。但他不知道,expr、for、labels、annotations,每一个都有讲究。

下一集:《告警规则:让 Prometheus 学会“喊人”》