Kafka集群搭建(简版)
Kafka集群利用Docker搭建,主要组成部分:zookeeper+Kafak
1、搭建zookeeper集群
需要注意端口是否重复,0.0.0.0可以改为自己相应的服务IP地址
需要安装 docker-compose 插件
以下是docker-compose.yml文件内容需要,使用方式:
docker-compose up -d # 创建并后台运行所有容器
version: '3.1' services: zoo1: image: zookeeper restart: always hostname: zoo1 ports: - 2181:2181 environment: ZOO_MY_ID: 1 ZOO_SERVERS: server.1=0.0.0.0:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181 zoo2: image: zookeeper restart: always hostname: zoo2 ports: - 2182:2181 environment: ZOO_MY_ID: 2 ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=0.0.0.0:2888:3888;2181 server.3=zoo3:2888:3888;2181 zoo3: image: zookeeper restart: always hostname: zoo3 ports: - 2183:2181 environment: ZOO_MY_ID: 3 ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=0.0.0.0:2888:3888;2181
2、搭建Kafka集群
需要一个个执行,x.x.x.x需要改为自己的宿主机IP
docker run -d --name kafka -p 9092:9092 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=x.x.x.x:2181,x.x.x.x:2182,x.x.x.x:2183 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://x.x.x.x:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -t wurstmeister/kafka docker run -d --name kafka1 -p 9093:9093 -e KAFKA_BROKER_ID=1 -e KAFKA_ZOOKEEPER_CONNECT=x.x.x.x:2181,x.x.x.x:2182,x.x.x.x:2183 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://x.x.x.x:9093 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9093 -t wurstmeister/kafka docker run -d --name kafka2 -p 9094:9094 -e KAFKA_BROKER_ID=2 -e KAFKA_ZOOKEEPER_CONNECT=x.x.x.x:2181,x.x.x.x:2182,x.x.x.x:2183 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://x.x.x.x:9094 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9094 -t wurstmeister/kafka
3、测试Kafka集群
利用Kafka提供的命令测试通路 Kafka命令行
#查看topic bin/kafka-topics.sh --zookeeper x.x.x.x:2181 --list #创建topic bin/kafka-topics.sh --zookeeper x.x.x.x:2181 --create --replication-factor 2 --partitions 2 --topic topic_test #发送消息 bin/kafka-console-producer.sh --broker-list x.x.x.x:9092 --topic topic_test #消费消息 bin/kafka-console-consumer.sh --bootstrap-server x.x.x.x:9092 --from-beginning --topic topic_test
4、安装Kafka管理页面
docker run -itd --name=kafka-manager -p 9000:9000 -e ZK_HOSTS="x.x.x.x:2181,x.x.x.x:2182,x.x.x.x:2183" sheepkiller/kafka-manager
引用文章:
https://www.cnblogs.com/jay763190097/p/10292227.html
https://segmentfault.com/a/1190000011446369
https://www.cnblogs.com/bystander/p/docker-an-zhuangkafkakafkamanager-ji-qun.html