

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

# 將 LinkedIn 的 Apache Kafka 定速控制與 Amazon MSK 搭配使用
<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. 執行以下命令來初始化本機儲存庫。使用您目前資料夾 (解壓縮 Cruise Control 下載內容時獲得的資料夾) 的名稱來取代 *Your-Cruise-Control-Folder*。

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

   對於快速代理程式，建議您不要`DiskCapacityGoal`在[分析器組態](https://github.com/linkedin/cruise-control/wiki/Configurations#analyzer-configurations)中設定的任何目標中使用 。

1. 編輯 `config/capacityCores.json` 檔案以指定正確的磁碟大小和 CPU 核心數，以及網路輸入/輸出限制。對於快速代理程式，只需要`DISK`容量項目來設定 Cruise Control。由於 MSK 會管理 Express 代理程式的所有儲存體，因此您應該將此值設定為極高的數字，例如 `Integer.MAX_VALUE (2147483647)`。對於標準代理程式，您可以使用 [DescribeCluster](https://docs.aws.amazon.com//msk/1.0/apireference/clusters-clusterarn.html#DescribeCluster) API 操作 （或 [describe-cluster](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/kafka/describe-cluster.html) CLI) 來取得磁碟大小。如需有關 CPU 核心數和網路輸入/輸出限制的詳細資訊，請參閱 [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 API 或 UI，以確保 Cruise Control 具有叢集負載資料，且正在提出重新平衡建議。可能需要花費幾分鐘來取得指標的有效視窗。
**重要**  
只有 Cruise Control 2.5.60 版和更新版本與 Express 代理程式相容，因為 Express 代理程式不會公開 Zookeeper 端點。

## 使用 Amazon MSK Cruise Control 的自動化部署範本
<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>

在生產環境中實作重新指派之前，請在具有類似組態的預備環境中執行負載測試。這可讓您微調參數，並將即時生產中的意外影響降至最低。