# 采集集群信息

首先确认 `operator` 启动成功，并且有 `clloectinfo controller` 在运行。 下面是 `operator` 的信息，可以看到 `collectinfo controller` 在运行。如果 `collectinfo controller` 不存在， 表明当前使用的 `operator` 版本不支持 `collectinfo` 功能，请升级 `operator` 版本

```console
kubectl get pod -n alluxio-operator
NAME                                             READY   STATUS    RESTARTS   AGE
alluxio-cluster-controller-8656d54bc-x6ms6       1/1     Running   0          19s
alluxio-collectinfo-controller-cc49c56b6-wlw8k   1/1     Running   0          19s
alluxio-csi-controller-84df9646fd-4d5b8          2/2     Running   0          19s
alluxio-csi-nodeplugin-fcp7b                     2/2     Running   0          19s
alluxio-csi-nodeplugin-t59ch                     2/2     Running   0          19s
alluxio-csi-nodeplugin-vbq2q                     2/2     Running   0          19s
alluxio-ufs-controller-57fbdf8d5c-2f79l          1/1     Running   0          19s
```

确认 Alluxio 集群是否启动成功, 假设 Alluxio 集群在 `default` 命名空间下 下面是 Alluxio 集群的信息，可以看到 Alluxio 集群的各个组件都在运行

```console
kubectl get pod 
NAME                                          READY   STATUS    RESTARTS   AGE
alluxio-coordinator-0                         1/1     Running   0          2m17s
alluxio-etcd-0                                1/1     Running   0          2m17s
alluxio-etcd-1                                1/1     Running   0          2m17s
alluxio-etcd-2                                1/1     Running   0          2m17s
alluxio-monitor-grafana-9fd587b4f-mnczs       1/1     Running   0          2m17s
alluxio-monitor-prometheus-6b55c568b8-sfp96   1/1     Running   0          2m17s
alluxio-worker-779d87567f-95wls               1/1     Running   0          2m17s
alluxio-worker-779d87567f-sgh4b               1/1     Running   0          2m17s
```

## 采集信息

