> For the complete documentation index, see [llms.txt](https://advwacloud.gitbook.io/rd-newbie-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://advwacloud.gitbook.io/rd-newbie-guide/kafka/zai-bm-zhan-dian-qi-dong-duo-ge-lb-broker-bing-da-pei-yu-ming.md).

# 在BM站點啟動多個LB Broker並搭配域名

## 目標

![](/files/-MCuzipnrQJs4sjm5FCX)

* 希望讓kafka cluster外的client能用一組固定的host去訪問kafka cluster, 相同host不同port
  * ex.&#x20;

```
kafka.bm.wise-paas.com.cn:19092,kafka.bm.wise-paas.com.cn:19093,kafka.bm.wise-paas.com.cn:19094
```

* 且不要像NodePort那樣, 還要在每個client主機上的hosts手動加上resolve record

## 前置作業 - 請BM站點維運同仁幫忙映射LB和域名

* 目前是要找陳磊
* 要跟他說, 請把這3個要暴露的url
  * kafka.bm.wise-paas.com.cn:19092
  * kafka.bm.wise-paas.com.cn:19093
  * kafka.bm.wise-paas.com.cn:19094
* 映射到下面這3個LoadBalancer service
  * roykfk-0-external
  * roykfk-1-external
  * roykfk-2-external
* 要跟他確認LB EXTERNAL-IP/Port的組合為何, 這樣才能在我們的deploy.sh陣列裡配置正確
  * ex. 是將172.21.92.212:19092映射到kafka.bm.wise-paas.com.cn:19092, 以此類推
  * 注意, 是映射LB svc的EXTERNAL-IP和Port, 不是LB svc的nodePort喔

![](/files/-MCuwoB28TpCxG03fjc7)

## 設定方式 (deploy.sh)

* `export SERVICE_TYPE="LoadBalancer"`
  * bm站點是私有雲的關係, 要映射域名只能用LB, 其他站點不一定
* `export FIRST_LISTENER_PORT=19092`
  * 起始的port要跟暴露的第一個port相同, chart裡有邏輯會跑迴圈對每個broker設定
* `export LB_EXTERNAL_ADVERTISED_LISTENERS={"kafka.bm.wise-paas.com.cn:19092"\,"kafka.bm.wise-paas.com.cn:19093"\,"kafka.bm.wise-paas.com.cn:19094"}`
  * 這個值會在內部chart做陣列處理, 將每個url分別帶進broker裡
* `export EXTERNAL_LOAD_BALANCER_IP={"172.21.92.212"\,"172.21.92.213"\,"172.21.92.214"}`
  * 這個是LoadBalancer EXTERNAL-IP, 此順序必須跟平台那邊映射域名的ip:port組合相同
  * 平台的人幫忙做的映射可能是172.21.92.212:19092,172.21.92.213:19093,172.21.92.214:19094
    * 那此陣列第一個元素就不能是172.21.92.213, 否則會組成172.21.92.213:9092

## 驗證方式

### 用Conductor測基本連線

但Conductor就只能測到如此而已, 因為multibroker要收費, 沒辦法測收發訊息

![](/files/-MCv06kbdtTfA2tjZt93)

### 自己寫Code測連線還送資料

```javascript
const { Kafka } = require('kafkajs');

const kafka = new Kafka({
  clientId: 'my-app',
  brokers: ['kafka.bm.wise-paas.com.cn:19092', 'kafka.bm.wise-paas.com.cn:19093', 'kafka.bm.wise-paas.com.cn:19094'],

  sasl: {
    mechanism: 'scram-sha-256', // scram-sha-256 or scram-sha-512
    username: 'admin',
    password: 'admin-secret'
  }
});

const producer = kafka.producer();

const run = async () => {
  let charLength = 524288;
  let fakemsg = genData(charLength);
  let topic = 'roy';

  await producer.connect();
  console.log('connected');

  let sendDataParams = [topic, fakemsg];

  setInterval(sendData, 300, ...sendDataParams);
};

function genData (charLenth) {
  let fakemsg = '';
  for (let i = 0; i < charLenth; i++) {
    fakemsg += 'a';
  }
  return fakemsg;
}

async function sendData (topic, data) {
  await producer.send({
    topic: topic,
    messages: [
      { value: data }
    ]
  });
  console.log('send done');
}

run().catch(console.error);

```

用這段code測負載平衡時, 請配合MP monitor觀察pod networking和disk用量是否有平均分配到多個broker

![](/files/-MCv2oNL_VHmBUid4goX)

![](/files/-MCv2w246jpWWcKLOKF3)

## helm chart修改幅度

* 為了能實現相同host不同port的機制, 有去改原本chart裡的template和values
* 主要是想辦法讓每個broker的advertised.listener都吃到正確的EXTERNAL URL
  * 原本的chart提供的方法是不同host相同port, 跟我們需求不同 (跟cloud kafka一樣)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://advwacloud.gitbook.io/rd-newbie-guide/kafka/zai-bm-zhan-dian-qi-dong-duo-ge-lb-broker-bing-da-pei-yu-ming.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
