零經驗玩轉隔離策略:多個 Pulsar 集羣

溫馨提示:本文是「Pulsar 傻瓜手冊」,手把手爲零經驗小白排坑避雷。只要按照指引操作,成功率 100% 喔!

本文是「Pulsar 隔離策略系列」的第 2 篇,該系列的第 1 篇博客 — 「深度解析如何在 Pulsar 中實現隔離」— 重點介紹了 Pulsar 隔離策略以及如何通過以下 3 種方式實現資源隔離:

• 多個 Pulsar 集羣 • 共享 BookKeeper 集羣 • 單個 Pulsar 集羣

本文將詳細講解第 1 種方式,即如何在「多個 Pulsar 集羣」的環境中,玩轉 Pulsar 隔離策略,併爲以下操作提供詳細步驟:

• 驗證數據隔離。• 同步並遷移集羣數據。• 擴縮容節點。

準備環境

本文以在 macOS(版本 11.2.3, 內存 8G)上操作爲示例。

如下圖所示,本文部署 2 個 Pulsar 集羣,每個集羣提供以下服務:

•1 個 ZooKeeper 節點 •1 個 bookie 節點 •1 個 broker 節點

軟件要求

Java 8

環境詳情

下文即將部署 2 個 Pulsar 集羣,組件詳情如下表所示。

部署準備

  1. 下載 Pulsar[1] 並解壓。
    本文以安裝 Pulsar 2.7.0 爲例。

  2. 在本地任意位置,按照以下目錄結構,創建相應的空文件夾。

|-separate-clusters
    |-configuration-store
        |-zk1
    |-cluster1
        |-zk1
        |-bk1
        |-broker1
    |-cluster2
        |-zk1
        |-bk1
        |-broker1
  1. 複製解壓後 Pulsar 文件夾中的內容至上一步創建的每個文件夾中。

  2. 啓動 configuration store[2]。

Configuration store 爲 Pulsar 實例提供跨集羣配置管理和任務協調。Pulsar 集羣 1 和 Pulsar 集羣 2 共享 configuration store。

cd configuration-store/zk1
bin/pulsar-daemon start configuration-store

部署 Pulsar 集羣 1

  1. 啓動 local ZooKeeper[3]。
    爲每個 Pulsar 集羣部署 1 個 local ZooKeeper,它負責爲該集羣管理配置和協調任務。
cd cluster1/zk1
bin/pulsar-daemon start zookeeper
  1. 初始化元數據 [4]。
    設置好 configuration store 和 local ZooKeeper 後,你需要向 ZooKeeper 寫入元數據。
cd cluster1/zk1
bin/pulsar initialize-cluster-metadata \
  --cluster cluster1 \
  --zookeeper localhost:2181 \
  --configuration-store localhost:2184 \
  --web-service-url http://localhost:8080/ \
  --web-service-url-tls https://localhost:8443/ \
  --broker-service-url pulsar://localhost:6650/ \
  --broker-service-url-tls pulsar+ssl://localhost:6651/
  1. 部署 BookKeeper[5]。
    BookKeeper 爲 Pulsar 提供消息持久化存儲 [6]。每個 Pulsar broker 擁有 bookie。BookKeeper 集羣和 Pulsar 集羣共享 local ZooKeeper。

1). 配置 bookie。

更改 cluster1/bk1/conf/bookkeeper.conf 文件中以下配置項的值。

allowLoopback=true
prometheusStatsHttpPort=8002
httpServerPort=8002

2). 啓動 bookie。

cd cluster1/bk1
bin/pulsar-daemon start bookie

驗證是否成功啓動 bookie。

bin/bookkeeper shell bookiesanity

輸出

Bookie sanity test succeeded
  1. 部署 broker。
  1. 配置 broker。

更改 cluster1/broker1/conf/broker.conf 文件中以下配置項的值。

zookeeperServers=127.0.0.1:2181
configurationStoreServers=127.0.0.1:2184
clusterName=cluster1
managedLedgerDefaultEnsembleSize=1
managedLedgerDefaultWriteQuorum=1
managedLedgerDefaultAckQuorum=1
  1. 啓動 broker。
cd cluster1/broker1
bin/pulsar-daemon start broker

