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_ADVERTISED_HOST_NAME: 192.168.220.150
      KAFKA_CREATE_TOPICS: "test:1:1"
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    volumes:
      - /data/product/zj_bigdata/data/kafka/docker.sock:/var/run/docker.sock

在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 test

发布信息

bash-4.4# $KAFKA_HOME/bin/kafka-console-producer.sh --topic=test --broker-list kafka_kafka_1:9092
>ni
>haha


同样注意--broker-list后面的参数
接收消息

bash-4.4# $KAFKA_HOME/bin/kafka-console-consumer.sh --bootstrap-server kafka_kafka_1:9092 --from-beginning --topic test
ni
haha

五.kafka集群管理界面

docker run -itd --name=kafka-manager -p 9000:9000 -e ZK_HOSTS="192.168.220.150:2181" sheepkiller/kafka-manager

访问
http://192.168.220.150:9000/

链接:https://www.jianshu.com/p/0edcc3addf3f

posted on 2020-11-29 19:10  卖肾割阑尾  阅读(133)  评论(0编辑  收藏  举报

导航