Tạo và quản lý cụm Kafka

Giới thiệu về Apache Kafka

Mike Metzger

Data Engineering Consultant

ZooKeeper là gì?

  • ZooKeeper là framework quản lý thông tin và cung cấp dịch vụ cần thiết để vận hành hệ phân tán
  • Chủ yếu dùng bởi nhà phát triển để tạo ứng dụng phân tán
    • Người dùng tương tác với ZooKeeper
  • Ví dụ ứng dụng
    • Kafka
    • Hadoop
    • Neo4j

Logo Apache ZooKeeper

Giới thiệu về Apache Kafka

ZooKeeper làm gì?

  • ZooKeeper cung cấp dịch vụ cần thiết để chạy ứng dụng phân tán
    • Xử lý cấu hình
    • Đặt tên hệ thống
    • Đồng bộ giữa các hệ
    • Dịch vụ cho một nhóm hệ thống
  • Thiết kế như một framework để tránh mỗi ứng dụng phân tán tự triển khai dịch vụ riêng.
    • Như một đầu nối chung, ví dụ phích cắm điện hoặc đầu vòi nước
Giới thiệu về Apache Kafka

ZooKeeper và Kafka

  • Kafka dùng ZooKeeper để quản lý cụm
    • Bản Kafka mới có thể dùng KRaft
  • Hai tệp được dùng
    • 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

...
Giới thiệu về Apache Kafka

config/server.properties

  • Xử lý cấu hình riêng của Kafka
    • Chi tiết broker
    • Cấu hình mạng
    • Vị trí lưu trữ sự kiện
    • Cấu hình chủ đề cơ bản, gồm sao lưu bản sao (replication)
...
# 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
Giới thiệu về Apache Kafka

Khởi động cụm Kafka

  • Cụm Kafka khởi động theo hai bước
$ 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)
...
Giới thiệu về Apache Kafka

Khởi động cụm Kafka (tiếp)

$ 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
Giới thiệu về Apache Kafka

Tắt cụm Kafka

  • bin/kafka-server-stop.sh
  • bin/zookeeper-server-stop.sh
  • Lưu ý thứ tự tắt ngược lại
Giới thiệu về Apache Kafka

Ayo berlatih!

Giới thiệu về Apache Kafka

Preparing Video For Download...