docker部署kafka
参考:
一.镜像拉取
docker pull wurstmeister/zookeeper docker pull wurstmeister/kafka
二.定义docker-compose.yml
version: '3' services: zookeeper: image: wurstmeister/zookeeper ports: - "2181:2181" kafka: image: wurstmeister/kafka depends_on: [ zookeeper ] ports: - "9092:9092" environment: KAFKA_BROKER_NO: 1 KAFKA_ADVERTISED_HOST_NAME: 10.18.11.194 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://10.18.11.194:9092 KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 KAFKA_HEAP_OPTS: "-Xmx512M -Xms16M" volumes: - /Users/admin/Documents/kafka/docker.sock:/var/run/docker.sock
其中:KAFKA_ADVERTISED_HOST_NAME、KAFKA_ADVERTISED_LISTENERS中的ip要为宿主机的ip(ifconf查询)或127.0.0.1/localhost(只能本机访问)
在docker-compose.yml文件目录进行服务打包
[root@VM_0_16_centos kafka] # docker-compose build zookeeper uses an image, skipping kafka uses an image, skipping
三.启动服务
[root@VM_0_16_centos kafka]# docker-compose up -d Starting kafka_kafka_1 ... done Starting kafka_zookeeper_1 ... done
四.启动测试
记住启动的启动名称,kafka为 kafka_kafka_1 ,zookeeper 为 kafka_zookeeper_1 .
如果docker-compose正常启动,此时docker ps会看到以上两个容器。进入kafka容器
docker exec -it kafka_kafka_1 bash
创建一个topic
$KAFKA_HOME/bin/kafka-topics.sh --create --topic topic --partitions 4 --zookeeper kafka_zookeeper_1:2181 --replication-factor 1
注意–zookeeper后面的参数为,容器的name
查看刚刚创建的topic
$KAFKA_HOME/bin/kafka-topics.sh --zookeeper kafka_zookeeper_1:2181 --describe --topic topic
发布信息
$KAFKA_HOME/bin/kafka-console-producer.sh --topic=topic --broker-list kafka_kafka_1:9092 >ni >haha 查看某消费group的详细信息: $KAFKA_HOME/bin/kafka-consumer-groups.sh --bootstrap-server kafka_kafka_1:9092 --group adan_userinfo --describe
或
$KAFKA_HOME/bin/kafka-consumer-groups.sh --bootstrap-server 10.18.11.194:9092 --group adan_user_contact_increment --describe
同样注意--broker-list后面的参数
修改group的offset
kafka-consumer-groups.sh --bootstrap-server kafka_kafka_1:9092 --group new_g_adan_user_contact2 --topic new_adan_user_contact_increment2 --execute --reset-offsets --to-offset 0
接收消息
bash-4.4# $KAFKA_HOME/bin/kafka-console-consumer.sh --bootstrap-server kafka_kafka_1:9092 --from-beginning --topic topic ni haha
五.kafka集群管理界面
docker run -itd --name=kafka-manager -p 9000:9000 -e ZK_HOSTS="192.168.220.150:2181" shee
梦想随心,天地随行