1.搭建zookeeper集群
docker-compose.yml文件
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
执行docker-compose up -d命令构建zookeeper集群
2.搭建kafka集群
2.1搭建3个kafka节点
节点1:$ docker run -d --name kafka -p 9092:9092 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=10.0.101.162:2181,10.0.101.162:2182,10.0.101.162:2183 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://10.0.101.162:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -t wurstmeister/kafka
节点2:$ docker run -d --name kafka1 -p 9093:9093 -e KAFKA_BROKER_ID=1 -e KAFKA_ZOOKEEPER_CONNECT=10.0.101.162:2181,10.0.101.162:2182,10.0.101.162:2183 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://10.0.101.162:9093 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9093 -t wurstmeister/kafka
节点3:$ docker run -d --name kafka2 -p 9094:9094 -e KAFKA_BROKER_ID=2 -e KAFKA_ZOOKEEPER_CONNECT=10.0.101.162:2181,10.0.101.162:2182,10.0.101.162:2183 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://10.0.101.162:9094 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9094 -t wurstmeister/kafka
其中10.0.101.162:2181为宿主机IP,查看宿主机方式为
ifconfig en()
3.测试kafka安装情况:
3.1进入其中一个kafka 节点容器中
docker exec -it kafka /bin/bash
3.2进入kafka所在的目录
cd /opt/kafka_2.13-2.6.0/
3.3创建一个topic·Replication为2·partition为2:
$ bin/kafka-topics.sh --create --zookeeper 10.0.101.162:2181,10.0.101.162:2182,10.0.101.162:2183 --replication-factor 2 --partitions 2 --topic testtopic Created topic testtopic.
3.4查看topic信息
$ bin/kafka-topics.sh --describe --zookeeper 0.0.101.162:2181,10.0.101.162:2182,10.0.101.162:2183 --topic testtopic Topic: testtopic PartitionCount: 2 ReplicationFactor: 2 Configs: Topic: testtopic Partition: 0 Leader: 2 Replicas: 2,1 Isr: 2,1 Topic: testtopic Partition: 1 Leader: 0 Replicas: 0,2 Isr: 0,2
3.5安装kafka-manager
$ docker run -itd --name=kafka-manager -p 9500:9000 -e ZK_HOSTS="10.0.101.162:2181,10.0.101.162:2182,10.0.101.162:2183" sheepkiller/kafka-manager
3.6容器启动之后访问localhost:9500访问kafka-manager 页面