「Confluent」:修訂間差異

出自Gea-Suan Lin's Wiki
跳至導覽 跳至搜尋
本頁面具有訪問限制。如果您看見此訊息,這代表您沒有訪問本頁面的權限。
 
(未顯示同一使用者於中間所作的 78 次修訂)
行 1: 行 1:
'''Confluent'''是一家公司,也是該公司 [[Apache Kafka]]產品名。
'''Confluent'''是一家公司, 同時 也是該公司[[Apache Kafka]] 產品線的 產品名。


== 簡介 ==
== 簡介 ==
行 7: 行 7:
== 硬體 ==
== 硬體 ==


 在PoC時我使用了三台<code>t3.small</code>(2 GB RAM,另外手動設定加上512 MB Swap),三台都安裝完整的套件並且跑起來,一開始不會有問題,但跑一陣子後會因為記憶體不足而異常。建議在正式環境下 建議 使用<code>m5.large</code>(8 GB RAM)或是更好的機器執行。
 在PoC時我使用了三台<code>t3.small</code>(2 GB RAM,另外手動設定加上512 MB Swap),三台都安裝完整的套件並且跑起來,一開始不會有問題,但跑一陣子後會因為記憶體不足而異常。
 
建議在正式環境下 ,ZooKeeper與Kafka分開跑,都 使用<code>m5a.large</code>(8 GB RAM)或是更好的機器執行。


== 安裝 ==
== 安裝 ==


 當然要先裝[[Java]]:
 當然要先裝[[Java]] ,然後安裝社群版本,這邊因為官方還沒有提供<code>jammy</code>版本(Ubuntu 22.04),這邊手動寫成<code>focal</code>


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
sudo apt install -y default-jre
wget -qO - https://packages.confluent.io/deb/7.4/archive.key | gpg --dearmor | sudo tee /etc/apt/keyrings/confluent.gpg > /dev/null; echo -e "deb [arch=amd64 signed-by=/etc/apt/keyrings/confluent.gpg] https://packages.confluent.io/deb/7.4 stable main\ndeb [signed-by=/etc/apt/keyrings/confluent.gpg] https://packages.confluent.io/clients/deb focal main" | sudo tee /etc/apt/sources.list.d/confluent.list; sudo apt update; sudo apt install -y confluent-community-2.13 default-jre; sudo apt clean
</syntaxhighlight>
</syntaxhighlight>


 這 邊只安裝社群版 本:
== ZooKeeper模式或是KRaft模式 ==
 
  現在 個時間點比較尷尬,得看需求背景取捨。很粗略的來說,如果你是既有的應用要使用[[Kafka]],用ZooKeeper模式會比較保險;如果你是自己開發的軟體要使用Kafka,用KRaft模式有機會減少將來的技術債。
 
=== KRaft ===
 
{{Main|Confluent/KRaft}}
 
KRaft模式是官方要取代ZooKeeper模式所提出的方案。
 
=== ZooKeeper ===
 
{{Main|Confluent/ZooKeeper}}
{{Main|Confluent/Kafka}}
 
ZooKeeper模式比較成熟,但官方已經宣布有計畫要淘汰掉ZooKeeper模式了。
 
== 基 操作 ==
 
依照模式的不同會有不同的參數設定:
 
* ZooKeeper模式的操作會有指定任何一台ZooKeeper服務,像是<code>--zookeeper lb-zookeeper.example.com</code>這樣的參數。
* KRaft模式的操作則是指定任何一台Broker服務,像是<code>--bootstrap-server lb-broker.example.com</code>這樣的參數。
 
=== topic ===
 
topic的操作都是透過<code>kafka-topics</code>處理,使用<code>--zookeeper</code>指定ZooKeeper位置(可省略Port資訊,預設使用2181)。
 
