การสร้างและจัดการคลัสเตอร์ Kafka

Apache Kafka เบื้องต้น

Mike Metzger

Data Engineering Consultant

ZooKeeper คืออะไร?

  • ZooKeeper คือเฟรมเวิร์กสำหรับจัดการข้อมูลและให้บริการที่จำเป็นสำหรับระบบกระจาย
  • ใช้งานหลักโดยนักพัฒนาเพื่อสร้างแอปพลิเคชันแบบกระจาย
    • ผู้ใช้งานโต้ตอบกับ ZooKeeper
  • ตัวอย่างแอปพลิเคชัน
    • Kafka
    • Hadoop
    • Neo4j

Apache_ZooKeeper_logo.svg.png

Apache Kafka เบื้องต้น

ZooKeeper ทำอะไรได้บ้าง?

  • ZooKeeper ให้บริการที่จำเป็นสำหรับการรันแอปพลิเคชันแบบกระจาย
    • จัดการการกำหนดค่า
    • การตั้งชื่อระบบ
    • การซิงโครไนซ์ระหว่างระบบ
    • บริการที่ใช้ร่วมกันระหว่างกลุ่มระบบ
  • ออกแบบเป็นเฟรมเวิร์กเพื่อป้องกันไม่ให้แต่ละแอปพลิเคชันสร้างบริการของตัวเองซ้ำซ้อน
    • เหมือนตัวเชื่อมต่อมาตรฐาน เช่น ปลั๊กไฟหรือหัวต่อสายยาง
Apache Kafka เบื้องต้น

ZooKeeper และ Kafka

  • Kafka ใช้ ZooKeeper สำหรับจัดการคลัสเตอร์
    • Kafka เวอร์ชันใหม่สามารถใช้ KRaft ได้
  • ไฟล์ที่ใช้งาน
    • config/zookeeper.properties
    • config/server.properties
# zookeeper.properties

# The directory where the 
# snapshot is stored.
dataDir=/tmp/zookeeper

# The port at which the clients 
# will connect
clientPort=2181

...
Apache Kafka เบื้องต้น

config/server.properties

  • จัดการการกำหนดค่าเฉพาะของ Kafka
    • รายละเอียดโบรกเกอร์
    • การกำหนดค่าเครือข่าย
    • ตำแหน่งเก็บ event
    • การกำหนดค่าหัวข้อพื้นฐาน รวมถึงการจำลองข้อมูล
...
# A comma separated list of directories under which to store log files
log.dirs=/tmp/kafka-logs
# The default number of log partitions per topic.
num.partitions=1
Apache Kafka เบื้องต้น

การเริ่มต้นคลัสเตอร์ Kafka

  • คลัสเตอร์ Kafka เริ่มต้นใช้งานใน 2 ขั้นตอน
$ bin/zookeeper-server-start.sh config/zookeeper.properties
...
INFO   ______                  _                                           (org.apache.zookeeper.server.ZooKeeperServer)
INFO  |___  /                 | |                                          (org.apache.zookeeper.server.ZooKeeperServer)
INFO     / /    ___     ___   | | __   ___    ___   _ __     ___   _ __    (org.apache.zookeeper.server.ZooKeeperServer)
INFO    / /    / _ \   / _ \  | |/ /  / _ \  / _ \ | '_ \   / _ \ | '__| (org.apache.zookeeper.server.ZooKeeperServer)
INFO   / /__  | (_) | | (_) | |   <  |  __/ |  __/ | |_) | |  __/ | |     (org.apache.zookeeper.server.ZooKeeperServer)
INFO  /_____|  \___/   \___/  |_|\_\  \___|  \___| | .__/   \___| |_| (org.apache.zookeeper.server.ZooKeeperServer)
INFO                                               | |                      (org.apache.zookeeper.server.ZooKeeperServer)
INFO                                               |_|                      (org.apache.zookeeper.server.ZooKeeperServer)
INFO  (org.apache.zookeeper.server.ZooKeeperServer)
...
Apache Kafka เบื้องต้น

การเริ่มต้นคลัสเตอร์ Kafka (ต่อ)

$ bin/kafka-server-start.sh config/server.properties
INFO Awaiting socket connections on 0.0.0.0:9092. (kafka.network.DataPlaneAcceptor)
INFO Kafka version: 3.7.0 (org.apache.kafka.common.utils.AppInfoParser)
INFO Kafka commitId: 2ae524ed625438c5 (org.apache.kafka.common.utils.AppInfoParser)
INFO Kafka startTimeMs: 1717502877829 (org.apache.kafka.common.utils.AppInfoParser)
INFO [KafkaServer id=0] started (kafka.server.KafkaServer)
INFO [zk-broker-0-to-controller-forwarding-channel-manager]: Recorded new controller, 
     from now on will use node 815f25786085:9092 (id: 0 ra
Apache Kafka เบื้องต้น

การหยุดคลัสเตอร์ Kafka

  • bin/kafka-server-stop.sh
  • bin/zookeeper-server-stop.sh
  • สังเกตว่าลำดับการปิดระบบเป็นแบบย้อนกลับ
Apache Kafka เบื้องต้น

มาฝึกกันเถอะ!

Apache Kafka เบื้องต้น

Preparing Video For Download...