

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在 Amazon LinkedIn MSK 上使用 Apache Kafka 的巡航控制系统
<a name="cruise-control"></a>

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

**注意**  
如果您为新创建的基于 Express 的集群启用了[智能再平衡](intelligent-rebalancing.md)，则您将无法使用第三方工具（例如 Cruise Control）进行分区重新平衡。您必须先暂停智能重新平衡，才能使用这些第三方工具提供的分区重新分配 API。

**下载并构建 Cruise Control**

1. 在与 Amazon MSK 集群相同的 Amazon VPC 中创建 Amazon EC2 实例。

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

1. 在 Amazon EC2 实例上下载 [Cruise Control](https://github.com/linkedin/cruise-control/releases)。（或者，如果您愿意，也可以针对 Cruise Control 使用单独的 Amazon EC2 实例。） 对于具有 Apache Kafka 版本 2.4.\$1 的集群，请使用最新的 2.4.\$1 Cruise Control 版本。如果集群的 Apache Kafka 版本早于 2.4.\$1，请使用最新的 2.0.\$1 Cruise Control 版本。

1. 解压缩 Cruise Control 文件，然后转到解压缩后的文件夹。

1. 运行以下命令以安装 Git：

   ```
   sudo yum -y install git
   ```

1. 运行以下命令以初始化本地存储库。*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."
   ```

1. 运行以下命令以安装并构建源代码。

   ```
   ./gradlew jar copyDependantLibs
   ```

**配置和运行 Cruise Control**

1. 对 `config/cruisecontrol.properties` 文件进行以下更新。将示例引导服务器和引导代理字符串替换为集群的值。要获取集群的这些字符串，您可以在控制台中查看集群详细信息。或者，您可以使用[GetBootstrapBrokers](https://docs.aws.amazon.com//msk/1.0/apireference/clusters-clusterarn-bootstrap-brokers.html#GetBootstrapBrokers)和 [DescribeCluster](https://docs.aws.amazon.com//msk/1.0/apireference/clusters-clusterarn.html#DescribeCluster)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
   ```

   对于快速代理，建议不要在[分析器配置](https://github.com/linkedin/cruise-control/wiki/Configurations#analyzer-configurations)中配置的任何目标中使用 `DiskCapacityGoal`。

1. 编辑`config/capacityCores.json`文件以指定正确的磁盘大小、CPU 核心和网络 in/out 限制。对于快速代理，只有在设置 Cruise Control 时才需要输入 `DISK` 容量。由于 MSK 管理着快速代理的所有存储，因此应将此值设置为一个极高的数字，例如 `Integer.MAX_VALUE (2147483647)`。对于标准代理，您可以使用 [DescribeCluster](https://docs.aws.amazon.com//msk/1.0/apireference/clusters-clusterarn.html#DescribeCluster)API 操作（或 desc [ribe-cluster](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/kafka/describe-cluster.html) CLI）来获取磁盘大小。有关 CPU 核心和网络 in/out 限制的信息，请参阅 [Amazon EC2 实例类型](https://aws.amazon.com/ec2/instance-types/)。

------
#### [ 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."
       }
     ]
   }
   ```

------

1. 您可以选择安装 Cruise Control UI。如需下载，请转到 [Setting Up Cruise Control Frontend](https://github.com/linkedin/cruise-control-ui/wiki/Single-Kafka-Cluster#setting-up-cruise-control-frontend)。

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

   ```
   <path-to-your-CRUISE-CONTROL-installation>/bin/kafka-cruise-control-start.sh config/cruisecontrol.properties 9091
   ```

1. 使用 Cruise Control APIs 或 UI 来确保 Cruise Control 拥有集群负载数据并提出重新平衡建议。获得有效的指标窗口可能需要几分钟时间。
**重要**  
只有 Cruise Control 2.5.60 及以上版本与快速代理兼容，这是因为快速代理不会公开 ZooKeeper 端点。

## 使用 Cruise Control for Amazon MSK 的自动部署模板
<a name="cruise-control-cfn-template"></a>

您还可以使用此[CloudFormation 模板](https://github.com/aws-samples/cruise-control-for-msk)轻松部署 Cruise Control 和 Prometheus，从而更深入地了解您的 Amazon MSK 集群的性能并优化资源利用率。

**主要功能：**
+ 使用预配置的 Cruise Control 和 Prometheus 自动预置 Amazon EC2 实例。
+ 支持 Amazon MSK 预置集群。
+ 使用[PlainText 和 IAM](kafka_apis_iam.md) 进行灵活的身份验证。
+ Cruise Control 不依赖 Zookeeper。
+ 通过提供存储在 Amazon S3 存储桶中的您自己的配置文件，轻松自定义 Prometheus 目标、Cruise Control 容量设置和其他配置。

## 分区再平衡指南
<a name="cruise-control-partition-rebalancing"></a>

### Kafka 分区重新分配指南
<a name="cruise-control-partition-reassignment"></a>

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

**注意**  
如果您有新创建的基于 Express 的集群，请在向上或向下扩展集群时使用[智能再平衡](intelligent-rebalancing.md)进行自动分区分配。

#### 在 Cruise Control 中管理并发
<a name="cruise-control-managing-concurrency"></a>

Cruise Control 通过自动调整参数控制分区移动和领导者切换的并发。以下参数有助于在重新分配期间保持可接受的负载：
+ **最大并发分区移动**：定义 `num.concurrent.partition.movements.per.broker` 以限制代理间并发分区移动，避免网络利用率过高。  
**Example 示例**  

  ```
  num.concurrent.partition.movements.per.broker = 5
  ```

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

#### 使用限制来控制带宽
<a name="cruise-control-control-bandwidth"></a>
+ **限制参数**：使用 `kafka-reassign-partitions.sh` 进行分区重新分配时，请使用 `--throttle parameter` 设置代理之间数据移动的最大传输速率（即每秒的字节数）。  
**Example 示例**  

  ```
  --throttle 5000000
  ```

  从而将最大带宽设置为 5 Mb/s。
+ **平衡限制设置**：选择合适的限制速率至关重要：

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

  如果设置得过高，客户端可能会遇到延迟增加的问题。
+ 先从保守的限制速率开始，然后根据集群性能监控进行调整。在应用到生产环境之前，请对所选的限制进行测试，从而找出最佳平衡。

#### 在暂存环境中进行测试和验证
<a name="cruise-control-partition-rebalancing-test"></a>

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