部署 Pulsar 集羣 2

  1. 部署 local ZooKeeper。
  1. 配置 local ZooKeeper。

• 更改 cluster2/zk1/conf/zookeeper.conf 文件中以下配置項的值。

clientPort=2186
admin.serverPort=9992

• 將以下配置項添加至 cluster2/zk1/conf/pulsar_env.sh 文件。

OPTS="-Dstats_server_port=8011"
  1. 啓動 local ZooKeeper。
cd cluster2/zk1
bin/pulsar-daemon start zookeeper
  1. 初始化元數據。
bin/pulsar initialize-cluster-metadata \
  --cluster cluster2 \
  --zookeeper localhost:2186 \
  --configuration-store localhost:2184 \
  --web-service-url http://localhost:8081/ \
  --web-service-url-tls https://localhost:8444/ \
  --broker-service-url pulsar://localhost:6660/ \
  --broker-service-url-tls pulsar+ssl://localhost:6661/
  1. 部署 BookKeeper。
  1. 配置 bookie。

更改 cluster2/bk1/conf/bookkeeper.conf 文件中以下配置項的值。

bookiePort=3182
zkServers=localhost:2186
allowLoopback=true
prometheusStatsHttpPort=8003
httpServerPort=8003
  1. 啓動 bookie。
cd cluster2/bk1
bin/pulsar-daemon start bookie

驗證是否成功啓動 bookie。

bin/bookkeeper shell bookiesanity

輸出

Bookie sanity test succeeded
  1. 部署 broker。
  1. 配置 broker。

• 更改 cluster2/broker1/conf/broker.conf 文件中以下配置項的值。

clusterName=cluster2
zookeeperServers=127.0.0.1:2186
configurationStoreServers=127.0.0.1:2184
brokerServicePort=6660
webServicePort=8081
managedLedgerDefaultEnsembleSize=1
managedLedgerDefaultWriteQuorum=1
managedLedgerDefaultAckQuorum=1

• 更改 cluster2/broker1/conf/client.conf 文件中以下配置項的值。

webServiceUrl=http://localhost:8081/
brokerServiceUrl=pulsar://localhost:6660/
  1. 啓動 broker。
cd cluster2/broker1
bin/pulsar-daemon start broker

驗證數據隔離

本章驗證 2 個 Pulsar 集羣中的數據是否隔離。

  1. 創建 namespace1,並將 namespace1 分配給 cluster1。

提示:namespace 的命名規則是 /。更多關於 namespace 的信息,參閱這裏 [7]。

cd cluster1/broker1
bin/pulsar-admin namespaces create -c cluster1 public/namespace1

驗證結果

bin/pulsar-admin namespaces list public

輸出

"public/default"
"public/namespace1"
  1. 設置 namespace1 的消息保留策略。

注意:如果不設置消息保留策略且 topic 未被訂閱,一段時間後,topic 的數據會被自動清理。

bin/pulsar-admin namespaces set-retention -s 100M -t 3d public/namespace1
  1. 在 namespace1 創建 topic1,並使用寫入 1000 條數據。

提示:pulsar-client 是發送和消費數據的命令行工具。更多關於 Pulsar 命令行工具的信息,參閱這裏 [8]。

bin/pulsar-client produce -m 'hello c1 to c2' -n 1000 public/namespace1/topic1
09:56:34.504 [main] INFO  org.apache.pulsar.client.cli.PulsarClientTool - 1000 messages successfully produced

驗證結果

bin/pulsar-admin --admin-url http://localhost:8080 topics stats-internal public/namespace1/topic1

輸出

entriesAddedCounter 顯示增加了 1000 條數據。

{
 "entriesAddedCounter" : 1000,
 "numberOfEntries" : 1000,
 "totalSize" : 65616,
 "currentLedgerEntries" : 1000,
 "currentLedgerSize" : 65616,
 "lastLedgerCreatedTimestamp" : "2021-04-22T10:24:00.582+08:00",
 "waitingCursorsCount" : 0,
 "pendingAddEntriesCount" : 0,
 "lastConfirmedEntry" : "4:999",
 "state" : "LedgerOpened",
 "ledgers" : [ {
   "ledgerId" : 4,
   "entries" : 0,
   "size" : 0,
   "offloaded" : false
 } ],
 "cursors" : { },
 "compactedLedger" : {
   "ledgerId" : -1,
   "entries" : -1,
   "size" : -1,
   "offloaded" : false
 }
}
  1. 通過 cluster2(localhost:8081)查看 public/namespace1/topic1 的數據。
