【Kafka】基于Docker的KRaft模式(无Zookeeper)集群安装及测试

【Kafka运维】部署基于Docker的Kraft模式Kafka集群

最近项目中想使用kafka做消息系统,但安装好像很复杂,现在把安装过程记录一下。
主流的安装方式是在服务器上直接安装,而我希望的是使用Docker。
主流的Kafka使用Zookeeper,但最近Kafka将会逐步舍弃Zookeeper而使用自带的KRaft
所以以下步骤是基于Docker安装无Zookeeper的Kafka。

环境

服务器1:

  • 10.8.15.49
  • Docker: 20.10.20
  • docker-compose: 1.26.0
  • Kafka: 3.3.1

服务器2:

  • 10.11.206.4
  • Docker: 20.10.20
  • docker-compose: 1.26.0
  • Kafka: 3.3.1

服务器3:

  • 10.11.206.141
  • Docker: 20.10.20
  • docker-compose: 1.26.0
  • Kafka: 3.3.1

docker-compose.yml

将以下docker-compose.yml分别放在各服务器的任意位置
服务器1:

version: "3"
services:
   kafka:
     image: 'bitnami/kafka:latest'
     user: root
     environment:
       - KAFKA_ENABLE_KRAFT=yes
       - KAFKA_CFG_PROCESS_ROLES=broker,controller
       - KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
       - KAFKA_CFG_LISTENERS=PLAINTEXT://:9095,CONTROLLER://:9096
       - KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
       - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://10.8.15.49:9095
       - KAFKA_BROKER_ID=1
       - KAFKA_KRAFT_CLUSTER_ID=LelM2dIFQkiUFvXCEcqRWA
       - [email protected]:9096,[email protected]:9093,[email protected]:9093
       - ALLOW_PLAINTEXT_LISTENER=yes
     volumes:
       - /data/deploy/kafkaCluster/kraft:/bitnami/kafka:rw
     network_mode: host

服务器2:

version: "3"
services:
   kafka:
     image: 'bitnami/kafka:latest'
     user: root
     environment:
       - KAFKA_ENABLE_KRAFT=yes
       - KAFKA_CFG_PROCESS_ROLES=broker,controller
       - KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
       - KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093
       - KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
       - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://10.11.206.4:9092
       - KAFKA_BROKER_ID=3
       - KAFKA_KRAFT_CLUSTER_ID=LelM2dIFQkiUFvXCEcqRWA
       - [email protected]:9096,[email protected]:9093,[email protected]:9093
       - ALLOW_PLAINTEXT_LISTENER=yes
     volumes:
       - /data/deploy/kafkaCluster/kraft:/bitnami/kafka:rw
     network_mode: host

服务器3:

version: "3"
services:
   kafka:
     image: 'bitnami/kafka:latest'
     user: root
     environment:
       - KAFKA_ENABLE_KRAFT=yes
       - KAFKA_CFG_PROCESS_ROLES=broker,controller
       - KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
       - KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093
       - KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
       - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://10.11.206.141:9092
       - KAFKA_BROKER_ID=3
       - KAFKA_KRAFT_CLUSTER_ID=LelM2dIFQkiUFvXCEcqRWA
       - [email protected]:9096,[email protected]:9093,[email protected]:9093
       - ALLOW_PLAINTEXT_LISTENER=yes
     volumes:
       - /data/deploy/kafkaCluster/kraft:/bitnami/kafka:rw
     network_mode: host

注:

  1. 由于10.8.15.49服务器的9092和9093端口被占用了,故这里使用9095和9096端口
  2. 由于network_mode: host原因,docker-compose文件中没有指定ports

启动

分别"docker-compose up -d"

测试

进入容器中

>>> docker exec -it kafka_kafka_1 /bin/bash

创建

副本为3、分区为5的topic

>>> /opt/bitnami/kafka/bin/kafka-topics.sh --create --topic foo --partitions 5 --replication-factor 3 --bootstrap-server 10.8.15.49:9095,10.11.206.141:9092,10.11.206.4:9092
Created topic foo.

查看topic列表

>>> /opt/bitnami/kafka/bin/kafka-topics.sh --list --bootstrap-server 10.8.15.49:9095,10.11.206.141:9092,10.11.206.4:9092
foo

查看topic详情

>>> /opt/bitnami/kafka/bin/kafka-topics.sh --describe --topic foo --bootstrap-server 10.8.15.49:9095,10.11.206.141:9092,10.11.206.4:9092
Topic: foo	TopicId: TrI5QIXpTle-yl2FJy9vCA	PartitionCount: 5	ReplicationFactor: 3	Configs:
	Topic: foo	Partition: 0	Leader: 1	Replicas: 1,2,3	Isr: 1,2,3
	Topic: foo	Partition: 1	Leader: 2	Replicas: 2,3,1	Isr: 2,3,1
	Topic: foo	Partition: 2	Leader: 3	Replicas: 3,1,2	Isr: 3,1,2
	Topic: foo	Partition: 3	Leader: 1	Replicas: 1,3,2	Isr: 1,3,2
	Topic: foo	Partition: 4	Leader: 3	Replicas: 3,2,1	Isr: 3,2,1

消息生产消费

生产者

在任意节点的容器中执行如下命令

>>> /opt/bitnami/kafka/bin/kafka-console-producer.sh --broker-list 10.8.15.49:9095,10.11.206.141:9092,10.11.206.4:9092 --topic foo

消费者

在任意节点的容器中执行如下命令

>>> /opt/bitnami/kafka/bin/kafka-console-consumer.sh --bootstrap-server 10.8.15.49:9095,10.11.206.141:9092,10.11.206.4:9092 --topic foo

效果如下:
【Kafka】基于Docker的KRaft模式(无Zookeeper)集群安装及测试_第1张图片


经测试,以上方法在树莓派上不适用

Starting pi_kafka_1 ... done
Attaching to pi_kafka_1
kafka_1  | exec /opt/bitnami/scripts/kafka/entrypoint.sh: exec format error
pi_kafka_1 exited with code 1

参考:https://www.cnblogs.com/linjiangplus/p/16424137.html


2022-10-20更新

删除topic

通过上面方式部署成功后,如果想删除某个topic,使用如下命令

>>> /opt/bitnami/kafka/bin/kafka-topics.sh --delete --bootstrap-server 10.8.15.49:9095,10.11.206.141:9092,10.11.206.4:9092 --topic HelloWorld

会发现删除成功一段时间后,再通过如下命令查看topic列表时,该topic又会出现,那是因为消费者生产者没有退出,将所有的生产者和消费者进程退出后再删除即可

你可能感兴趣的:(后端,kafka,运维,kafka,docker,python,消息队列)