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: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_kafka_1 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_kafka_1 bash
cd /opt/kafka_2.11-0.11.0.3/bin/
./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
执行完上述命令后,在生产者窗口中 输入任意内容回车,即可在消费者的窗口查看到消息