建立是<code>--create</code>


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
wget -qO - https://packages.confluent.io/deb/5.1/archive.key | sudo apt-key add -
kafka-topics --zookeeper internal-test-kafka-zookeeper-lb-123456789.ap-southeast-1.elb.amazonaws.com --create --topic test --replication-factor 3 --partitions 2
sudo add-apt-repository "deb [arch=amd64] https://packages.confluent.io/deb/5.1 stable main"
sudo apt install -y confluent-community-2.11
</syntaxhighlight>
</syntaxhighlight>


== 設定ZooKeeper ==
  觀看是透過<code>--list</code>(簡易)或是<code>--describe</code>(詳細):


修改<code>/etc/kafka/zookeeper.properties</code> ,其中IP address需要填寫對應的位置:
<syntaxhighlight lang="bash">
kafka-topics --zookeeper internal-test-kafka-zookeeper-lb-123456789.ap-southeast-1.elb.amazonaws.com --list
</syntaxhighlight>


<syntaxhighlight lang="ini">
<syntaxhighlight lang="bash">
#
kafka-topics --zookeeper internal-test-kafka-zookeeper-lb-123456789.ap-southeast-1.elb.amazonaws.com --describe
tickTime=2000
</syntaxhighlight>
dataDir=/var/lib/zookeeper/
 
clientPort=2181
刪除topic則是透過<code>--delete</code>:
initLimit=5
 
syncLimit=2
<syntaxhighlight lang="bash">
server.1=1.2.3.4:2888:3888
kafka-topics --zookeeper internal-test-kafka-zookeeper-lb-123456789.ap-southeast-1.elb.amazonaws.com --delete --topic test
server.2=5.6.7.8:2888:3888
</syntaxhighlight>
server.3=9.10.11.12:2888:3888
 
autopurge.snapRetainCount=3
預設值只會標記(<code>MarkedForDeletion</code>),而非實際刪除:
autopurge.purgeInterval=24
 
<pre>
Topic test is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.
</pre>
 
=== 訊息 ===
 
把目前的日期資訊傳到<code>test</code>裡:
 
<syntaxhighlight lang="bash">
date | kafka-console-producer --zookeeper internal-test-kafka-zookeeper-lb-123456789.ap-southeast-1.elb.amazonaws.com --topic test
</syntaxhighlight>
 
接收訊息,並且從頭開始收(<code>--from-beginning</code>):
 
<syntaxhighlight lang="bash">
kafka-console-consumer --zookeeper internal-test-kafka-zookeeper-lb-123456789.ap-southeast-1.elb.amazonaws.com --topic test --from-beginning --property print.timestamp=true
</syntaxhighlight>
 
新版的會使用Broker的點而非ZooKeeper的點,這時候會使用<code>--bootstrap_server</code>加上Broker的位置,而非<code>--zookeeper</code>加上ZooKeeper的位置,像是這樣:
 
<syntaxhighlight lang="bash">
kafka-console-consumer --bootstrap_server internal-test-kafka-broker-lb-123456789.ap-southeast-1.elb.amazonaws.com:9092 --topic test --from-beginning --property print.timestamp=true
</syntaxhighlight>
</syntaxhighlight>


  新增<code>/var/lib/zookeeper/myid</code>,每一台都需要不同,<code>1</code>或<code>2</code>或<code>3</code>:
== 額外設定Schema Registry ==
 
修改<code>/etc/schema-registry/schema-registry.properties</code>:


<syntaxhighlight lang="ini">
<syntaxhighlight lang="ini">
1
kafkastore.bootstrap.servers=PLAINTEXT://kafka-broker.srv.example.net:9092
metadata.encoder.secret=x
host.name=kafka-mishmash-1.private.example.net
</syntaxhighlight>
</syntaxhighlight>


  然後修改檔案擁有人
  啟動Schema Registry


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
sudo chown cp-kafka:confluent /var/lib/zookeeper/myid
sudo systemctl enable --now confluent-schema-registry
sudo service confluent-schema-registry status
</syntaxhighlight>
</syntaxhighlight>


  目前的ZooKeeper(Confluent 2.11版內的ZooKeeper) 預設 值是使用512 MB的記憶體,但主機有7.5 GB的記憶體 ,所以 會想要讓ZooKeeper可以用7 GB,因此需要 改ZooKeeper的JVM參數。這邊需要新增<code>/lib/systemd/system/confluent-zookeeper.service.d/30-options.conf</code> (目錄可能需要自己建立)
