提醒
在hub.docker.com网站上,Star最多的kafka镜像是wurstmeister/kafka,今天一起来实践这个镜像,使用此镜像搭建kafka环境,并且生产和消费消息;
https://developer.aliyun.com/article/988818
实战环境
操作系统:CentOS7
Docker:24.0.2
docker-compose:v2.18.1
编写docker-compose.yml
version:"3"# 通用配置x-common-config:&common-configKAFKA_ZOOKEEPER_CONNECT: zookeeper:2181KAFKA_NUM_PARTITIONS:3KAFKA_DEFAULT_REPLICATION_FACTOR:2# kafka镜像通用配置x-kafka:&kafkaimage: wurstmeister/kafka
networks:net:depends_on:- zookeeper
services:zookeeper:container_name: zookeeper
image: wurstmeister/zookeeper
ports:-"2181:2181"environment:- ALLOW_ANONYMOUS_LOGIN=yes
networks:- net
volumes:- zookeeper_data:/wurstmeister/zookeeper
kafka-0:container_name: kafka-0<<:*kafkaports:-"9093:9093"environment:<<:*common-configKAFKA_BROKER_ID:0KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://114.115.166.169:9093KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9093volumes:- kafka_0_data:/wurstmeister/kafka
kafka-1:container_name: kafka-1<<:*kafkaports:-"9094:9094"environment:<<:*common-configKAFKA_BROKER_ID:1KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://114.115.166.169:9094KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9094volumes:- kafka_1_data:/wurstmeister/kafka
kafka-2:container_name: kafka-2<<:*kafkaports:-"9095:9095"environment:<<:*common-configKAFKA_BROKER_ID:2KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://114.115.166.169:9095KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9095volumes:- kafka_2_data:/wurstmeister/kafka
volumes:zookeeper_data:kafka_0_data:kafka_1_data:kafka_2_data:networks:net:
启动server
在docker-compose.yml所在的文件夹下,执行命令
docker compose up -d
,会先下载zookeeper和kafka的镜像,然后创建容器;
执行命令
docker compose ps
,可见启动了一个zookeeper和三个kafka容器:
[root@hecs-334217 kafka-compose]# docker compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
kafka-0 wurstmeister/kafka "start-kafka.sh" kafka-0 28 minutes ago Up 28 minutes 0.0.0.0:9093->9093/tcp, :::9093->9093/tcp
kafka-1 wurstmeister/kafka "start-kafka.sh" kafka-1 28 minutes ago Up 28 minutes 0.0.0.0:9094->9094/tcp, :::9094->9094/tcp
kafka-2 wurstmeister/kafka "start-kafka.sh" kafka-2 28 minutes ago Up 28 minutes 0.0.0.0:9095->9095/tcp, :::9095->9095/tcp
zookeeper wurstmeister/zookeeper "/bin/sh -c '/usr/sb…" zookeeper 28 minutes ago Up 28 minutes 22/tcp, 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, :::2181->2181/tcp
查看版本号
执行以下命令查看容器中的kafka版本号:
[root@hecs-334217 kafka-compose]# docker exec kafka-0 find / -name \*kafka_\* | head -1 |grep -o '\kafka[^\n]*'
kafka_2.13-2.8.1
上述命令执行后,我这边控制台显示kafka_2.11-0.11.0.3,这表示Scala 的版本为2.11,kafka的版本是0.11.0.3;
执行以下命令查看zookeeper版本:
[root@hecs-334217 kafka-compose]# docker exec zookeeper pwd
/opt/zookeeper-3.4.13
创建topic
创建一个topic,名为topic001,3个partition,副本因子2,执行以下命令即可:
[root@hecs-334217 kafka-compose]# docker exec kafka-1 kafka-topics.sh --create --topic topic001 --partitions 3 --zookeeper zookeeper:2181 --replication-factor 2
Created topic topic001.
执行以下命令查看刚刚创建的topic,这次在容器kafka-2上执行命令试试:
[root@hecs-334217 kafka-compose]# docker exec kafka-2 kafka-topics.sh --list --zookeeper zookeeper:2181
test
topic001
可见刚刚创建的topic可以被查到;
查看刚刚创建的topic的情况,borker和副本情况一目了然,如下:
[root@hecs-334217 kafka-compose]# docker exec kafka-0 kafka-topics.sh --describe --topic topic001 --zookeeper zookeeper:2181
Topic: topic001 TopicId: umQtyLTtQQCeMG8W5bWcpA PartitionCount: 3 ReplicationFactor: 2 Configs:
Topic: topic001 Partition: 0 Leader: 0 Replicas: 0,1 Isr: 0,1
Topic: topic001 Partition: 1 Leader: 1 Replicas: 1,2 Isr: 1,2
Topic: topic001 Partition: 2 Leader: 2 Replicas: 2,0 Isr: 2,0
消费消息
执行如下命令,即可进入等待topic为topic001消息的状态:
docker exec kafka-2 \
kafka-console-consumer.sh \
--topic topic001 \
--bootstrap-server kafka-0:9093,kafka-1:9094,kafka-2:9095
目前还没有生产消息,因此控制台不会有内容输出,接下来尝试生产消息;
生产消息
打开一个新的窗口,执行如下命令,进入生产消息的命令行模式,注意不要漏掉参数"-it"
docker exec -it kafka-1 \
kafka-console-producer.sh \
--topic topic001 \
--broker-list kafka-0:9093,kafka-1:9094,kafka-2:9095
现在已经进入了生产消息的命令行模式,输入一些字符串然后回车,再去消费消息的控制台窗口看看,已经有消息打印出来,说明消息的生产和消费都成功了。
docker compose 命令
# 启动:
$ docker compose up -d # docker-compose up -d 这种是旧版的,新版本已经抛弃了# 查看compose容器
$ docker compose ps# 删除容器:
$ docker compose down
Kafka 可视化工具(Kafka Tool)
版权归原作者 范欣德 所有, 如有侵权,请联系我们删除。