bin/pulsar-admin --admin-url http://localhost:8081 topics stats-internal public/namespace1/topic1

輸出

查看失敗。打印信息顯示 public/namespace1 僅分配至 cluster1,未分配至 cluster 2。此時驗證了數據已隔離。

Namespace missing local cluster name in clusters list: local_cluster=cluster2 ns=public/namespace1 clusters=[cluster1]
Reason: Namespace missing local cluster name in clusters list: local_cluster=cluster2 ns=public/namespace1 clusters=[cluster1]
  1. 在 cluster2 中,向 public/namespace1/topic1 寫入數據。
cd cluster2/broker1
bin/pulsar-client produce -m 'hello c1 to c2' -n 1000 public/namespace1/topic1

輸出

結果顯示寫入消息數量爲 0,操作失敗,因爲 namespace1 僅分配至 cluster1,未分配至 cluster 2。此時驗證了數據已隔離。

12:09:50.005 [main] INFO  org.apache.pulsar.client.cli.PulsarClientTool - 0 messages successfully produced

遷移數據

以下步驟接着前文「驗證數據隔離」繼續操作。

確認數據已隔離後,本章講述如何同步(使用跨地域複製功能)並遷移集羣數據。

  1. 分配 namespace1 至 cluster2,即添加 cluster2 至 namesapce1 的 cluster 列表。

該步驟啓用跨地域複製功能,同步 cluster1 和 cluster2 的數據。

bin/pulsar-admin namespaces set-clusters --clusters cluster1,cluster2 public/namespace1

驗證結果

bin/pulsar-admin namespaces get-clusters public/namespace1

輸出

"cluster1"
"cluster2"
  1. 查看 cluster2 是否有 topic1。
bin/pulsar-admin --admin-url http://localhost:8081 topics stats-internal public/namespace1/topic1

輸出

結果顯示 cluster2 的 topic1 有 1000 條數據,說明 cluster1 的 topic1 數據已成功複製至 cluster2。

{
  "entriesAddedCounter" : 1000,
  "numberOfEntries" : 1000,
  "totalSize" : 75616,
  "currentLedgerEntries" : 1000,
  "currentLedgerSize" : 75616,
  "lastLedgerCreatedTimestamp" : "2021-04-23T12:02:52.929+08:00",
  "waitingCursorsCount" : 1,
  "pendingAddEntriesCount" : 0,
  "lastConfirmedEntry" : "1:999",
  "state" : "LedgerOpened",
  "ledgers" : [ {
    "ledgerId" : 1,
    "entries" : 0,
    "size" : 0,
    "offloaded" : false
  } ],
  "cursors" : {
    "pulsar.repl.cluster1" : {
      "markDeletePosition" : "1:999",
      "readPosition" : "1:1000",
      "waitingReadOp" : true,
      "pendingReadOps" : 0,
      "messagesConsumedCounter" : 1000,
      "cursorLedger" : 2,
      "cursorLedgerLastEntry" : 2,
      "individuallyDeletedMessages" : "[]",
      "lastLedgerSwitchTimestamp" : "2021-04-23T12:02:53.248+08:00",
      "state" : "Open",
      "numberOfEntriesSinceFirstNotAckedMessage" : 1,
      "totalNonContiguousDeletedMessagesRange" : 0,
      "properties" : { }
    }
  },
  "compactedLedger" : {
    "ledgerId" : -1,
    "entries" : -1,
    "size" : -1,
    "offloaded" : false
  }
}
  1. 遷移 cluster1 的 producer 和 consumer 至 cluster2。
PulsarClient pulsarClient1 = PulsarClient.builder().serviceUrl("pulsar://localhost:6650").build();
// migrate the client to cluster2 pulsar://localhost:6660
PulsarClient pulsarClient2 = PulsarClient.builder().serviceUrl("pulsar://localhost:6660").build();
  1. 從 namespace1 的 cluster 列表中移除 cluster1 。
bin/pulsar-admin namespaces set-clusters --clusters cluster2 public/namespace1
  1. 檢查 cluster1 的 topic1 的數據。