== 額外設定Kafka Connect ==
 
系統 預設 會跑distributed mode ,所以修 <code>/etc/kafka/connect-distributed.properties</code>:


<syntaxhighlight lang="ini">
<syntaxhighlight lang="ini">
[Service]
bootstrap.servers=kafka-broker.srv.example.net:9092
Environment=KAFKA_HEAP_OPTS="-Xmx7g -Xms7g"
</syntaxhighlight>
</syntaxhighlight>


  理論上就可以啟
  自己建立三個需要的topic,因為自 建立的會有問題(因為需要<code>--config cleanup.policy=compact</code>),這邊的<code>--replication-factor</code>與<code>--partitions</code>都是用Kafka Connect的預設值


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
sudo systemctl daemon-reload
kafka-topics --bootstrap-server kafka-broker.srv.example.net:9092 --topic connect-configs --create --replication-factor 3 --partitions 1 --config cleanup.policy=compact
sudo service confluent-zookeeper start
kafka-topics --bootstrap-server kafka-broker.srv.example.net:9092 --topic connect-offsets --create --replication-factor 3 --partitions 25 --config cleanup.policy=compact
sudo service confluent-zookeeper status
kafka-topics --bootstrap-server kafka-broker.srv.example.net:9092 --topic connect-status --create --replication-factor 3 --partitions 5 --config cleanup.policy=compact
</syntaxhighlight>
</syntaxhighlight>


  輸出 的資訊判斷系統狀態
  另外Kafka Connect因為預設值的關係,log只能在<code>/var/log/syslog</code>裡面看到,所 另外修改<code>/etc/kafka/connect-log4j.properties</code>讓他 輸出 到<code>/var/log/kafka/connect.log</code>


<syntaxhighlight lang="bash">
<syntaxhighlight lang="ini">
echo stat | nc 127.0.0.1 2181
log4j.rootLogger=INFO, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/var/log/kafka/connect.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%d] %p %m (%c:%L)%n
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=5
log4j.appender.file.append=true
</syntaxhighlight>
</syntaxhighlight>


  或是直接透過指令操作測試
  但還要修改目錄權限讓Kafka Connect可以寫進去


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
zookeeper-shell 127.0.0.1:2181
sudo chmod g+w /var/log/kafka
</syntaxhighlight>
</syntaxhighlight>


  接下來可以將TCP Port 2181建立對應的TCP Load Balancer(像是用ELB)。
  啟動Kafka Connect:


