添加新 Prometheus 抓取目标的教程:Amazon EKS 和 Kubernetes 集群上的 Redis
本教程提供了在 Amazon EKS 和 Kubernetes 上抓取示例 Redis 应用程序的 Prometheus 指标的实践介绍。Redis (https://redis.io/) 是一个开源(获得 BSD 许可)的内存数据结构存储,用作数据库、缓存和消息代理。有关更多信息,请参阅 redis
redis_exporter(MIT 许可证)用于在指定端口(默认值:0.0.0.0:9121)上公开 Redis prometheus 指标。有关更多信息,请参阅 redis_exporter
本教程使用了以下两个 Docker Hub 存储库中的 Docker 镜像:
安装公开 Prometheus 指标的示例 Redis 工作负载
-
为示例 Redis 工作负载设置命名空间。
REDIS_NAMESPACE=redis-sample
-
如果您在具有 Fargate 启动类型的集群上运行 Redis,则需要设置 Fargate 配置文件。要设置配置文件,请输入以下命令。将
MyCluster
替换为您的集群的名称。eksctl create fargateprofile --cluster
MyCluster
\ --namespace $REDIS_NAMESPACE --name $REDIS_NAMESPACE -
输入以下命令以安装示例 Redis 工作负载。
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 -
-
安装包括一个名为
my-redis-metrics
的服务,该服务在端口 9121 上公开 Redis 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 Prometheus 指标
-
通过输入以下任一命令,下载
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
-
使用文本编辑器打开文件,然后找到
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 指标放入 CloudWatch 代理允许列表中。有关这些指标的列表,请参阅以下部分。
-
如果您已在此集群中部署了具有 Prometheus 支持的 CloudWatch 代理,则必须通过输入以下命令将其删除。
kubectl delete deployment cwagent-prometheus -n amazon-cloudwatch
-
通过输入以下任一命令,使用更新的配置部署 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 Prometheus 指标
本教程会将以下指标发送到 CloudWatch 中的 ContainerInsights/Prometheus 命名空间。您可以使用 CloudWatch 控制台查看该命名空间中的指标。
指标名称 | 尺寸 |
---|---|
|
ClusterName、 |
|
ClusterName、 |
|
ClusterName、 |
|
ClusterName、 |
|
ClusterName、 |
|
ClusterName、 |
|
ClusterName、 |
|
ClusterName、 |
|
ClusterName、 |
|
Cluster、 |
注意
cmd 维度的值可以是 append
、client
、command
、config
、dbsize
、flushall
、get
、incr
、info
、latency
或 slowlog
。
db 维度的值可以从 db0
到 db15
。
您还可以为 Redis Prometheus 指标创建 CloudWatch 控制面板。
为 Redis Prometheus 指标创建控制面板
-
创建环境变量,替换以下值以匹配部署。
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
-
输入以下命令以创建控制面板。
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" \