# 监控 Alluxio

指标为了解 Alluxio 集群的健康状况和性能提供了宝贵的见解。Alluxio 以 [Prometheus 导出格式](https://prometheus.io/docs/instrumenting/exposition_formats/)公开指标，从而可以轻松地与现代监控堆栈集成。

本指南介绍了如何监控您的 Alluxio 集群，从使用 Alluxio Operator 提供的预配置仪表板到手动设置您自己的监控。

## 使用 Alluxio Operator 进行默认监控

在 Kubernetes 上监控 Alluxio 的最简单方法是使用 Alluxio Operator。默认情况下，operator 会在您的 Alluxio 集群旁边部署一个完整的监控堆栈，包括用于指标收集的 [Prometheus](https://prometheus.io/) 和用于可视化的 [Grafana](https://grafana.com/)。

### 访问 Grafana 仪表板

Grafana 仪表板是可视化集群指标的主要工具。您可以通过两种方式访问它：

#### 1. 通过端口转发访问（推荐）

使用 `kubectl port-forward` 从您的本地计算机安全地访问 Grafana UI。

```console
# 找到 Grafana pod 并转发端口 3000
kubectl -n alx-ns port-forward $(kubectl -n alx-ns get pod -l app.kubernetes.io/component=grafana -o jsonpath="{.items[0].metadata.name}") 3000:3000
```

然后，您可以打开浏览器并导航到 `http://localhost:3000`。

#### 2. 通过节点主机名访问

如果您的 Kubernetes 节点在您的网络上可以直接访问，您可以通过其 NodePort 访问 Grafana。

```shell
# 获取 Grafana 正在运行的节点的主机名
kubectl -n alx-ns get pod $(kubectl -n alx-ns get pod -l app.kubernetes.io/component=grafana --no-headers -o custom-columns=:metadata.name) -o jsonpath='{.spec.nodeName}'
```

假设主机名是 `foo.kubernetes.org`，您可以在 `http://foo.kubernetes.org:8080/` 访问 Grafana 服务。

### 了解仪表板

默认仪表板提供了集群状态的全面概览。

<figure><img src="/files/B6KfoJYn7cALfZfWRXeM" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/KD7ArA8zcNyEoeELM1UH" alt=""><figcaption></figcaption></figure>

* **集群**部分提供了集群状态的高级摘要。
* **进程**部分详细说明了每个 Alluxio 组件的资源消耗（CPU、内存）和 JVM 指标。
* 其他部分提供了特定组件（如Coordinator和 worker）的详细指标。

### 禁用默认 Grafana

如果您希望使用自己的 Grafana 实例，可以通过在 `AlluxioCluster` 定义中将 `spec.grafana.enabled` 设置为 `false` 来禁用默认实例。Prometheus 是一个核心组件，无法禁用。

```yaml
apiVersion: k8s-operator.alluxio.com/v1
kind: AlluxioCluster
spec:
  grafana:
    enabled: false
```

### 高级：直接查询指标

对于高级分析或调试，您可以直接查询 Prometheus 和组件端点。

#### 使用 Promtool 查询

您可以直接对集群中运行的 Prometheus 服务器执行查询。

```shell
# 打开一个到 Prometheus pod 的 shell
kubectl -n alx-ns exec -it $(kubectl -n alx-ns get pod -l app.kubernetes.io/component=prometheus --no-headers -o custom-columns=:metadata.name) -- /bin/sh

# 示例：列出所有可用的 Alluxio 指标
promtool query instant http://localhost:9090 'count({__name__=~".+"}) by (__name__)' | grep alluxio_

# 示例：获取总缓存容量
promtool query instant http://localhost:9090 'alluxio_cached_capacity_bytes'
# 示例输出：
# alluxio_cached_capacity_bytes{instance="worker:30000", job="worker"} => 10737418240 @[1753677978.351]
```

#### 查询组件端点

Alluxio 组件（Coordinator、worker、FUSE）公开一个 `/metrics/` 端点用于抓取。

```shell
# 直接从组件获取指标（例如，本地Coordinator）
$ curl 127.0.0.1:19999/metrics/
```

有关可用指标的完整列表，请参阅指标参考。

## 与现有监控系统集成

如果您不使用 Alluxio Operator 或拥有现有的监控基础架构，您可以手动将其与 Alluxio 集成。

### 与 Prometheus 集成

将以下抓取作业添加到您的 `prometheus.yml` 以从 Alluxio 收集指标。

#### 独立 Prometheus

对于独立的 Prometheus 实例，请使用 `static_configs`：

```yaml
global:
  scrape_interval: 60s

scrape_configs:
  - job_name: "coordinator"
    static_configs:
      - targets: [ '<COORDINATOR_HOSTNAME>:<COORDINATOR_WEB_PORT>' ]
  - job_name: "worker"
    static_configs:
      - targets: [ '<WORKER_HOSTNAME>:<WORKER_WEB_PORT>' ]
  - job_name: "fuse"
    static_configs:
      - targets: [ '<FUSE_HOSTNAME>:<FUSE_WEB_PORT>' ]
```

#### Kubernetes 中的 Prometheus

对于在 Kubernetes 中运行的 Prometheus，请使用 `kubernetes_sd_configs` 自动发现 Alluxio pod。确保您的 Alluxio pod 具有所需的标签和注释。

```yaml
# 用于 Kubernetes 服务发现的 prometheus.yml 片段
scrape_configs:
  - job_name: 'alluxio-components'
    kubernetes_sd_configs:
      - role: pod
    relabel_configs:
      # 仅保留带有 prometheus.io/scrape=true 注释的 pod
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
        action: keep
        regex: true
      # 仅抓取 Alluxio 组件
      - source_labels: [__meta_kubernetes_pod_label_app_kubernetes_io_name]
        action: keep
        regex: alluxio
      # 使用带注释的路径，默认为 /metrics
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
        action: replace
        target_label: __metrics_path__
        regex: (.+)
      # 使用带注释的端口
      - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
        action: replace
        regex: ([^:]+)(?::\d+)?;(\d+)
        replacement: $1:$2
        target_label: __address__
      # 从组件名称创建“job”标签
      - source_labels: [__meta_kubernetes_pod_label_app_kubernetes_io_component]
        action: replace
        target_label: job
```

您的 Alluxio pod 必须具有以下元数据：

```yaml
# Alluxio worker pod 的示例元数据
metadata:
  labels:
    app.kubernetes.io/name: alluxio
    app.kubernetes.io/component: worker # (或 coordinator, fuse)
  annotations:
    prometheus.io/scrape: "true"
    prometheus.io/port: "30000" # (Coordinator为 19999，fuse 为 49999)
    prometheus.io/path: "/metrics/"
```

### 与 Grafana 集成

1. **将 Prometheus 添加为数据源**：在 Grafana 中，将您的 Prometheus 服务器添加为新的数据源。
2. **导入 Alluxio 仪表板**：下载官方 Alluxio 仪表板模板并将其导入 Grafana。
   * **模板 URL**：[alluxio-ai-dashboard-template.json](https://alluxio-binaries.s3.amazonaws.com/artifactsBundle/ee/AI-3.6-12.0.0/alluxio-ai-dashboard-template.json)
   * 遵循 [Grafana 导入指南](https://grafana.com/docs/grafana/latest/dashboards/export-import/#importing-a-dashboard)。

### 与 Datadog 集成

Datadog 可以直接从 Alluxio 的 Prometheus 端点提取指标。

1. 确保您的 Datadog 代理可以访问 Alluxio 组件的指标端口（Coordinator为 `19999`，worker 为 `30000`）。
2. 在您的 Datadog 配置中，将 Alluxio 端点添加到您的 `prometheus.yml` 检查配置中。

`conf.d/prometheus.d/conf.yaml` 片段示例：

```yaml
instances:
  - prometheus_url: http://<alluxio-coordinator-hostname>:19999/metrics
    namespace: alluxio
    metrics:
      - "*"
  - prometheus_url: http://<alluxio-worker-1-hostname>:30000/metrics
    namespace: alluxio
    metrics:
      - "*"
  # 为每个 worker 添加一个条目
```

此配置允许 Datadog 收集、监控和对您的 Alluxio 集群的指标进行告警。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://documentation.alluxio.io/ee-ai-cn/ai-3.7/administration/monitoring-alluxio.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