cd cluster1/broker1
bin/pulsar-admin --admin-url http://localhost:8080 topics stats-internal public/namespace1/topic1

輸出

結果顯示數據爲空,說明數據已從 cluster1 的 topic1 中成功移除。

{
  "entriesAddedCounter" : 0,
  "numberOfEntries" : 0,
  "totalSize" : 0,
  "currentLedgerEntries" : 0,
  "currentLedgerSize" : 0,
  "lastLedgerCreatedTimestamp" : "2021-04-23T15:20:08.1+08:00",
  "waitingCursorsCount" : 1,
  "pendingAddEntriesCount" : 0,
  "lastConfirmedEntry" : "3:-1",
  "state" : "LedgerOpened",
  "ledgers" : [ {
    "ledgerId" : 3,
    "entries" : 0,
    "size" : 0,
    "offloaded" : false
  } ],
  "cursors" : {
    "pulsar.repl.cluster2" : {
      "markDeletePosition" : "3:-1",
      "readPosition" : "3:0",
      "waitingReadOp" : true,
      "pendingReadOps" : 0,
      "messagesConsumedCounter" : 0,
      "cursorLedger" : 4,
      "cursorLedgerLastEntry" : 0,
      "individuallyDeletedMessages" : "[]",
      "lastLedgerSwitchTimestamp" : "2021-04-23T15:20:08.122+08:00",
      "state" : "Open",
      "numberOfEntriesSinceFirstNotAckedMessage" : 1,
      "totalNonContiguousDeletedMessagesRange" : 0,
      "properties" : { }
    }
  },
  "compactedLedger" : {
    "ledgerId" : -1,
    "entries" : -1,
    "size" : -1,
    "offloaded" : false
  }
}

至此,我們已將 cluster1 的 topic1 數據成功複製至 cluster2,之後移除了 cluster1 的 topic1 數據。

擴縮容節點

本章講述如何擴縮容 broker 和 bookie 節點。

Broker

增加 broker 節點

本示例在 cluster1/broker1 創建 2 個 partitioned topic,再增加 2 個 broker 節點。之後卸載 partitioned topic 數據,並查看數據在 3 個 broker 之間的分配情況。

  1. 查看 cluster1 的 broker 信息。
cd/cluster1/broker1
bin/pulsar-admin brokers list cluster1

輸出
結果說明當前 cluster1 只有 broker1。

"192.168.0.105:8080"
  1. 在 cluster1/broker1 上創建 2 個 partitioned topic。
    爲 partitioned-topic1 創建 6 個分區,爲 partitioned-topic2 創建 7 個分區。
bin/pulsar-admin topics create-partitioned-topic -p 6 public/namespace1/partitioned-topic1
bin/pulsar-admin topics create-partitioned-topic -p 7 public/namespace1/partitioned-topic2

查看結果。

bin/pulsar-admin topics partitioned-lookup public/namespace1/partitioned-topic1

輸出

結果顯示,partitioned-topic1 所有數據屬於 broker1。

"persistent://public/namespace1/partitioned-topic1-partition-0    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-1    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-2    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-3    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-4    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-5    pulsar://192.168.0.105:6650"

輸入

bin/pulsar-admin topics partitioned-lookup public/namespace1/partitioned-topic2

輸出

結果顯示,partitioned-topic2 所有數據屬於 broker1。

"persistent://public/namespace1/partitioned-topic2-partition-0    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-1    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-2    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-3    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-4    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-5    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-6    pulsar://192.168.0.105:6650"
  1. 新增 2 個 broker 節點:broker2 和 broker3。
  1. 部署準備。
    在 cluster1 文件夾中創建 broker2 和 broker3 文件夾,複製解壓後 Pulsar 文件夾中的內容至 broker2 和 broker3 文件夾。
|-separate-clusters
    |-configuration-store
        |-zk1
    |-cluster1
        |-zk1
        |-bk1
        |-broker1
        |-broker2
        |-broker3
    |-cluster2
        |-zk1
        |-bk1
        |-broker1
  1. 部署 broker。

a). 配置 broker。

b). 啓動 broker。

c). 查看 cluster1 中已啓動的 broker。

bin/pulsar-admin brokers list cluster1

輸出

