1.搭建zookeeper docker pull wurstmeister/zookeeperdocker run -d --name zookeeper -p 2181:2181 --volume /etc/localtime:/etc/localtime -t wurstmeister/zoo ...
1.編寫docker-compose.yml
version: '2' services: zookeeper: image: wurstmeister/zookeeper ports: - "2181:2181" kafka: image: wurstmeister/kafka:2.11-0.11.0.3 ports: - "9092" environment: KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://:9092 KAFKA_LISTENERS: PLAINTEXT://:9092 KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 volumes: - /var/run/docker.sock:/var/run/docker.sock - /etc/localtime:/etc/localtime
2.啟動server
在docker-compose.yml所在的文件夾下,執行命令docker-compose up -d,會先下載zookeeper和kafka的鏡像,然後創建容器;
安裝docker-compose
wget https://github.com/docker/compose/releases/download/1.14.0-rc2/docker-compose-Linux-x86_64
rename docker-compose-Linux-x86_64 docker-compose docker-compose-Linux-x86_64
chmod +x /usr/local/bin/docker-compose
再通過docker-compose version命令進行查看
3.開啟埠
firewall-cmd --zone=public --permanent --add-port=2181/tcp
firewall-cmd --zone=public --permanent --add-port=9092/tcp
設置開啟啟動
docker update --restart=always kafka_zookeeper_1
docker update --restart=always kafka_kafka_1
3. 測試kafka
進入到kafka容器中 並創建topic 生產者,執行如下命令:
docker exec -it kafka bash
cd /opt/kafka_2.11-0.11.0.3/bin/
./kafka-topics.sh --create --zookeeper zookeeper:2181 --replication-factor 1 --partitions 8 --topic test
./kafka-console-producer.sh --broker-list localhost:9092 --topic test
執行上述命令後,另起一個標簽頁,執行如下命令 創建kafka消費者消費消息:
docker exec -it kafka bash
cd /opt/kafka_2.11-0.11.0.3/bin/
./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
執行完上述命令後,在生產者視窗中 輸入任意內容回車,即可在消費者的視窗查看到消息