下載Kafka和ZooKeeper的鏡像
docker pull wurstmeister/kafka
docker pull wurstmeister/zookeeper
為構(gòu)建集群方便李滴,創(chuàng)建docker虛擬網(wǎng)絡(luò)
docker network create --driver bridge --subnet=172.18.12.0/16 --gateway=172.18.1.1 dockernet
創(chuàng)建kafka.yml文件
version: '2'
services:
?zoo1:
???image: wurstmeister/zookeeper
???restart: unless-stopped
???hostname: zoo1
???ports:
?????- "2181:2181"
???container_name: zookeeper
???networks:
?????dockernet:
???????ipv4_address: 172.18.12.5
?kafka1:
???image: wurstmeister/kafka
???ports:
?????- "9092:9092"
???environment:
?????KAFKA_ADVERTISED_HOST_NAME: 172.18.12.6
?????KAFKA_ADVERTISED_PORT: 9092
?????KAFKA_ZOOKEEPER_CONNECT: "172.18.12.5:2181"
?????KAFKA_BROKER_ID: 1
?????KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
?????KAFKA_CREATE_TOPICS: "mendix.log.in:2:1"
???depends_on:
?????- zoo1
???container_name: kafka1
???networks:
?????dockernet:
???????ipv4_address: 172.18.12.6
?kafka2:
???image: wurstmeister/kafka
???ports:
?????- "9093:9092"
???environment:
?????KAFKA_ADVERTISED_HOST_NAME: 172.18.12.7
?????KAFKA_ADVERTISED_PORT: 9092
?????KAFKA_ZOOKEEPER_CONNECT: "172.18.12.5:2181"
?????KAFKA_BROKER_ID: 2
?????KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
???depends_on:
?????- zoo1
???container_name: kafka2
???networks:
?????dockernet:
???????ipv4_address: 172.18.12.7
networks:
?dockernet:
???external:
????? name: dockernet
環(huán)境變量說明:
KAFKA_ADVERTISED_HOST_NAME:?
zookeeper利用這個hostname在集群中的broker之間同步消息, 這里配置為kafka在docker虛擬網(wǎng)絡(luò)中的IP强缘。 使用你的宿主機(jī)的IP也可以勒叠,但是端口號要改成宿主機(jī)的端口號
KAFKA_ADVERTISED_PORT:?
Kafka在Docker container中的端口號
備注
如果你需要在Docker虛擬網(wǎng)絡(luò)之外使用Kafka集群识颊,你需要把這兩個參數(shù)配置成你宿主機(jī)的IP填帽,端口號要改成Docker映射到宿主機(jī)的端口號(9092, 9093)瞳脓。當(dāng)往Kafka其中的一個broker發(fā)送消息時悔政,Kafka集群從ZooKeeper取得Broker IP和端口號,然后同步數(shù)據(jù)桥言,使用虛擬網(wǎng)絡(luò)IP會導(dǎo)致發(fā)送和接收消息失敗萌踱,因為宿主機(jī)無法訪問Docker虛擬網(wǎng)絡(luò)內(nèi)的節(jié)點
?例如我的宿主機(jī)IP是,192.168号阿,0.2并鸵,
上述2個Kafka容器,9092端口號分別映射到宿主機(jī)的9092和9093端口扔涧。
環(huán)境變量配置如下
Kafka1配置為
? ? ? KAFKA_ADVERTISED_HOST_NAME: 192.168.0.2
? ? ? KAFKA_ADVERTISED_PORT: 9092
Kafka2配置為
? ? ? KAFKA_ADVERTISED_HOST_NAME: 192.168.0.2
? ? ? KAFKA_ADVERTISED_PORT: 9093
啟動Dockers
docker-compose -f kafka.yml up -d
測試
進(jìn)入容器zookeeper
docker exec -it zookeeper /bin/bash
檢查是否已經(jīng)成功建立topic
root@zoo1:/# cd $ZK_HOME/bin
root@zoo1:/opt/zookeeper-3.4.13/bin# ./zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /brokers/topics
[__consumer_offsets, test, mendix.log.in]
測試Kafka消息發(fā)送
進(jìn)入容器kafka2园担, 利用kafka-console-producer.sh向broker2發(fā)送消息
>docker exec -it kafka2 /bin/bash
bash-4.4# cd /opt/kafka/bin
bash-4.4# kafka-console-producer.sh --broker-list localhost:9092 --topic mendix.log.in
>test1
>test2
>test 3
>
測試Kafka消息接收
進(jìn)入容器kafka1届谈, 利用kafka-console-producer.sh從broker1接收消息
docker exec -it kafka1 /bin/bash
cd /opt/kafka/bin
bash-4.4# kafka-console-consumer.sh --bootstrap-server localhost:9092 -topic mendix.log.in
test1
test2
test 3