"192.168.0.105:8080" // broker1
"192.168.0.105:8082" // broker2
"192.168.0.105:8083" // broker3
  1. 卸載 namespace 1 中 partitioned-topic1 的數據。
bin/pulsar-admin namespaces unload public/namespace1

驗證結果。

1). 查看 partitioned-topic1 的數據分配情況。

bin/pulsar-admin topics partitioned-lookup public/namespace1/partitioned-topic1

輸出

結果顯示,partitioned-topic1 的數據平均分配在 broker1、broker2 和 broker3 上。

"persistent://public/namespace1/partitioned-topic1-partition-0    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-1    pulsar://192.168.0.105:6653"
"persistent://public/namespace1/partitioned-topic1-partition-2    pulsar://192.168.0.105:6652"
"persistent://public/namespace1/partitioned-topic1-partition-3    pulsar://192.168.0.105:6653"
"persistent://public/namespace1/partitioned-topic1-partition-4    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-5    pulsar://192.168.0.105:6653"

2). 查看 partitioned-topic2 的數據分配情況。

bin/pulsar-admin topics partitioned-lookup public/namespace1/partitioned-topic2

結果顯示,partitioned-topic2 的數據平均分配在 broker1、broker2 和 broker3 上。

輸出

"persistent://public/namespace1/partitioned-topic2-partition-0    pulsar://192.168.0.105:6653"
"persistent://public/namespace1/partitioned-topic2-partition-1    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-2    pulsar://192.168.0.105:6653"
"persistent://public/namespace1/partitioned-topic2-partition-3    pulsar://192.168.0.105:6652"
"persistent://public/namespace1/partitioned-topic2-partition-4    pulsar://192.168.0.105:6653"
"persistent://public/namespace1/partitioned-topic2-partition-5    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-6    pulsar://192.168.0.105:6653"

減少 broker 節點

以下步驟接着前文「如何增加 broker 節點」繼續操作。

本示例在 cluster1 中減少 1 個 broker 節點,並查看 partitioned topic 數據在其餘 2 個 broker 之間的分配情況。

  1. 減少 1 個 broker 節點,即停止 broker3。
cd/cluster1/broker3
bin/pulsar-daemon stop broker

驗證結果。

bin/pulsar-admin brokers list cluster1

輸出

結果顯示,當前 cluster1 僅啓動了 broker1 和 broker2。

"192.168.0.105:8080" // broker1
"192.168.0.105:8082" // broker2
  1. 查看 partitioned-topic1 數據的分配情況。
bin/pulsar-admin topics partitioned-lookup public/namespace1/partitioned-topic1

輸出

結果顯示,partitioned-topic1 數據平均分配至 broker1 和 broker2,即原屬於 broker3 的數據已被重新平均分配至 broker1 和 broker2。

"persistent://public/namespace1/partitioned-topic1-partition-0    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-1    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-2    pulsar://192.168.0.105:6652"
"persistent://public/namespace1/partitioned-topic1-partition-3    pulsar://192.168.0.105:6652"
"persistent://public/namespace1/partitioned-topic1-partition-4    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic1-partition-5    pulsar://192.168.0.105:6650"

同理,partitioned-topic2 的數據也被平均分配至 broker1 和 broker2。

bin/pulsar-admin topics partitioned-lookup public/namespace1/partitioned-topic2

輸出

"persistent://public/namespace1/partitioned-topic2-partition-0    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-1    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-2    pulsar://192.168.0.105:6652"
"persistent://public/namespace1/partitioned-topic2-partition-3    pulsar://192.168.0.105:6652"
"persistent://public/namespace1/partitioned-topic2-partition-4    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-5    pulsar://192.168.0.105:6650"
"persistent://public/namespace1/partitioned-topic2-partition-6    pulsar://192.168.0.105:6652"

Bookie

增加 bookie 節點

本示例在 cluster1/bookkeeper 1 已有 bookie1,增加 2 個 bookie 節點後,向 topic1 寫入數據,並查看數據是否保存了多個副本。

  1. 查看 cluster1 的 bookie 信息。
cd cluster1/bk1
bin/bookkeeper shell listbookies -rw -h

輸出

結果說明當前 cluster1 只有 bookie1。