创建最简单的 yaml，使用默认值采集信息。（完整的配置可以参考[详细配置](#详细配置)）

假设 Alluxio 集群位于 `default` 命名空间，创建 `collectinfo.yaml` 文件，内容如下：

```yaml
apiVersion: k8s-operator.alluxio.com/v1
kind: CollectInfo
metadata:
  name: example-collectinfo
spec:
  alluxio:
    namespace: "default"
```

创建 `collectinfo` ，开始采集信息。

```console
kubectl apply -f collectinfo.yaml
```

可以通过 `collectinfo` 的状态来查看采集信息的进度， 下面表示采集 5 种信息，全部采集完成。

```console
kubectl get collectinfo
NAME                  COMPLETED   FAILED   STATE       AGE
example-collectinfo   5/5         0/5      Completed   6m16s
```

`collectinfo` 会在 `alluxio-operator` 命名空间下创建多个 `job` ，用于采集 Alluxio 集群的信息。 默认采集全部信息，可以看到有 5 个 `job` 在运行，分别是 `config`, `hardware`, `license`, `logs`, `metrics`。

```console
kubectl get job -n alluxio-operator
NAME                               COMPLETIONS   DURATION   AGE
example-collectinfo-config-job     1/1           4s         4m10s
example-collectinfo-hardware-job   1/1           5s         4m10s
example-collectinfo-license-job    1/1           10s        4m10s
example-collectinfo-logs-job       1/1           5s         4m10s
example-collectinfo-metrics-job    1/1           4s         4m10s
```

### 采集信息失败

下面表示采集信息失败，有 4 种信息采集失败。

```console
kubectl get collectinfo
NAME                  COMPLETED   FAILED   STATE    AGE
example-collectinfo   1/5         4/5      Failed   52s
```

查看 `collectinfo` 的 `job` 信息， 可以看到 `collectinfo` 的 `job` 只有 `hardware job` 成功，其他的 `job` 都失败了。

```console
kubectl get job -n alluxio-operator
NAME                               COMPLETIONS   DURATION   AGE
example-collectinfo-config-job     0/1           4m18s      4m18s
example-collectinfo-hardware-job   1/1           5s         4m18s
example-collectinfo-license-job    0/1           4m18s      4m18s
example-collectinfo-logs-job       0/1           4m18s      4m18s
example-collectinfo-metrics-job    0/1           4m18s      4m18s
```

无论 `collectinfo` 操作是成功还是失败，您都可以下载收集结果。

如果存在任何失败，结果中将包含 `error.log` 以供调试。

## 下载结果

有两种方法可以下载信息的结果：`kubectl cp` 和 `kubectl port-forward`。

结果包含以下类型的信息：

* config: Alluxio conf/ 目录中的配置文件，例如 `alluxio-site.properties` 和 `alluxio-env.sh`。
* hardware: 每个 Kubernetes 节点的 CPU 和内存详细信息。coordinator、worker、fuse 和 operator 组件的硬件规格。
* license: Alluxio 集群的许可证信息，包括类型、productionId 和 licenseVersion。以及正在使用的 vCPU、内存和存储。
* logs: coordinator、worker、fuse、etcd 和 operator 组件的日志。支持 tail 日志以显示末尾指定行数的日志。
* metrics: 允许设置持续时间(duration)和步长(step)，以定义指标的时间范围和采样间隔（收集所有指标）。

### kubectl cp

通过 `kubectl cp` 的方式，将采集的信息拷贝到本地。

```shell
# 设置环境变量保存collectinfo controller的名字 
COLLECTINFO_CONTROLLER_NAME=$(kubectl get pod -n alluxio-operator -l app.kubernetes.io/component=collectinfo-controller -o jsonpath="{.items[0].metadata.name}")
# 需要填入collectinfo的名字
kubectl cp alluxio-operator/${COLLECTINFO_CONTROLLER_NAME}:/tmp/output/<COLLECTINFO_NAME> output -n alluxio-operator
```

### kubectl port-forward

通过 `port-forward` 的方式，将 `collectinfo controller` 的端口映射到本地，将远程 `collectinfo controller` 的 `80` 端口映射到本地的 `28080` 端口

```shell
# 设置环境变量保存collectinfo controller的名字 
CLOLLECTINFO_CONTROLLER_NAME=$(kubectl get pod -n alluxio-operator -l app.kubernetes.io/component=collectinfo-controller -o jsonpath="{.items[0].metadata.name}")
kubectl port-forward -n alluxio-operator ${COLLECTINFO_CONTROLLER_NAME} 28080:80
```

通过 `curl` 的方式，下载采集的信息

```shell
curl -H "Collectinfo-Name: <COLLECTINFO_NAME>" http://127.0.0.1:28080/download -o output.tar
```

解压下载的文件

```shell
tar -xvf output.tar
```

## 详细配置

```yaml
apiVersion: k8s-operator.alluxio.com/v1
kind: CollectInfo
metadata:
  name: example-collectinfo
spec:
  alluxio:
    # alluxio cluster所在的namespace
    namespace: "default"
  # 采集信息类型，包括config, hardware, license, logs, metrics
  # 如果不指定或者指定all，表示采集全部信息
  # 允许指定多个类型:
  # type:
  #   - config
  #   - hardware
  type:
    - all
  # 重试次数，如果采集job失败，会重试指定次数
  backoffLimit: 2
  logs:
    # 采集日志的数量，比如100表示采集最新的100条日志
    tail: 100
  # 指标信息，duration表示采集的时长，step表示采集的间隔
  # 下面的参数示例表示采集从现在到过去2小时内的全部指标(metrics)，间隔每分钟采集一次指标(metrics)
  metrics:
    # 采集指标(metrics)的时长，比如2h表示采集从现在到过去2小时内的指标(metrics)
    duration: 2h
    # 采集指标(metrics)的间隔，比如1m表示每分钟采集一次指标(metrics)
    step: 1m
  # 执行采集任务的镜像，默认使用alluxio operator的镜像
  # 可以不指定，使用默认值
  image: "<ALLUXIO_OPERATOR_IMAGE>"
  imagePullPolicy: "Always"
  # 采集信息的资源限制
  resources:
    requests:
      memory: "512Mi"
      cpu: "250m"
    limits:
      memory: "1Gi"
      cpu: "500m"
```
