将 LinkedIn 的 Cruise Control for Apache Kafka 与 Amazon MSK 结合使用 - Amazon Managed Streaming for Apache Kafka

将 LinkedIn 的 Cruise Control for Apache Kafka 与 Amazon MSK 结合使用

您可以使用 LinkedIn 的 Cruise Control 来重新平衡 Amazon MSK 集群,检测和修复异常,并监控集群的状态和运行状况。

下载并构建 Cruise Control
  1. 在与 Amazon MSK 集群相同的 Amazon VPC 中创建 Amazon EC2 实例。

  2. 在上一步中创建的 Amazon EC2 实例上安装 Prometheus。记下私有 IP 和端口。默认端口号为 9090。有关如何配置 Prometheus 以聚合集群指标的信息,请参阅使用 Prometheus 监控预置 MSK 集群

  3. 在 Amazon EC2 实例上下载 Cruise Control。(或者,如果您愿意,也可以针对 Cruise Control 使用单独的 Amazon EC2 实例。) 对于具有 Apache Kafka 版本 2.4.* 的集群,请使用最新的 2.4.* Cruise Control 版本。如果集群的 Apache Kafka 版本早于 2.4.*,请使用最新的 2.0.* Cruise Control 版本。

  4. 解压缩 Cruise Control 文件,然后转到解压缩后的文件夹。

  5. 运行以下命令以安装 Git:

    sudo yum -y install git
  6. 运行以下命令以初始化本地存储库。将 Your-Cruise-Control-Folder 替换为当前文件夹(解压缩 Cruise Control 下载文件时获得的文件夹)的名称。

    git init && git add . && git commit -m "Init local repo." && git tag -a Your-Cruise-Control-Folder -m "Init local version."
  7. 运行以下命令以安装并构建源代码。

    ./gradlew jar copyDependantLibs
配置和运行 Cruise Control
  1. config/cruisecontrol.properties 文件进行以下更新。将示例引导服务器和引导代理字符串替换为集群的值。要获取集群的这些字符串,您可以在控制台中查看集群详细信息。或者,可以使用 GetBootstrapBrokersDescribeCluster API 操作或其等效 CLI 操作。

    # If using TLS encryption, use 9094; use 9092 if using plaintext bootstrap.servers=b-1.test-cluster.2skv42.c1.kafka.us-east-1.amazonaws.com:9094,b-2.test-cluster.2skv42.c1.kafka.us-east-1.amazonaws.com:9094,b-3.test-cluster.2skv42.c1.kafka.us-east-1.amazonaws.com:9094 # SSL properties, needed if cluster is using TLS encryption security.protocol=SSL ssl.truststore.location=/home/ec2-user/kafka.client.truststore.jks # Use the Prometheus Metric Sampler metric.sampler.class=com.linkedin.kafka.cruisecontrol.monitor.sampling.prometheus.PrometheusMetricSampler # Prometheus Metric Sampler specific configuration prometheus.server.endpoint=1.2.3.4:9090 # Replace with your Prometheus IP and port # Change the capacity config file and specify its path; details below capacity.config.file=config/capacityCores.json

    对于快速代理,建议不要在分析器配置中配置的任何目标中使用 DiskCapacityGoal

  2. 编辑 config/capacityCores.json 文件以指定正确的磁盘大小、CPU 核心和网络输入/输出限制。对于快速代理,只有在设置 Cruise Control 时才需要输入 DISK 容量。由于 MSK 管理着快速代理的所有存储,因此应将此值设置为一个极高的数字,例如 Integer.MAX_VALUE (2147483647)。对于标准代理,可使用DescribeCluster API 操作(或 describe-cluster CLI)来获取磁盘大小。有关 CPU 核心和网络输入/输出限制,请参阅 Amazon EC2 实例类型

    Standard broker config/capacityCores.json
    { "brokerCapacities": [ { "brokerId": "-1", "capacity": { "DISK": "10000", "CPU": { "num.cores": "2" }, "NW_IN": "5000000", "NW_OUT": "5000000" }, "doc": "This is the default capacity. Capacity unit used for disk is in MB, cpu is in number of cores, network throughput is in KB." } ] }
    Express broker config/capacityCores.json
    { "brokerCapacities":[ { "brokerId": "-1", "capacity": { "DISK": "2147483647", "CPU": {"num.cores": "16"}, "NW_IN": "1073741824", "NW_OUT": "1073741824" }, "doc": "This is the default capacity. Capacity unit used for disk is in MB, cpu is in number of cores, network throughput is in KB." } ] }
  3. 您可以选择安装 Cruise Control UI。如需下载,请转到 Setting Up Cruise Control Frontend

  4. 运行以下命令以启动 Cruise Control。考虑使用类似 screentmux 的工具来保持长时间运行的会话处于开放状态。

    <path-to-your-CRUISE-CONTROL-installation>/bin/kafka-cruise-control-start.sh config/cruisecontrol.properties 9091
  5. 使用 Cruise Control API 或 UI 来确保 Cruise Control 拥有集群加载数据并提出重新平衡建议。获得有效的指标窗口可能需要几分钟时间。

    重要

    只有 Cruise Control 2.5.60 及以上版本与快速代理兼容,这是因为快速代理不会公开 ZooKeeper 端点。