12:31:34.933 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - ReadWrite Bookies :
12:31:34.946 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - BookieID:192.168.0.105:3181, IP:192.168.0.105, Port:3181, Hostname:192.168.0.105
  1. 允許 3 個 bookie 節點服務。

更改 cluster1/broker1/conf/broker.conf 文件中以下配置項的值。

managedLedgerDefaultEnsembleSize=3 // 指定 bookie 節點服務的數量
managedLedgerDefaultWriteQuorum=3 // 指定數據副本寫入的數量
managedLedgerDefaultAckQuorum=2  // 指定數據成功寫入幾個副本後,數據纔算寫入成功
  1. 重啓 broker1,使配置生效。
cd cluster1/broker1
bin/pulsar-daemon stop broker
bin/pulsar-daemon start broker
  1. 設置 public/default 的消息保留策略。

注意:如果不設置消息保留策略且 topic 未被訂閱,一段時間後,topic 的數據會被自動清理。

cd cluster1/broker1
bin/pulsar-admin namespaces set-retention -s 100M -t 3d public/default
  1. 在 public/default 創建 topic1,並寫入 100 條數據。
bin/pulsar-client produce -m 'hello' -n 100 topic1

輸出

結果顯示 bookie 節點數量不足導致數據寫入失敗。

···
12:40:38.886 [pulsar-client-io-1-1] WARN  org.apache.pulsar.client.impl.ClientCnx - [id: 0x56f92aff, L:/192.168.0.105:53069 - R:/192.168.0.105:6650] Received error from server: org.apache.bookkeeper.mledger.ManagedLedgerException: Not enough non-faulty bookies available
...
12:40:38.886 [main] ERROR org.apache.pulsar.client.cli.PulsarClientTool - Error while producing messages
…
12:40:38.890 [main] INFO  org.apache.pulsar.client.cli.PulsarClientTool - 0 messages successfully produced
  1. 新增 2 個 bookie 節點:bookie2 和 bookie3。

1). 部署準備。

在 cluster1 中新增 bk2 和 bk3 文件夾,複製解壓後 Pulsar 文件夾中的內容至 bk2 和 bk3 文件夾。

|-separate-clusters
    |-configuration-store
        |-zk1
    |-cluster1
        |-zk1
        |-bk1
        |-bk2
        |-bk3
        |-broker1
    |-cluster2
        |-zk1
        |-bk1
        |-broker1

2). 部署 bookie。

a). 配置 bookie。

b). 啓動 bookie。

c). 檢查 cluster1 中已啓動的 bookie。

bin/bookkeeper shell listbookies -rw -h

輸出

結果顯示 cluster 1 中已啓動 3 個 bookie:

•bookie1:192.168.0.105:3181•bookie2:192.168.0.105:3183•bookie3:192.168.0.105:3184

12:12:47.574 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - BookieID:192.168.0.105:3183, IP:192.168.0.105, Port:3183, Hostname:192.168.0.105 
12:12:47.575 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - BookieID:192.168.0.105:3184, IP:192.168.0.105, Port:3184, Hostname:192.168.0.105
12:12:47.576 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - BookieID:192.168.0.105:3181, IP:192.168.0.105, Port:3181, Hostname:192.168.0.105
  1. 設置 public/default 的消息保留策略。

注意:如果不設置消息保留策略且 topic 未被訂閱,一段時間後,topic 的數據會被自動清理。

cd cluster1/broker1
bin/pulsar-admin namespaces set-retention -s 100M -t 3d public/default
  1. 在 public/default 創建 topic1,並寫入 100 條數據。
bin/pulsar-client produce -m 'hello' -n 100 topic1

輸出

結果顯示數據寫入成功。

...
12:17:40.222 [main] INFO  org.apache.pulsar.client.cli.PulsarClientTool - 100 messages successfully produced
  1. 查看 topic1 的信息。
bin/pulsar-admin topics stats-internal topic1

輸出

結果顯示 ledgerId 5 保存了 topic1 的數據。

