【Kafka运维】部署基于Docker的Kraft模式Kafka集群
最近项目中想使用kafka做消息系统,但安装好像很复杂,现在把安装过程记录一下。
主流的安装方式是在服务器上直接安装,而我希望的是使用Docker。
主流的Kafka使用Zookeeper,但最近Kafka将会逐步舍弃Zookeeper而使用自带的KRaft
所以以下步骤是基于Docker安装无Zookeeper的Kafka。
服务器1:
服务器2:
服务器3:
将以下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
注:
分别"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.
>>> /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
>>> /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
经测试,以上方法在树莓派上不适用
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
通过上面方式部署成功后,如果想删除某个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又会出现,那是因为消费者生产者没有退出,将所有的生产者和消费者进程退出后再删除即可