

# 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 ライセンス適用) は、指定されたポート (デフォルト: 0.0.0.0:9121) で Redis OSS Prometheus メトリクスを公開するために使用します。詳細については、「[redis\$1exporter](https://github.com/oliver006/redis_exporter)」を参照してください。

このチュートリアルでは、次の 2 つの Docker ハブリポジトリの 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. インストールには、ポート 9121 で Redis OSS Prometheus メトリクスを公開する、`my-redis-metrics` という名前のサービスが含まれています。サービスの詳細を取得するには、次のコマンドを入力します。

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

   結果の `Annotations` セクションには、CloudWatch エージェントの Prometheus スクレイプ設定に一致する 2 つの注釈が表示されます。これにより、ワークロードを自動検出できます。

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

   関連する Prometheus スクレイプの設定は、`- job_name: kubernetes-service-endpoints` または `kubernetes-eks.yaml` の `kubernetes-k8s.yaml` のセクションに記載されています。

**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* と *リージョン*を設定に合わせて置き換えます。

   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" \
   ```