{
  "entriesAddedCounter" : 100,
  "numberOfEntries" : 100,
  "totalSize" : 5500,
  "currentLedgerEntries" : 100,
  "currentLedgerSize" : 5500,
  "lastLedgerCreatedTimestamp" : "2021-05-11T12:17:38.881+08:00",
  "waitingCursorsCount" : 0,
  "pendingAddEntriesCount" : 0,
  "lastConfirmedEntry" : "5:99",
  "state" : "LedgerOpened",
  "ledgers" : [ {
    "ledgerId" : 5,
    "entries" : 0,
    "size" : 0,
    "offloaded" : false
  } ],
  "cursors" : { },
  "compactedLedger" : {
    "ledgerId" : -1,
    "entries" : -1,
    "size" : -1,
    "offloaded" : false
  }
}
  1. 查看 ledgerid 5 存儲在哪些 bookie 節點上。
bin/bookkeeper shell ledgermetadata -ledgerid 5

輸出

結果顯示正如前文所配置, ledgerid 5 存儲在 bookie1(3181)、bookie2(3183) 和 bookie3(3184)上。

...
12:23:17.705 [main] INFO  org.apache.bookkeeper.tools.cli.commands.client.LedgerMetaDataCommand - ledgerID: 5
12:23:17.714 [main] INFO  org.apache.bookkeeper.tools.cli.commands.client.LedgerMetaDataCommand - LedgerMetadata{formatVersion=3, ensembleSize=3, writeQuorumSize=3, ackQuorumSize=2, state=OPEN, digestType=CRC32C, password=base64:, ensembles={0=[192.168.0.105:3184, 192.168.0.105:3181, 192.168.0.105:3183]}, customMetadata={component=base64:bWFuYWdlZC1sZWRnZXI=, pulsar/managed-ledger=base64:cHVibGljL2RlZmF1bHQvcGVyc2lzdGVudC90b3BpYzE=, application=base64:cHVsc2Fy}}
...

減少 bookie 節點

以下步驟接着前文「如何增加 bookie 節點」繼續操作。

本示例在 cluster1 中減少 2 個 bookie 節點,再向 topic2 寫入數據,並查看數據保存在哪些節點。

  1. 允許 1 個 bookie 節點服務。

更改 cluster1/broker1/conf/broker.conf 文件中以下配置項的值。

managedLedgerDefaultEnsembleSize=1 // 指定 bookie 節點服務的數量
managedLedgerDefaultWriteQuorum=1 // 指定數據副本寫入的數量
managedLedgerDefaultAckQuorum=1  // 指定數據成功寫入幾個副本後,數據纔算寫入成功
  1. 重啓 broker1,使配置生效。
cd cluster1/broker1
bin/pulsar-daemon stop broker
bin/pulsar-daemon start broker
  1. 查看 cluster1 的 bookie 信息。
cd cluster1/bk1
bin/bookkeeper shell listbookies -rw -h

輸出

結果說明當前 cluster1 已啓動了 bookie1(3181)、bookie2(3183) 和 bookie3(3184)。

...
15:47:41.370 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - ReadWrite Bookies :
15:47:41.382 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - BookieID:192.168.0.105:3183, IP:192.168.0.105, Port:3183, Hostname:192.168.0.105
15:47:41.383 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - BookieID:192.168.0.105:3184, IP:192.168.0.105, Port:3184, Hostname:192.168.0.105
15:47:41.384 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - BookieID:192.168.0.105:3181, IP:192.168.0.105, Port:3181, Hostname:192.168.0.105
…
  1. 減少 2 個 bookie 節點,即停止 bookie2 和 bookie3。

提示:更多關於如何減少 bookie 的信息,參閱這裏 [9]。

cd cluster1/bk2
bin/bookkeeper shell listunderreplicated
bin/pulsar-daemon stop bookie
bin/bookkeeper shell decommissionbookie
cd cluster1/bk3
bin/bookkeeper shell listunderreplicated
bin/pulsar-daemon stop bookie
bin/bookkeeper shell decommissionbookie
  1. 查看 cluster1 的 bookie 信息。
cd cluster1/bk1
bin/bookkeeper shell listbookies -rw -h

輸出

結果說明當前 cluster1 僅啓動了 bookie1(3181)。

…
16:05:28.690 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - ReadWrite Bookies :
16:05:28.700 [main] INFO  org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand - BookieID:192.168.0.105:3181, IP:192.168.0.105, Port:3181, Hostname:192.168.0.105
...
  1. 設置 public/default 的消息保留策略。

注意:如果不設置消息保留策略且沒有訂閱,一段時間後,數據會被自動清理。