使用 Cruise Control for Amazon MSK 的自动部署模板

您还可以使用此 CloudFormation 模板轻松部署 Cruise Control 和 Prometheus,以深入了解您的 Amazon MSK 集群的性能并优化资源利用率。

主要功能:
  • 使用预配置的 Cruise Control 和 Prometheus 自动预置 Amazon EC2 实例。

  • 支持 Amazon MSK 预置集群。

  • 使用明文和 IAM 进行灵活的身份验证。

  • Cruise Control 不依赖 Zookeeper。

  • 通过提供存储在 Amazon S3 存储桶中的您自己的配置文件,轻松自定义 Prometheus 目标、Cruise Control 容量设置和其他配置。

分区再平衡指南

Kafka 分区重新分配指南

Kafka 中的分区重新分配可能占用大量资源,因为这涉及在代理之间传输大量数据,从而有可能造成网络拥塞并影响客户端操作。借助于以下最佳实践,您可以通过调整限制速率、利用并发控制和了解重新分配类型来最大限度地减小对集群操作的干扰,从而有效管理分区的重新分配。

在 Cruise Control 中管理并发

Cruise Control 通过自动调整参数控制分区移动和领导者切换的并发。以下参数有助于在重新分配期间保持可接受的负载:

  • 最大并发分区移动:定义 num.concurrent.partition.movements.per.broker 以限制代理间并发分区移动,避免网络利用率过高。

    例 示例
    num.concurrent.partition.movements.per.broker = 5

    此设置将每个代理在任何给定时间移动的分区数限制为不超过 10 个,从而平衡代理间的负载。

使用限制来控制带宽

  • 限制参数:使用 kafka-reassign-partitions.sh 进行分区重新分配时,请使用 --throttle parameter 设置代理之间数据移动的最大传输速率(即每秒的字节数)。

    例 示例
    --throttle 5000000

    从而将最大带宽设置为 5 Mb/s。

  • 平衡限制设置:选择合适的限制速率至关重要:

    如果设置得过低,则重新分配可能需要更长的时间。

    如果设置得过高,客户端可能会遇到延迟增加的问题。

  • 先从保守的限制速率开始,然后根据集群性能监控进行调整。在应用到生产环境之前,请对所选的限制进行测试,从而找出最佳平衡。

在暂存环境中进行测试和验证

在生产环境中实现重新分配之前,先在配置相似的暂存环境中进行负载测试。这样就可以微调参数,并最大限度地降低实际生产中的意外影响。