=== 認證 ===
<syntaxhighlight lang="bash">
sudo systemctl enable --now confluent-kafka-connect
sudo service confluent-kafka-connect status
</syntaxhighlight>


  如果需要讓ZooKeeper啟用認證,需要先建立對應的帳號與密碼字串(這個例子裡面是<code>admin</code>與<code>password</code>)
  理論上就可以看到有安裝的plugin資訊了


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
java -cp "$(echo /usr/share/java/kafka/* | sed 's/ /:/g')" org.apache.zookeeper.server.auth.DigestAuthenticationProvider admin:password
curl -s -v http://10.1.2.3:8083/connector-plugins | jq .
</syntaxhighlight>
</syntaxhighlight>


  會產生像是這樣 輸出,其中 面的那串值是
  可以把plugin安裝到預設 <code>/usr/share/java</code>下 後重 新啟動,像是MongoDB的plugin<ref>{{Cite web |url=https://www.mongodb.com/docs/kafka-connector/current/introduction/install/ |title=Install the MongoDB Kafka Connector |language=en |accessdate=2023-09-20}}</ref>


<syntaxhighlight lang="text">
<syntaxhighlight lang="bash">
admin:password->admin:bjkZ9W+M82HUZ9xb8/Oy4cmJGfg=
cd /usr/share/java; sudo wget -c https://repo1.maven.org/maven2/org/mongodb/kafka/mongo-kafka-connect/1.11.0/mongo-kafka-connect-1.11.0-all.jar; sudo service confluent-kafka-connect restart
</syntaxhighlight>
</syntaxhighlight>


== 設定Kafka ==
  然後建立一個JSON檔案
 
如果[[ZooKeeper]]與[[Kafka]]不同伺服器,需要修改<code>/etc/kafka/server.properties</code>的<code>zookeeper.connect</code>的值,像是這樣


<syntaxhighlight lang="ini">
<syntaxhighlight lang="json">
#zookeeper.connect=localhost:2181
{
zookeeper.connect=internal-test-gslin-confluent-xxxxxxxxx.us-east-1.elb.amazonaws.com:2181
   "name": "source-mongoatlas-dev",
   "config": {
     "connector.class": "com.mongodb.kafka.connect.MongoSourceConnector",
     "tasks.max": "1",
     "connection.uri": "mongodb+srv://readonly:x@foo.bar.mongodb.net",
     "topic.prefix": "mongoatlas_dev"
   }
}
</syntaxhighlight>
</syntaxhighlight>


  修改<code>/etc/kafka/server.properties</code>的<code>broker.id</code> 設定,讓他自動產生而不需要自己指定
  這邊使用的<code>mongodb+srv://</code> 可以先驗證


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
#broker.id=0
dig _mongodb._tcp.foo.bar.mongodb.net srv
broker.id.generation.enable=true
</syntaxhighlight>
</syntaxhighlight>


  理論上 可以啟動了
  接著 對著任何一台Kafka Connect的API打


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
sudo service confluent-confluent start
curl -X POST -H "Content-Type: application/json" --data @a.json http://10.1.2.3:8083/connectors
sudo service confluent-confluent status
</syntaxhighlight>
</syntaxhighlight>


== 設定其他套件 ==
  後續就可以看到
 
啟動Schema Registry


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
sudo service confluent-schema-registry start
curl -s -v http://10.1.2.3:8083/connectors | jq .
sudo service confluent-schema-registry status
</syntaxhighlight>
</syntaxhighlight>


  啟動Kafka Connect
  另外如果有MongoDB有動靜,就會同步到Kafka上面,會自動建立topic(以<code>mongoatlas_dev</code>開頭)


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
sudo service confluent-kafka-connect start
kafka-topics --bootstrap-server kafka-broker.srv.example.net:9092 --list
sudo service confluent-kafka-connect status
</syntaxhighlight>
</syntaxhighlight>
== 設定其他套件 ==


 啟動Kafka REST Proxy:
 啟動Kafka REST Proxy:
行 149: 行 219:
sudo service confluent-ksql start
sudo service confluent-ksql start
sudo service confluent-ksql status
sudo service confluent-ksql status
</syntaxhighlight>
== 測試 ==
新版幾乎都是使用<code>--bootstrap-server</code>指定一台活著的broker位置當作起點,像是簡單列出topics:
<syntaxhighlight lang="bash">
kafka-topics --bootstrap-server kafka-broker.srv.example.net:9092 --list
</syntaxhighlight>
或是詳細列出:
<syntaxhighlight lang="bash">
kafka-topics --bootstrap-server kafka-broker.srv.example.net:9092 --describe
</syntaxhighlight>
</syntaxhighlight>


行 155: 行 239:
* [[Apache Kafka]]
* [[Apache Kafka]]
* [[Apache ZooKeeper]]
* [[Apache ZooKeeper]]
== 參考資料 ==
{{Reflist|2}}


== 外部連結 ==
== 外部連結 ==

於 2023年10月23日 (一) 15:48 的最新修訂

Confluent是一家公司,同時也是該公司Apache Kafka產品線的產品名。

簡介

Confluent是Apache Kafka發明人出來開的公司,也是目前最知名的Kafka商業支援服務。同時Confluent也是該公司推出的軟體品牌,提供了眾多的Open Source套件用以管理Kafka Cluster(即社群版本元件),另外提供商用版本,包括Control Center(提供三十天試用)。

硬體

在PoC時我使用了三台t3.small(2 GB RAM,另外手動設定加上512 MB Swap),三台都安裝完整的套件並且跑起來,一開始不會有問題,但跑一陣子後會因為記憶體不足而異常。

建議在正式環境下,ZooKeeper與Kafka分開跑,都使用m5a.large(8 GB RAM)或是更好的機器執行。

安裝

當然要先裝Java,然後安裝社群版本,這邊因為官方還沒有提供jammy版本(Ubuntu 22.04),這邊手動寫成focal

wget -qO - https://packages.confluent.io/deb/7.4/archive.key | gpg --dearmor | sudo tee /etc/apt/keyrings/confluent.gpg > /dev/null; echo -e "deb [arch=amd64 signed-by=/etc/apt/keyrings/confluent.gpg] https://packages.confluent.io/deb/7.4 stable main\ndeb [signed-by=/etc/apt/keyrings/confluent.gpg] https://packages.confluent.io/clients/deb focal main" | sudo tee /etc/apt/sources.list.d/confluent.list; sudo apt update; sudo apt install -y confluent-community-2.13 default-jre; sudo apt clean

ZooKeeper模式或是KRaft模式

現在這個時間點比較尷尬,得看需求背景取捨。很粗略的來說,如果你是既有的應用要使用Kafka,用ZooKeeper模式會比較保險;如果你是自己開發的軟體要使用Kafka,用KRaft模式有機會減少將來的技術債。

KRaft

KRaft模式是官方要取代ZooKeeper模式所提出的方案。

ZooKeeper

ZooKeeper模式比較成熟,但官方已經宣布有計畫要淘汰掉ZooKeeper模式了。

基本操作

依照模式的不同會有不同的參數設定:

  • ZooKeeper模式的操作會有指定任何一台ZooKeeper服務,像是--zookeeper lb-zookeeper.example.com這樣的參數。
  • KRaft模式的操作則是指定任何一台Broker服務,像是--bootstrap-server lb-broker.example.com這樣的參數。

topic

topic的操作都是透過kafka-topics處理,使用--zookeeper指定ZooKeeper位置(可省略Port資訊,預設使用2181)。

建立是--create

kafka-topics --zookeeper internal-test-kafka-zookeeper-lb-123456789.ap-southeast-1.elb.amazonaws.com --create --topic test --replication-factor 3 --partitions 2

觀看是透過--list(簡易)或是--describe(詳細):

kafka-topics --zookeeper internal-test-kafka-zookeeper-lb-123456789.ap-southeast-1.elb.amazonaws.com --list
kafka-topics --zookeeper internal-test-kafka-zookeeper-lb-123456789.ap-southeast-1.elb.amazonaws.com --describe

刪除topic則是透過--delete

kafka-topics --zookeeper internal-test-kafka-zookeeper-lb-123456789.ap-southeast-1.elb.amazonaws.com --delete --topic test

預設值只會標記(MarkedForDeletion),而非實際刪除:

Topic test is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.

訊息

把目前的日期資訊傳到test裡:

date | kafka-console-producer --zookeeper internal-test-kafka-zookeeper-lb-123456789.ap-southeast-1.elb.amazonaws.com --topic test

接收訊息,並且從頭開始收(--from-beginning):

kafka-console-consumer --zookeeper internal-test-kafka-zookeeper-lb-123456789.ap-southeast-1.elb.amazonaws.com --topic test --from-beginning --property print.timestamp=true

新版的會使用Broker的點而非ZooKeeper的點,這時候會使用--bootstrap_server加上Broker的位置,而非--zookeeper加上ZooKeeper的位置,像是這樣:

kafka-console-consumer --bootstrap_server internal-test-kafka-broker-lb-123456789.ap-southeast-1.elb.amazonaws.com:9092 --topic test --from-beginning --property print.timestamp=true

額外設定Schema Registry

修改/etc/schema-registry/schema-registry.properties

kafkastore.bootstrap.servers=PLAINTEXT://kafka-broker.srv.example.net:9092
metadata.encoder.secret=x
host.name=kafka-mishmash-1.private.example.net

啟動Schema Registry:

sudo systemctl enable --now confluent-schema-registry
sudo service confluent-schema-registry status

額外設定Kafka Connect

系統預設會跑distributed mode,所以修改/etc/kafka/connect-distributed.properties

bootstrap.servers=kafka-broker.srv.example.net:9092

自己建立三個需要的topic,因為自動建立的會有問題(因為需要--config cleanup.policy=compact),這邊的--replication-factor--partitions都是用Kafka Connect的預設值:

kafka-topics --bootstrap-server kafka-broker.srv.example.net:9092 --topic connect-configs --create --replication-factor 3 --partitions 1 --config cleanup.policy=compact
kafka-topics --bootstrap-server kafka-broker.srv.example.net:9092 --topic connect-offsets --create --replication-factor 3 --partitions 25 --config cleanup.policy=compact
kafka-topics --bootstrap-server kafka-broker.srv.example.net:9092 --topic connect-status --create --replication-factor 3 --partitions 5 --config cleanup.policy=compact

另外Kafka Connect因為預設值的關係,log只能在/var/log/syslog裡面看到,所以另外修改/etc/kafka/connect-log4j.properties讓他輸出到/var/log/kafka/connect.log

log4j.rootLogger=INFO, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/var/log/kafka/connect.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%d] %p %m (%c:%L)%n
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=5
log4j.appender.file.append=true

但還要修改目錄權限讓Kafka Connect可以寫進去:

sudo chmod g+w /var/log/kafka

啟動Kafka Connect:

sudo systemctl enable --now confluent-kafka-connect
sudo service confluent-kafka-connect status

理論上就可以看到有安裝的plugin資訊了:

curl -s -v http://10.1.2.3:8083/connector-plugins | jq .

可以把plugin安裝到預設的/usr/share/java下後重新啟動,像是MongoDB的plugin[1]

cd /usr/share/java; sudo wget -c https://repo1.maven.org/maven2/org/mongodb/kafka/mongo-kafka-connect/1.11.0/mongo-kafka-connect-1.11.0-all.jar; sudo service confluent-kafka-connect restart

然後建立一個JSON檔案:

{
    "name": "source-mongoatlas-dev",
    "config": {
        "connector.class": "com.mongodb.kafka.connect.MongoSourceConnector",
        "tasks.max": "1",
        "connection.uri": "mongodb+srv://readonly:x@foo.bar.mongodb.net",
        "topic.prefix": "mongoatlas_dev"
    }
}

這邊使用的mongodb+srv://可以先驗證:

dig _mongodb._tcp.foo.bar.mongodb.net srv

接著就對著任何一台Kafka Connect的API打:

curl -X POST -H "Content-Type: application/json" --data @a.json http://10.1.2.3:8083/connectors

後續就可以看到:

curl -s -v http://10.1.2.3:8083/connectors | jq .

另外如果有MongoDB有動靜,就會同步到Kafka上面,會自動建立topic(以mongoatlas_dev開頭):

kafka-topics --bootstrap-server kafka-broker.srv.example.net:9092 --list

設定其他套件

啟動Kafka REST Proxy:

sudo service confluent-kafka-rest start
sudo service confluent-kafka-rest status

啟動KSQL:

sudo service confluent-ksql start
sudo service confluent-ksql status

測試

新版幾乎都是使用--bootstrap-server指定一台活著的broker位置當作起點,像是簡單列出topics:

kafka-topics --bootstrap-server kafka-broker.srv.example.net:9092 --list

或是詳細列出:

kafka-topics --bootstrap-server kafka-broker.srv.example.net:9092 --describe

相關連結

參考資料

  1. Install the MongoDB Kafka Connector. [2023-09-20] (English). 

外部連結