cd cluster1/broker1
bin/pulsar-admin namespaces set-retention -s 100M -t 3d public/default
  1. 在 public/default 創建 topic2,並寫入 100 條數據。
bin/pulsar-client produce -m 'hello' -n 100 topic2

輸出

結果顯示數據寫入成功。

…
16:06:59.448 [main] INFO  org.apache.pulsar.client.cli.PulsarClientTool - 100 messages successfully produced
  1. 查看 topic2 的信息。
bin/pulsar-admin topics stats-internal topic2

輸出

結果顯示 ledgerId 7 保存了 topic 2 的數據。

{
  "entriesAddedCounter" : 100,
  "numberOfEntries" : 100,
  "totalSize" : 5400,
  "currentLedgerEntries" : 100,
  "currentLedgerSize" : 5400,
  "lastLedgerCreatedTimestamp" : "2021-05-11T16:06:59.058+08:00",
  "waitingCursorsCount" : 0,
  "pendingAddEntriesCount" : 0,
  "lastConfirmedEntry" : "7:99",
  "state" : "LedgerOpened",
  "ledgers" : [ {
    "ledgerId" : 7,
    "entries" : 0,
    "size" : 0,
    "offloaded" : false
  } ],
  "cursors" : { },
  "compactedLedger" : {
    "ledgerId" : -1,
    "entries" : -1,
    "size" : -1,
    "offloaded" : false
  }
}
  1. 查看 ledgerid 7 存儲在哪些 bookie 節點上。
bin/bookkeeper shell ledgermetadata -ledgerid 7

輸出

結果顯示 ledgerid 7 存儲在 bookie1(3181)上。

...
16:11:28.843 [main] INFO  org.apache.bookkeeper.tools.cli.commands.client.LedgerMetaDataCommand - ledgerID: 7
16:11:28.846 [main] INFO  org.apache.bookkeeper.tools.cli.commands.client.LedgerMetaDataCommand - LedgerMetadata{formatVersion=3, ensembleSize=1, writeQuorumSize=1, ackQuorumSize=1, state=OPEN, digestType=CRC32C, password=base64:, ensembles={0=[192.168.0.105:3181]}, customMetadata={component=base64:bWFuYWdlZC1sZWRnZXI=, pulsar/managed-ledger=base64:cHVibGljL2RlZmF1bHQvcGVyc2lzdGVudC90b3BpYzM=, application=base64:cHVsc2Fy}}

總結

本文是「Pulsar 隔離策略系列」的第 2 篇,講解了如何在「多個 Pulsar 集羣」的環境中驗證數據隔離、同步集羣數據和擴縮容節點。

本系列的下一篇博客將詳細分析如何在「共享 BookKeeper 集羣」的環境中玩轉 Pulsar 隔離策略,同樣也會是「Pulsar 傻瓜手冊」喔!敬請期待!

引用鏈接

[1] 下載 Pulsar: https://pulsar.apache.org/docs/en/next/standalone/#install-pulsar-using-binary-release
[2] configuration store: https://pulsar.apache.org/docs/en/next/deploy-bare-metal-multi-cluster/#deploy-the-configuration-store
[3] local ZooKeeper: https://pulsar.apache.org/docs/en/next/deploy-bare-metal-multi-cluster/#deploy-local-zookeeper
[4] 初始化元數據: https://pulsar.apache.org/docs/en/next/deploy-bare-metal-multi-cluster/#cluster-metadata-initialization
[5] 部署 BookKeeper: https://pulsar.apache.org/docs/en/next/deploy-bare-metal-multi-cluster/#deploy-bookkeeper
[6] 消息持久化存儲: https://pulsar.apache.org/docs/en/next/deploy-bare-metal-multi-cluster/#deploy-bookkeeper
[7] 這裏: https://pulsar.apache.org/docs/en/next/concepts-messaging/#namespaces
[8] 這裏: https://pulsar.apache.org/docs/en/next/reference-cli-tools/
[9] 這裏: https://bookkeeper.apache.org/docs/4.13.0/admin/decomission/
[10] Pulsar 隔離策略 - 官網文檔: https://pulsar.apache.org/docs/en/next/administration-isolation/

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/C00LFvbkR7RSnHnvvygcKQ