1. 启动zookeeper容器
docker search zookeeper docker pull wurstmeister/zookeeper:3.4.13 #如果版本不存在就不加版本号 docker run -d --name zookeeper --restart=always -p 2181:2181 -t wurstmeister/zookeeper3.4.13
2. 启动kafka容器
docker search kafka docker pull wurstmeister/kafka:2.1.1 #如果版本不存在则去掉版本号 //docker run -d --name kafka --restart=always --publish 9092:9092 --link zookeeper --env KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 --env KAFKA_ADVERTISED_HOST_NAME=10.0.1.66 --env KAFKA_ADVERTISED_PORT=9092 --volume /etc/localtime:/etc/localtime wurstmeister/kafka:latest docker run -d --name kafka --restart=always --link zookeeper -p 9092:9092 --env KAFKA_ADVERTISED_HOST_NAME=10.0.1.66 --env KAFKA_ADVERTISED_PORT=9092 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://10.0.1.66:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 --volume /etc/localtime:/etc/localtime wurstmeister/kafka:2.1.1 docker run -d --name kafka --restart=always -p 9092:9092 --link zookeeper -e KAFKA_ADVERTISED_HOST_NAME="192.168.81.135" -e KAFKA_ADVERTISED_PORT=9092 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT="zookeeper:2181" -e KAFKA_ADVERTISED_LISTENERS="PLAINTEXT://192.168.81.135:9092" -e KAFKA_LISTENERS="PLAINTEXT://0.0.0.0:9092" -e TZ="Asia/Shanghai" wurstmeister/kafka:2.12-2.5.0
参数详解
KAFKA_ADVERTISED_HOST_NAME //Docker宿主机IP(如果你要配置多个brokers,就不能设置为 localhost 或 127.0.0.1) KAFKA_BROKER_ID=0 KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://10.0.1.66:9092 KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 //中间两个参数的172.16.90.109改为宿主机器的IP地址,如果不这么设置,可能会导致在别的机器上访问不到kafka。
3. 测试kafka(此步骤可有可无)
进入kafka容器的命令行
docker exec -ti kafka /bin/bash
进入kafka所在目录
cd /opt/kafka_2.12-1.1.0/ bin/kafka-topics.sh --create --zookeeper zookeeper:2181 --replication-factor 1 --partitions 1 --topic zipkin bin/kafka-topics.sh --describe --zookeeper zookeeper:2181 --topic zipkin //查看
安装zipkin(用于浏览器查看)
docker pull openzipkin/zipkin:2.12.6
docker run -it -d --restart=always -e KAFKA_BOOTSTRAP_SERVERS=10.0.1.66:9092 -e STORAGE_TYPE=elasticsearch -e ES_HOSTS=http://10.0.1.66:9200 -p 10086:9411 openzipkin/zipkin:2.12.6
浏览器访问zipkin地址:http://10.0.1.66:10086/zipkin
4. 集群搭建
使用docker命令可快速在同一台机器搭建多个kafka,只需要改变brokerId和端口
docker run -d --name kafka1 -p 9093:9093 -e KAFKA_BROKER_ID=1 -e KAFKA_ZOOKEEPER_CONNECT=172.16.90.109:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://172.16.90.109:9093 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9093 -t wurstmeister/kafka
5. 创建Replication为2,Partition为2的topic
在kafka容器中的opt/kafka_2.12-1.1.0/目录下输入
bin/kafka-topics.sh --create --zookeeper 172.16.90.109:2181 --replication-factor 2 --partitions 2 --topic partopic
6. 查看topic的状态
在kafka容器中的opt/kafka_2.12-1.1.0/目录下输入
bin/kafka-topics.sh --describe --zookeeper 172.16.90.109:2181 --topic partopic
输出结果:
Topic:partopic PartitionCount:2 ReplicationFactor:2 Configs:
Topic: partopic Partition: 0 Leader: 0 Replicas: 0,1 Isr: 0,1
Topic: partopic Partition: 1 Leader: 0 Replicas: 1,0 Isr: 0,1
显示每个分区的Leader机器为broker0,在broker0和1上具有备份,Isr代表存活的备份机器中存活的。
当停掉kafka1后,
docker stop kafka1
再查看topic状态,输出结果:
Topic:partopic PartitionCount:2 ReplicationFactor:2 Configs:
Topic: partopic Partition: 0 Leader: 0 Replicas: 0,1 Isr: 0
Topic: partopic Partition: 1 Leader: 0 Replicas: 1,0 Isr: 0
参考:https://blog.csdn.net/lblblblblzdx/article/details/80548294