

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 新增 Prometheus 湊集目標的教學課程：Amazon EKS 和 Kubernetes 叢集上的 Redis OSS
<a name="ContainerInsights-Prometheus-Setup-redis-eks"></a>

本教學課程提供實作介紹，讓您在 Amazon EKS 和 Kubernetes 上湊集範例 Redis OSS 應用程式的 Prometheus 指標。Redis OSS (https://redis.io/) 是一個開放原始碼 (BSD 授權)、記憶體內資料結構存儲，可用作資料庫、快取和訊息代理程式。如需詳細資訊，請參閱 [redis](https://redis.io/)。

redis\$1exporter (授權的 MIT 授權) 可用於在指定的連接埠上公開 Redis OSS Prometheus 指標 (預設：0.0.0.0:9121)。如需詳細資訊，請參閱 [redis\$1exporter](https://github.com/oliver006/redis_exporter)。

本教學課程會使用下列兩個 Docker Hub 儲存庫中的 Docker 影像：
+ [redis](https://hub.docker.com/_/redis?tab=description)
+ [redis\$1exporter](https://hub.docker.com/r/oliver006/redis_exporter)

**若要安裝公開 Prometheus 指標的範例 Redis OSS 工作負載**

1. 設定範例 Redis OSS 工作負載的命名空間。

   ```
   REDIS_NAMESPACE=redis-sample
   ```

1. 如果您在具有 Fargate 啟動類型的叢集上執行 Redis OSS，則需要設定 Fargate 描述檔。若要設定描述檔，請輸入下列命令。將 *MyCluster* 取代為您的叢集名稱。

   ```
   eksctl create fargateprofile --cluster MyCluster \
   --namespace $REDIS_NAMESPACE --name $REDIS_NAMESPACE
   ```

1. 輸入下列命令以安裝範例 Redis OSS 工作負載。

   ```
   curl https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/service/cwagent-prometheus/sample_traffic/redis/redis-traffic-sample.yaml \
   | sed "s/{{namespace}}/$REDIS_NAMESPACE/g" \
   | kubectl apply -f -
   ```

1. 此安裝包含名為 `my-redis-metrics` 的服務，而該服務會在連接埠 9121 上公開 Redis OSS Prometheus 指標。請輸入下列命令，以取得服務的詳細資訊：

   ```
   kubectl describe service/my-redis-metrics  -n $REDIS_NAMESPACE
   ```

   在結果的 `Annotations` 區段中，您會看到兩個符合 CloudWatch 代理程式 Prometheus 刮取組態的註釋，以便它可以自動探索工作負載：

   ```
   prometheus.io/port: 9121
   prometheus.io/scrape: true
   ```

   相關的 Prometheus 湊集組態可以在 `kubernetes-eks.yaml` 或 `kubernetes-k8s.yaml` 的 `- job_name: kubernetes-service-endpoints` 區段找到。

**若要開始在 CloudWatch 中收集 Redis OSS Prometheus 指標**

1. 輸入下列其中一個命令，以下載最新版本的 `kubernetes-eks.yaml` 或 `kubernetes-k8s.yaml` 檔案。對於具有 EC2 啟動類型的 Amazon EKS 叢集，請輸入此命令。

   ```
   curl -O https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/service/cwagent-prometheus/prometheus-eks.yaml
   ```

   對於具有 Fargate 啟動類型的 Amazon EKS 叢集，請輸入此命令。

   ```
   curl -O https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/service/cwagent-prometheus/prometheus-eks-fargate.yaml
   ```

   對於在 Amazon EC2 執行個體上執行的 Kubernetes 叢集，請輸入下列命令。

   ```
   curl -O https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/service/cwagent-prometheus/prometheus-k8s.yaml
   ```

1. 使用文字編輯器開啟檔案，然後找出 `cwagentconfig.json` 區段。新增下列子區段並儲存變更。請務必遵循現有的縮排模式。

   ```
   {
     "source_labels": ["pod_name"],
     "label_matcher": "^redis-instance$",
     "dimensions": [["Namespace","ClusterName"]],
     "metric_selectors": [
       "^redis_net_(in|out)put_bytes_total$",
       "^redis_(expired|evicted)_keys_total$",
       "^redis_keyspace_(hits|misses)_total$",
       "^redis_memory_used_bytes$",
       "^redis_connected_clients$"
     ]
   },
   {
     "source_labels": ["pod_name"],
     "label_matcher": "^redis-instance$",
     "dimensions": [["Namespace","ClusterName","cmd"]],
     "metric_selectors": [
       "^redis_commands_total$"
     ]
   },
   {
     "source_labels": ["pod_name"],
     "label_matcher": "^redis-instance$",
     "dimensions": [["Namespace","ClusterName","db"]],
     "metric_selectors": [
       "^redis_db_keys$"
     ]
   },
   ```

   您新增的區段會將 Redis OSS 指標放到 CloudWatch 代理程式允許清單中。如需了解這些指標的清單，請參閱下列章節。

1. 如果您已在此叢集中部署具有 Prometheus 支援的 CloudWatch 代理程式，則必須輸入下列命令來刪除它。

   ```
   kubectl delete deployment cwagent-prometheus -n amazon-cloudwatch
   ```

1. 輸入下列其中一個命令，以更新的組態來部署 CloudWatch 代理程式：將 *MyCluster* 和 *region* 取代為可比對設定的數值。

   對於具有 EC2 啟動類型的 Amazon EKS 叢集，請輸入此命令。

   ```
   kubectl apply -f prometheus-eks.yaml
   ```

   對於具有 Fargate 啟動類型的 Amazon EKS 叢集，請輸入此命令。

   ```
   cat prometheus-eks-fargate.yaml \
   | sed "s/{{cluster_name}}/MyCluster/;s/{{region_name}}/region/" \
   | kubectl apply -f -
   ```

   對於 Kubernetes 叢集，請輸入此命令。

   ```
   cat prometheus-k8s.yaml \
   | sed "s/{{cluster_name}}/MyCluster/;s/{{region_name}}/region/" \
   | kubectl apply -f -
   ```

## 檢視 Redis OSS Prometheus 指標
<a name="ContainerInsights-Prometheus-Setup-redis-eks-view"></a>

此教學課程會將下列指標傳送至 CloudWatch 中的 **ContainerInsights/Prometheus** 命名空間。您可以使用 CloudWatch 主控台查看該命名空間中的指標。


| 指標名稱 | 維度 | 
| --- | --- | 
|  `redis_net_input_bytes_total` |  ClusterName、`Namespace`  | 
|  `redis_net_output_bytes_total` |  ClusterName、`Namespace`  | 
|  `redis_expired_keys_total` |  ClusterName、`Namespace`  | 
|  `redis_evicted_keys_total` |  ClusterName、`Namespace`  | 
|  `redis_keyspace_hits_total` |  ClusterName、`Namespace`  | 
|  `redis_keyspace_misses_total` |  ClusterName、`Namespace`  | 
|  `redis_memory_used_bytes` |  ClusterName、`Namespace`  | 
|  `redis_connected_clients` |  ClusterName、`Namespace`  | 
|  `redis_commands_total` |  ClusterName、`Namespace`、cmd  | 
|  `redis_db_keys` |  ClusterName、`Namespace`、db  | 

**注意**  
**cmd** 維度的數值可以是：`append`、`client`、`command`、`config`、`dbsize`、`flushall`、`get`、`incr`、`info` `latency` 或 `slowlog`。  
**db** 維度的數值可以是 `db0` 至 `db15`。

您也可以為 Redis OSS Prometheus 指標建立 CloudWatch 儀表板。

**若要建立 Redis OSS Prometheus 指標的儀表板**

1. 建立環境變數，取代下面的數值，以符合您的部署。

   ```
   DASHBOARD_NAME=your_cw_dashboard_name
   REGION_NAME=your_metric_region_such_as_us-east-1
   CLUSTER_NAME=your_k8s_cluster_name_here
   NAMESPACE=your_redis_service_namespace_here
   ```

1. 輸入下列命令建立儀表板。

   ```
   curl https://raw.githubusercontent.com/aws-samples/amazon-cloudwatch-container-insights/latest/k8s-deployment-manifest-templates/deployment-mode/service/cwagent-prometheus/sample_cloudwatch_dashboards/redis/cw_dashboard_redis.json \
   | sed "s/{{YOUR_AWS_REGION}}/${REGION_NAME}/g" \
   | sed "s/{{YOUR_CLUSTER_NAME}}/${CLUSTER_NAME}/g" \
   | sed "s/{{YOUR_NAMESPACE}}/${NAMESPACE}/g" \
   ```