> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 监控 Kubernetes

> 使用 ClickHouse Observability 采集 Kubernetes 日志和指标

从 Kubernetes 集群中收集日志和指标，并将其发送到 **ClickHouse Observability**。您可以使用现有的工作负载，也可以部署下方可选的演示应用。无论采用全托管部署还是自管理部署，演示架构和应用的部署方式均相同。

## 前置条件

* 一个正在运行的 **Kubernetes 集群**，且有足够容量运行采集器以及演示应用 (如使用) 。
* 一个 ClickHouse Observability 服务。请参阅[全托管入门指南](https://clickhouse.com/docs/zh/clickstack/observability/getting-started.md)或[自管理入门指南](https://clickhouse.com/docs/zh/clickstack/self-configured/getting-started/managed.md)。
* **[Helm](https://helm.sh/)** v3+。
* **`kubectl`**，已配置为可与您的集群交互，并具有创建命名空间、Secrets、ConfigMaps 和集群角色的权限。
* 采集器能够通过网络访问所选部署方式对应的端点。

## 演示应用

如果您已有需要监控的工作负载，可以跳过演示部署，直接前往[配置采集](#configure-collection)。

[OpenTelemetry Demo](https://github.com/ClickHouse/opentelemetry-demo) 的 ClickStack 分叉版本提供了一个电商应用，包含已插桩的微服务以及自动生成的流量。

### 演示架构

该演示由使用不同编程语言编写的微服务组成，这些微服务通过 gRPC 和 HTTP 相互通信，此外还有一个使用 Locust 模拟用户流量的负载生成器。此演示的原始源代码已修改为使用 [ClickStack 插桩](https://clickhouse.com/docs/zh/clickstack/ingesting-data/sdks/index.md)。

![架构](https://mintcdn.com/private-7c7dfe99/Y7YJeVzyAMn9ugXv/images/use-cases/observability/hyperdx-demo/architecture.webp?fit=max\&auto=format\&n=Y7YJeVzyAMn9ugXv\&q=85\&s=76b5086a03fd753513254433b4911fe0)

*鸣谢：[https://opentelemetry.io/docs/demo/architecture/](https://opentelemetry.io/docs/demo/architecture/)*

有关该演示的更多详细信息，请参见：

* [OpenTelemetry 文档](https://opentelemetry.io/docs/demo/)
* [由 ClickStack 维护的分支](https://github.com/ClickHouse/opentelemetry-demo)

### 部署演示应用

下载并部署演示应用清单：

```shell theme={null}
curl -fSL -o opentelemetry-demo.yaml \
  https://raw.githubusercontent.com/ClickHouse/opentelemetry-demo/d658416d6b36381126eba4416877b6518c320921/kubernetes/opentelemetry-demo.yaml

kubectl apply --namespace otel-demo -f opentelemetry-demo.yaml
kubectl get pods --namespace otel-demo
```

等待应用程序的 pod (容器组)  进入就绪状态。该清单会创建 `otel-demo` 命名空间，并包含所需的辅助服务。

这些命令使用的是固定的演示修订版本。部署应用程序后，请根据下方与您的部署方式对应的选项卡安装采集器，并接入演示遥测数据。采集器负责收集 Kubernetes 日志和基础设施指标，已插桩的演示服务则通过 OTLP 发送应用程序遥测数据。

## 配置数据采集

> [tab] 全托管
>
> [Beta 版产品](https://clickhouse.com/docs/reference/settings/beta-and-experimental-features.md#beta-features)
>
> 在 Kubernetes 集群中部署 OpenTelemetry 收集器，采集日志和指标，并将其转发到您的托管遥测数据端点。ClickHouse Observability 会负责数据摄取、存储和 UI，因此您无需在集群中部署 ClickHouse、ClickStack 用户界面或 MongoDB。
>
> **1. 复制连接详情**
>
> 打开您的 ClickHouse Observability 服务，进入 **团队设置 → 数据 → Managed Telemetry**。在 **OpenTelemetry 收集器** 下，选择 **URL** 选项卡，然后复制 OTLP HTTP 端点和 API key。点击 **Reveal key** 即可查看完整的配置值。
>
> 在后续步骤中，您将使用这些连接详情来配置 Kubernetes 采集器。
>
> ![Managed Telemetry 页面中显示的 OTLP 端点和 API key 连接详情](https://mintcdn.com/private-7c7dfe99/Zk1feNn6fCQBpold/images/clickstack/getting-started/observability-managed-telemetry.png?fit=max\&auto=format\&n=Zk1feNn6fCQBpold\&q=85\&s=e63a2ac2e6ab996d3fc360ebe0f487a5)
>
> **2. 保存连接详情**
>
> 将托管遥测数据的 API key 存储在 Kubernetes Secret 中，并将 OTLP HTTP 端点存储在 ConfigMap 中。这两个资源都需要创建在你将要安装采集器的命名空间中。以下示例使用专用的 `otel-collectors` 命名空间。
>
> ```shell theme={null}
> kubectl create namespace otel-collectors
>
> kubectl create secret generic hyperdx-secret \
>   --namespace otel-collectors \
>   --from-literal=HYPERDX_API_KEY='<api-key>'
>
> kubectl create configmap otel-config-vars \
>   --namespace otel-collectors \
>   --from-literal=YOUR_OTEL_COLLECTOR_ENDPOINT='<managed-otlp-http-endpoint>'
> ```
>
> 如果该命名空间已存在，请跳过第一条命令。将占位符替换为 **Managed Telemetry** 中的值，并保留端点的 HTTPS 协议和端口。
>
> **3. 添加 OpenTelemetry Helm 仓库**
>
> 使用官方 OpenTelemetry Helm 图表部署采集器，将日志和指标发送到您的托管遥测数据端点。
>
> 添加图表仓库并刷新其索引：
>
> ```shell theme={null}
> helm repo add open-telemetry https://open-telemetry.github.io/opentelemetry-helm-charts
> helm repo update open-telemetry
> ```
>
> **4. 部署 Kubernetes 采集器组件**
>
> 要同时采集集群本身和各个节点的日志与指标，我们需要部署两个独立的 OpenTelemetry 收集器，每个收集器各使用一个 Helm values 文件。提供的两个文件 `k8s_deployment.yaml` 和 `k8s_daemonset.yaml` 相互配合，可从您的 Kubernetes 集群中全面采集遥测数据。
>
> * `k8s_deployment.yaml` 会部署一个**单实例 OpenTelemetry 收集器**，负责采集**集群级事件和元数据**。它会收集 Kubernetes 事件和集群指标，并利用 pod (容器组) 的标签和注解为遥测数据补充信息。该采集器以单副本的独立部署方式运行，以避免产生重复数据。
>
> * `k8s_daemonset.yaml` 会部署一个**基于 DaemonSet 守护进程集的采集器**，运行在集群的每个节点上。它借助 `kubeletstats`、`hostmetrics` 以及 Kubernetes 属性处理器等组件，采集**节点级和 pod (容器组) 级指标**以及容器日志。这些采集器会为日志补充元数据，并通过 OTLP HTTP 导出器将其发送到托管的遥测数据端点。
>
> 两个采集器均直接将数据导出到 ClickHouse Observability。在单节点集群中，这会创建两个采集器 Pod (容器组) 。此外，该部署还会接收演示应用发送的 OTLP 应用日志、指标和链路追踪数据。
>
> 以下命令会先下载各个 Helm values 文件，再安装对应的采集器。展开每条命令下方的配置即可查看其内容。这些示例使用 `0.175.0` 版本的 Helm 图表和 `0.161.0` 版本的采集器。
>
> 首先，以部署 (Deployment) 形式安装采集器：
>
> ```shell theme={null}
> curl -fSL -o k8s_deployment.yaml \
>   https://raw.githubusercontent.com/ClickHouse/ClickHouse/master/docs/clickstack/observability/example-datasets/_snippets/k8s_deployment.yaml
>
> helm install --namespace otel-collectors k8s-otel-deployment open-telemetry/opentelemetry-collector \
>   --version 0.175.0 -f k8s_deployment.yaml
> ```
>
> > [accordion] k8s\_deployment.yaml
> >
> > ```yaml theme={null}
> > # k8s_deployment.yaml
> > mode: deployment
> >
> > image:
> >   repository: otel/opentelemetry-collector-contrib
> >   tag: 0.161.0
> >
> > # We only want one of these collectors - any more and we'd produce duplicate data
> > replicaCount: 1
> >
> > presets:
> >   kubernetesAttributes:
> >     enabled: true
> >     # When enabled, the processor will extract all labels for an associated pod and add them as resource attributes.
> >     # The label's exact name will be the key.
> >     extractAllPodLabels: true
> >     # When enabled, the processor will extract all annotations for an associated pod and add them as resource attributes.
> >     # The annotation's exact name will be the key.
> >     extractAllPodAnnotations: true
> >   # Configures the collector to collect Kubernetes events.
> >   # Adds the k8sobject receiver to the logs pipeline and collects Kubernetes events by default.
> >   # More Info: https://opentelemetry.io/docs/kubernetes/collector/components/#kubernetes-objects-receiver
> >   kubernetesEvents:
> >     enabled: true
> >   # Configures the Kubernetes Cluster Receiver to collect cluster-level metrics.
> >   # Adds the k8s_cluster receiver to the metrics pipeline and adds the necessary rules to ClusterRole.
> >   # More Info: https://opentelemetry.io/docs/kubernetes/collector/components/#kubernetes-cluster-receiver
> >   clusterMetrics:
> >     enabled: true
> >
> > extraEnvs:
> >   - name: HYPERDX_API_KEY
> >     valueFrom:
> >       secretKeyRef:
> >         name: hyperdx-secret
> >         key: HYPERDX_API_KEY
> >   - name: YOUR_OTEL_COLLECTOR_ENDPOINT
> >     valueFrom:
> >       configMapKeyRef:
> >         name: otel-config-vars
> >         key: YOUR_OTEL_COLLECTOR_ENDPOINT
> >
> > config:
> >   exporters:
> >     otlp_http:
> >       endpoint: "${env:YOUR_OTEL_COLLECTOR_ENDPOINT}"
> >       compression: gzip
> >       headers:
> >         authorization: "Bearer ${env:HYPERDX_API_KEY}"
> >   service:
> >     pipelines:
> >       logs:
> >         exporters:
> >           - otlp_http
> >       metrics:
> >         exporters:
> >           - otlp_http
> >       traces:
> >         exporters:
> >           - otlp_http
> >
> > resources:
> >   requests:
> >     cpu: 100m
> >     memory: 128Mi
> >   limits:
> >     memory: 512Mi
> > ```
>
> 接下来，将采集器部署为 DaemonSet 守护进程集，以采集节点和 pod (容器组) 级别的指标和日志：
>
> ```shell theme={null}
> curl -fSL -o k8s_daemonset.yaml \
>   https://raw.githubusercontent.com/ClickHouse/ClickHouse/master/docs/clickstack/observability/example-datasets/_snippets/k8s_daemonset.yaml
>
> helm install --namespace otel-collectors k8s-otel-daemonset open-telemetry/opentelemetry-collector \
>   --version 0.175.0 -f k8s_daemonset.yaml
> ```
>
> > [accordion] k8s\_daemonset.yaml
> >
> > ```yaml theme={null}
> > # k8s_daemonset.yaml
> > mode: daemonset
> >
> > image:
> >   repository: otel/opentelemetry-collector-contrib
> >   tag: 0.161.0
> >
> > # Required to use the kubeletstats cpu/memory utilization metrics
> > clusterRole:
> >   create: true
> >   rules:
> >     - apiGroups:
> >         - ''
> >       resources:
> >         - nodes/proxy
> >       verbs:
> >         - get
> >
> > presets:
> >   logsCollection:
> >     enabled: true
> >   hostMetrics:
> >     enabled: true
> >   # Configures the Kubernetes Processor to add Kubernetes metadata.
> >   # Adds the k8sattributes processor to all the pipelines and adds the necessary rules to ClusterRole.
> >   # More Info: https://opentelemetry.io/docs/kubernetes/collector/components/#kubernetes-attributes-processor
> >   kubernetesAttributes:
> >     enabled: true
> >     # When enabled, the processor will extract all labels for an associated pod and add them as resource attributes.
> >     # The label's exact name will be the key.
> >     extractAllPodLabels: true
> >     # When enabled, the processor will extract all annotations for an associated pod and add them as resource attributes.
> >     # The annotation's exact name will be the key.
> >     extractAllPodAnnotations: true
> >   # Configures the collector to collect node, pod, and container metrics from the kubelet API.
> >   # Adds the kubeletstats receiver to the metrics pipeline and adds the necessary rules to ClusterRole.
> >   # More Info: https://opentelemetry.io/docs/kubernetes/collector/components/#kubeletstats-receiver
> >   kubeletMetrics:
> >     enabled: true
> >
> > extraEnvs:
> >   - name: HYPERDX_API_KEY
> >     valueFrom:
> >       secretKeyRef:
> >         name: hyperdx-secret
> >         key: HYPERDX_API_KEY
> >   - name: YOUR_OTEL_COLLECTOR_ENDPOINT
> >     valueFrom:
> >       configMapKeyRef:
> >         name: otel-config-vars
> >         key: YOUR_OTEL_COLLECTOR_ENDPOINT
> >
> > config:
> >   receivers:
> >     kubeletstats:
> >       insecure_skip_verify: true
> >   exporters:
> >     otlp_http:
> >       endpoint: "${env:YOUR_OTEL_COLLECTOR_ENDPOINT}"
> >       compression: gzip
> >       headers:
> >         authorization: "Bearer ${env:HYPERDX_API_KEY}"
> >
> >   service:
> >     pipelines:
> >       logs:
> >         exporters:
> >           - otlp_http
> >       metrics:
> >         exporters:
> >           - otlp_http
> >
> > resources:
> >   requests:
> >     cpu: 100m
> >     memory: 128Mi
> >   limits:
> >     memory: 512Mi
> > ```
>
> DaemonSet 守护进程集示例为本地 kubelet 节点代理连接设置了 `insecure_skip_verify: true`，因为本地集群使用的证书可能未涵盖节点地址。在生产环境中，请配置受信任的 kubelet 节点代理证书，并启用证书验证。此设置不影响托管遥测数据端点的 TLS 验证。
>
> 检查采集器是否已就绪：
>
> ```shell theme={null}
> kubectl get pods --namespace otel-collectors -l app.kubernetes.io/name=opentelemetry-collector
> kubectl logs --namespace otel-collectors -l app.kubernetes.io/name=opentelemetry-collector --prefix --tail=50
> ```
>
> 检查日志中是否存在身份验证、导出或抓取错误。pod (容器组) 处于运行状态，并不代表遥测数据已成功送达。
>
> **5. 连接演示应用**
>
> 如果您部署了演示应用，请将其导出器指向 in-cluster 采集器。采集器在转发遥测数据时，会附加托管端点所需的身份验证请求头。
>
> ```shell theme={null}
> kubectl set env deployment --namespace otel-demo \
>   -l app.kubernetes.io/part-of=opentelemetry-demo \
>   OTEL_COLLECTOR_NAME=k8s-otel-deployment-opentelemetry-collector.otel-collectors.svc.cluster.local
>
> kubectl rollout status deployment --namespace otel-demo \
>   -l app.kubernetes.io/part-of=opentelemetry-demo --timeout=10m
> ```
>
> 按照上文的配置，托管 API key 应放在 `otel-collectors` 命名空间中。演示应用将数据发送到 in-cluster 接收器，因此无需该 API key。

> [tab] 自管理
>
> ClickHouse 和可观测性 UI 运行在 ClickHouse Cloud 中。此配置会在您的 Kubernetes 集群中部署一个采集器及其配套组件。
>
> 以下命令均使用 `otel-demo` 命名空间。如果您跳过了演示应用的部署步骤，请先创建该命名空间：
>
> ```shell theme={null}
> kubectl create namespace otel-demo
> ```
>
> **1. 添加 ClickStack Helm 图表仓库**
>
> 我们使用[官方 Helm 图表](https://clickhouse.com/docs/zh/clickstack/deployment/helm.md)来部署采集器及其配套组件。
>
> 为此，需要先添加 HyperDX Helm 代码仓库：
>
> ```shell theme={null}
> helm repo add hyperdx https://hyperdxio.github.io/helm-charts
> helm repo update
> ```
>
> **2. 部署采集器及配套组件**
>
> 安装 Helm 图表后，将采集器及其配套组件部署到您的集群中，并使用 ClickHouse Cloud 作为数据库。
>
> 部署该图表，并[禁用其中自带的 ClickHouse](https://clickhouse.com/docs/zh/clickstack/deployment/helm.md#using-clickhouse-cloud)。
>
> > [!NOTE]
> >
> > 该图表目前总会同时部署 ClickStack 用户界面和 MongoDB。虽然这些组件提供了另一种访问途径，但它们并未与 ClickHouse Cloud 身份验证集成。在此部署模式下，这些组件仅供管理员使用，用于[获取安全摄取密钥](#retrieve-ingestion-api-key-self-configured)，以便通过已部署的 OTel collector 摄取数据，但不应向最终用户开放。
>
> ```shell theme={null}
> # specify ClickHouse Cloud credentials
> export CLICKHOUSE_URL=<CLICKHOUSE_CLOUD_URL> # full https url
> export CLICKHOUSE_USER=<CLICKHOUSE_USER>
> export CLICKHOUSE_PASSWORD=<CLICKHOUSE_PASSWORD>
>
> helm install my-hyperdx hyperdx/hdx-oss-v2 --version 0.8.4 --set clickhouse.enabled=false --set clickhouse.persistence.enabled=false --set otel.clickhouseEndpoint=${CLICKHOUSE_URL} --set clickhouse.config.users.otelUserName=${CLICKHOUSE_USER} --set clickhouse.config.users.otelUserPassword=${CLICKHOUSE_PASSWORD} --set global.storageClassName="standard-rwo" -n otel-demo
> ```
>
> 要验证部署状态，请运行以下命令，并确认所有组件均处于 `Running` 状态。ClickHouse 运行在 ClickHouse Cloud 中，因此不会出现在 pod (容器组) 列表中：
>
> ```shell theme={null}
> kubectl get pods -l "app.kubernetes.io/name=hdx-oss-v2" -n otel-demo
>
> NAME                                                    READY   STATUS    RESTARTS   AGE
> my-hyperdx-hdx-oss-v2-app-78876d79bb-565tb              1/1     Running   0          14m
> my-hyperdx-hdx-oss-v2-mongodb-984845f96-czb6m           1/1     Running   0          14m
> my-hyperdx-hdx-oss-v2-otel-collector-64cf698f5c-8s7qj   1/1     Running   0          14m
> ```
>
> **3. 访问 ClickStack 用户界面**
>
> > [!NOTE]
> >
> > 即使使用 ClickHouse Observability — Self-managed，仍需在 Kubernetes 集群中部署本地 ClickStack 用户界面实例。该实例提供由 ClickStack 用户界面内置的 OpAMP 服务器管理的摄取密钥，用于保护经由已部署的 OTel collector 进行的摄取——ClickHouse Observability — Self-managed 目前尚不具备这一能力。
>
> 出于安全考虑，该服务默认使用 `ClusterIP`，不对外暴露。
>
> 如需访问 ClickStack 用户界面，请将端口 3000 转发到本地端口 8080。
>
> ```shell theme={null}
> kubectl port-forward \
>  deployment/my-hyperdx-hdx-oss-v2-app \
>   8080:3000 \
>  -n otel-demo
> ```
>
> 访问 [http://localhost:8080](http://localhost:8080)，打开 ClickStack 用户界面。
>
> 创建用户，并设置符合复杂度要求的用户名和密码。
>
> **4. 获取摄取 API key**
>
> 发送到由 ClickStack 采集器部署的 OTel collector 的数据，通过摄取密钥进行安全保护。
>
> 前往 [`Team Settings`](http://localhost:8080/team)，在 `API Keys` 部分复制 `Ingestion API Key`。该 API key 可确保通过 OpenTelemetry collector 进行的数据摄取安全可靠。
>
> **5. 创建用于存储 API key 的 Kubernetes Secret**
>
> 创建一个包含摄取 API key 的新 Kubernetes Secret，以及一个 ConfigMap，用于记录通过 ClickStack Helm 图表部署的 OTel collector 的地址。后续组件将借助它们，将数据摄取到通过 ClickStack Helm 图表部署的采集器中：
>
> ```shell theme={null}
> # create secret with the ingestion API key
> kubectl create secret generic hyperdx-secret \
> --from-literal=HYPERDX_API_KEY=<ingestion_api_key> \
> -n otel-demo
>
> # create a ConfigMap pointing to the ClickStack OTel collector deployed above
> kubectl create configmap -n=otel-demo otel-config-vars --from-literal=YOUR_OTEL_COLLECTOR_ENDPOINT=http://my-hyperdx-hdx-oss-v2-otel-collector:4318
> ```
>
> 如果您已部署演示应用，请更新其采集器服务名称，使其与此 Helm 部署保持一致。更新环境配置还会重启应用程序 Pod (容器组) ，使其获取摄取密钥：
>
> ```shell theme={null}
> kubectl set env deployment --namespace otel-demo \
>   -l app.kubernetes.io/part-of=opentelemetry-demo \
>   OTEL_COLLECTOR_NAME=my-hyperdx-hdx-oss-v2-otel-collector
>
> kubectl rollout status deployment --namespace otel-demo \
>   -l app.kubernetes.io/part-of=opentelemetry-demo --timeout=10m
> ```
>
> 演示应用的导出器通过此服务名称访问采集器。对于您自己的应用程序，请另行配置其 OTLP 端点和接收器身份验证。
>
> **6. 添加 OpenTelemetry Helm 仓库**
>
> 为了采集 Kubernetes 指标，我们将部署一个标准的 OTel collector，并将其配置为使用上述摄取 API key，将数据安全地发送到 ClickStack 采集器。
>
> 为此，需要先安装 OpenTelemetry Helm 代码仓库：
>
> ```shell theme={null}
> # Add Otel Helm repo
> helm repo add open-telemetry https://open-telemetry.github.io/opentelemetry-helm-charts
> ```
>
> **7. 部署 Kubernetes 采集器组件**
>
> 要同时采集集群本身和各个节点的日志与指标，我们需要部署两个独立的 OpenTelemetry 收集器，每个收集器各有一份清单。所提供的两份清单——`k8s_deployment.yaml` 和 `k8s_daemonset.yaml`——相互配合，从您的 Kubernetes 集群中全面采集遥测数据。
>
> * `k8s_deployment.yaml` 部署**单个 OpenTelemetry 收集器实例**，负责采集**集群级事件和元数据**。它会采集 Kubernetes 事件和集群指标，并利用 pod (容器组) 标签和注解为遥测数据补充信息。该采集器以单副本的独立部署方式运行，以避免产生重复数据。
>
> * `k8s_daemonset.yaml` 部署一个**基于 DaemonSet 守护进程集的采集器**，在集群中的每个节点上运行。它借助 `kubeletstats`、`hostmetrics` 以及 Kubernetes Attribute Processor 等组件，采集**节点级和 pod 级指标**以及容器日志。这些采集器会为日志补充元数据，并通过 OTLP 导出器将其发送到 ClickStack。
>
> 这些资源清单协同工作，为整个集群实现从基础设施到应用级遥测数据的全栈可观测性，并将富化后的数据发送到您的服务进行集中分析。
>
> 首先，将采集器安装为 Deployment (部署) ：
>
> ```shell theme={null}
> # download manifest file
> curl -O https://raw.githubusercontent.com/ClickHouse/clickhouse-docs/refs/heads/main/docs/use-cases/observability/clickstack/example-datasets/_snippets/k8s_deployment.yaml
> # install the helm chart
> helm install --namespace otel-demo k8s-otel-deployment open-telemetry/opentelemetry-collector -f k8s_deployment.yaml
> ```
>
> > [accordion] k8s\_deployment.yaml
> >
> > ```yaml theme={null}
> > # k8s_deployment.yaml
> > mode: deployment
> >
> > image:
> >   repository: otel/opentelemetry-collector-contrib
> >   tag: 0.123.0
> >
> > # We only want one of these collectors - any more and we'd produce duplicate data
> > replicaCount: 1
> >
> > presets:
> >   kubernetesAttributes:
> >     enabled: true
> >     # When enabled, the processor will extract all labels for an associated pod and add them as resource attributes.
> >     # The label's exact name will be the key.
> >     extractAllPodLabels: true
> >     # When enabled, the processor will extract all annotations for an associated pod and add them as resource attributes.
> >     # The annotation's exact name will be the key.
> >     extractAllPodAnnotations: true
> >   # Configures the collector to collect Kubernetes events.
> >   # Adds the k8sobject receiver to the logs pipeline and collects Kubernetes events by default.
> >   # More Info: https://opentelemetry.io/docs/kubernetes/collector/components/#kubernetes-objects-receiver
> >   kubernetesEvents:
> >     enabled: true
> >   # Configures the Kubernetes Cluster Receiver to collect cluster-level metrics.
> >   # Adds the k8s_cluster receiver to the metrics pipeline and adds the necessary rules to ClusteRole.
> >   # More Info: https://opentelemetry.io/docs/kubernetes/collector/components/#kubernetes-cluster-receiver
> >   clusterMetrics:
> >     enabled: true
> >
> > extraEnvs:
> >   - name: HYPERDX_API_KEY
> >     valueFrom:
> >       secretKeyRef:
> >         name: hyperdx-secret
> >         key: HYPERDX_API_KEY
> >         optional: true
> >   - name: YOUR_OTEL_COLLECTOR_ENDPOINT
> >     valueFrom:
> >       configMapKeyRef:
> >         name: otel-config-vars
> >         key: YOUR_OTEL_COLLECTOR_ENDPOINT
> >
> > config:
> >   exporters:
> >     otlphttp:
> >       endpoint: "${env:YOUR_OTEL_COLLECTOR_ENDPOINT}"
> >       compression: gzip
> >       headers:
> >         authorization: "${env:HYPERDX_API_KEY}"
> >   service:
> >     pipelines:
> >       logs:
> >         exporters:
> >           - otlphttp
> >       metrics:
> >         exporters:
> >           - otlphttp
> > ```
>
> 接下来，将采集器部署为 DaemonSet 守护进程集，用于采集节点和 pod (容器组) 级别的指标和日志：
>
> ```shell theme={null}
> # download manifest file
> curl -O https://raw.githubusercontent.com/ClickHouse/clickhouse-docs/refs/heads/main/docs/use-cases/observability/clickstack/example-datasets/_snippets/k8s_daemonset.yaml
> # install the helm chart
> helm install --namespace otel-demo k8s-otel-daemonset open-telemetry/opentelemetry-collector -f k8s_daemonset.yaml
> ```
>
> > [accordion] k8s\_daemonset.yaml
> >
> > ```yaml theme={null}
> > # k8s_daemonset.yaml
> > mode: daemonset
> >
> > image:
> >   repository: otel/opentelemetry-collector-contrib
> >   tag: 0.123.0
> >
> > # Required to use the kubeletstats cpu/memory utilization metrics
> > clusterRole:
> >   create: true
> >   rules:
> >     - apiGroups:
> >         - ''
> >       resources:
> >         - nodes/proxy
> >       verbs:
> >         - get
> >
> > presets:
> >   logsCollection:
> >     enabled: true
> >   hostMetrics:
> >     enabled: true
> >   # Configures the Kubernetes Processor to add Kubernetes metadata.
> >   # Adds the k8sattributes processor to all the pipelines and adds the necessary rules to ClusterRole.
> >   # More Info: https://opentelemetry.io/docs/kubernetes/collector/components/#kubernetes-attributes-processor
> >   kubernetesAttributes:
> >     enabled: true
> >     # When enabled, the processor will extract all labels for an associated pod and add them as resource attributes.
> >     # The label's exact name will be the key.
> >     extractAllPodLabels: true
> >     # When enabled, the processor will extract all annotations for an associated pod and add them as resource attributes.
> >     # The annotation's exact name will be the key.
> >     extractAllPodAnnotations: true
> >   # Configures the collector to collect node, pod, and container metrics from the API server on a kubelet..
> >   # Adds the kubeletstats receiver to the metrics pipeline and adds the necessary rules to ClusterRole.
> >   # More Info: https://opentelemetry.io/docs/kubernetes/collector/components/#kubeletstats-receiver
> >   kubeletMetrics:
> >     enabled: true
> >
> > extraEnvs:
> >   - name: HYPERDX_API_KEY
> >     valueFrom:
> >       secretKeyRef:
> >         name: hyperdx-secret
> >         key: HYPERDX_API_KEY
> >         optional: true
> >   - name: YOUR_OTEL_COLLECTOR_ENDPOINT
> >     valueFrom:
> >       configMapKeyRef:
> >         name: otel-config-vars
> >         key: YOUR_OTEL_COLLECTOR_ENDPOINT
> >
> > config:
> >   receivers:
> >     # Configures additional kubelet metrics
> >     kubeletstats:
> >       collection_interval: 20s
> >       auth_type: 'serviceAccount'
> >       endpoint: '${env:K8S_NODE_NAME}:10250'
> >       insecure_skip_verify: true
> >       metrics:
> >         k8s.pod.cpu_limit_utilization:
> >           enabled: true
> >         k8s.pod.cpu_request_utilization:
> >           enabled: true
> >         k8s.pod.memory_limit_utilization:
> >           enabled: true
> >         k8s.pod.memory_request_utilization:
> >           enabled: true
> >         k8s.pod.uptime:
> >           enabled: true
> >         k8s.node.uptime:
> >           enabled: true
> >         k8s.container.cpu_limit_utilization:
> >           enabled: true
> >         k8s.container.cpu_request_utilization:
> >           enabled: true
> >         k8s.container.memory_limit_utilization:
> >           enabled: true
> >         k8s.container.memory_request_utilization:
> >           enabled: true
> >         container.uptime:
> >           enabled: true
> >
> >   exporters:
> >     otlphttp:
> >       endpoint: "${env:YOUR_OTEL_COLLECTOR_ENDPOINT}"
> >       compression: gzip
> >       headers:
> >         authorization: "${env:HYPERDX_API_KEY}"
> >
> >   service:
> >     pipelines:
> >       logs:
> >         exporters:
> >           - otlphttp
> >       metrics:
> >         exporters:
> >           - otlphttp
> > ```

## 探索 Kubernetes 数据

打开您的 ClickHouse Observability 服务。在[搜索](https://clickhouse.com/docs/zh/clickstack/observability/features/search.md)页面中，选择 **Logs** source，并选择一个涵盖采集器运行期间的时间范围。找到来自某个工作负载的日志消息，查看其 Kubernetes 资源属性，例如 `k8s.namespace.name` 和 `k8s.pod.name`。

以下示例展示了来自本地 Kubernetes 集群的日志，其中包括一个输出 `Kubernetes documentation test` 消息的 `sample-logs` 工作负载。实际的服务名称和日志消息取决于您的工作负载。

![搜索页面显示来自本地集群的示例工作负载日志和 Kubernetes 系统日志](https://mintcdn.com/private-7c7dfe99/Zk1feNn6fCQBpold/images/clickstack/getting-started/observability-kubernetes-logs.png?fit=max\&auto=format\&n=Zk1feNn6fCQBpold\&q=85\&s=e07a2339b917a09a36d514f35c58658a)

在 **Chart Explorer** 中，选择 **Time Series** 和 **Metrics** source。对 `k8s.pod.memory.usage` 选择 **Average**，按 `ResourceAttributes['k8s.pod.name']` 分组，并保持 **Delta** 处于未勾选状态。选择一个涵盖采集器运行期间的时间范围，然后点击 **Run**，即可比较各 pod (容器组)  的内存使用量。如果图表出现断档，请选择不小于采集时间间隔的时间粒度。

![Chart Explorer 显示按 pod（容器组） 名称分组的 Kubernetes pod（容器组） 平均内存使用量](https://mintcdn.com/private-7c7dfe99/Zk1feNn6fCQBpold/images/clickstack/getting-started/observability-kubernetes-metrics.png?fit=max\&auto=format\&n=Zk1feNn6fCQBpold\&q=85\&s=ff2698712a19fb9cd35f7d36e979534a)

要查看预置的 Kubernetes 仪表板，请在侧边栏中打开 **Dashboards**，然后在 **Preset Dashboards** 下选择 **Kubernetes**。选择 **Logs** 和 **Metrics** source，并选择一个涵盖采集器运行期间的时间范围。

通过 **pod (容器组) **、**Nodes** 和 **Namespaces** 选项卡浏览您的集群。您还可以使用 pod (容器组) 、部署、节点、命名空间和集群筛选条件来缩小查看范围。

![Kubernetes 仪表板显示 pod（容器组） 的 CPU 和内存使用量、pod（容器组） 状态以及 Kubernetes Warning 事件](https://mintcdn.com/private-7c7dfe99/Zk1feNn6fCQBpold/images/clickstack/getting-started/observability-kubernetes-dashboard.png?fit=max\&auto=format\&n=Zk1feNn6fCQBpold\&q=85\&s=e81a725f8f2186f60f26397d46fc4fed)
