docker-compose 搭建 kafka 叢集
阿新 • • 發佈:2021-06-23
環境準備
kafka依賴zookeeper,所以搭建kafka需要先配置zookeeper。網格資訊如下:
zookeeper | 192.168.56.101:2181 |
kafka1 | 192.168.56.101:9092 |
kafka2 | 192.168.56.101:9093 |
kafka3 | 192.168.56.101:9094 |
開始搭建
1、安裝 docker-compose
curl -L http://mirror.azure.cn/docker-toolbox/linux/compose/1.25.4/docker-compose-Linux-x86_64 -o /usr/local/bin/docker-composechmod +x /usr/local/bin/docker-compose
2、建立 docker-compose.yaml 檔案
version: '3.3' services: zookeeper: image: wurstmeister/zookeeper container_name: zookeeper ports: - 2181:2181 volumes: - ./data/zookeeper/data:/data - ./data/zookeeper/datalog:/datalog - ./data/zookeeper/logs:/logs restart: always kafka1: image: wurstmeisterView Code/kafka depends_on: - zookeeper container_name: kafka1 ports: - 9092:9092 environment: KAFKA_BROKER_ID: 1 KAFKA_ZOOKEEPER_CONNECT: 192.168.56.101:2181 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.56.101:9092 KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092 volumes:- /data/kafka1/data:/data - /data/kafka1/log:/datalog restart: unless-stopped kafka2: image: wurstmeister/kafka depends_on: - zookeeper container_name: kafka2 ports: - 9093:9093 environment: KAFKA_BROKER_ID: 2 KAFKA_ZOOKEEPER_CONNECT: 192.168.56.101:2181 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.56.101:9093 KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9093 volumes: - /data/kafka2/data:/data - /data/kafka2/log:/datalog restart: unless-stopped kafka3: image: wurstmeister/kafka depends_on: - zookeeper container_name: kafka3 ports: - 9094:9094 environment: KAFKA_BROKER_ID: 3 KAFKA_ZOOKEEPER_CONNECT: 192.168.56.101:2181 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.56.101:9094 KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9094 volumes: - /data/kafka3/data:/data - /data/kafka3/log:/datalog restart: unless-stopped
引數說明:
KAFKA_ZOOKEEPER_CONNECT: zk服務地址
KAFKA_ADVERTISED_LISTENERS: kafka服務地址
3、啟動
docker-compose up -d
4、檢視啟動的容器:docker-compose ps
出現如上結果表示啟動正常~
測試
1、登入到 kafka1 容器內
docker-compose exec kafka1 bash
2、建立一個 topic:名稱為first,3個分割槽,2個副本
./opt/kafka_2.13-2.7.0/bin/kafka-topics.sh --create --topic first --zookeeper 192.168.56.101:2181 --partitions 3 --replication-factor 2
注意:副本數不能超過brokers數(分割槽是可以超過的),否則會建立失敗。
3、檢視 topic 列表
./opt/kafka_2.13-2.7.0/bin/kafka-topics.sh --list --zookeeper 192.168.56.101:2181
4、檢視 topic 為 first 的詳情
./opt/kafka_2.13-2.7.0/bin/kafka-topics.sh --describe --topic first --zookeeper 192.168.56.101:2181
5、建立一個生產者,向 topic 中傳送訊息
./opt/kafka_2.13-2.7.0/bin/kafka-console-producer.sh --topic first --broker-list 192.168.56.101:9092
6、登入到 kafka2 或者 kafka3 容器內(參考第1步),然後建立一個消費者,接收 topic 中的訊息
./opt/kafka_2.13-2.7.0/bin/kafka-console-consumer.sh --topic first --bootstrap-server 192.168.56.101:9093 --from-beginning
注意:--from-beginning表示從最開始讀訊息,不加該引數則根據最大offset讀(從最新訊息開始讀取)