docker快速部署kafka


docker查看并创建本地公共网络

# 根据需要选择命令执行,如果 deploy 网络已创建,可忽略
docker network ls
docker network inspect deploy
docker netwok rm deploy
docker network create --driver=bridge --subnet=172.10.0.0/16 deploy

通过docker-compose部署管理应用

# docker-compose配置文件 docker-compose.yaml
cat << EOF> docker-compose-kafka.yaml
version: '3'
services:
  zookeeper:
    image: zookeeper:${ZOOKEEPER_VERSION:-3.6.2}
    container_name: flink-zookeeper
    hostname: zookeeper
    networks:
      deploy:
        ipv4_address: 172.10.0.8
        aliases:
          - zookeeper
    # extra_hosts:
    #   - "jobmanager:172.10.0.2"
    #   - "taskmanager:172.10.0.3"
    ports:
      - 2181:2181
       
  kafka:
    image: wurstmeister/kafka:${KAFKA_VERSION:-2.13-2.6.0}
    container_name: flink-kafka
    hostname: kafka
    networks:
      deploy:
        ipv4_address: 172.10.0.9
        aliases:
          - kafka
    ports:
      - 9092:9092
    environment:
      KAFKA_BROKER_ID: 0
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://127.0.0.1:9092
      KAFKA_CREATE_TOPICS: "test.base:2:1"   #kafka启动后初始化一个有2个partition(分区)1个副本名叫metric的topic 
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
    depends_on:
      - zookeeper

networks:
  deploy:
    external: true
EOF

# docker-compose管理
docker-compose -f docker-compose-kafka.yaml up -d # 启动
docker-compose -f docker-compose-kafka.yaml down -v # 停止并移除实例

kafka 基础命令

# 创建topic
bin/kafka-topics.sh --create \
  --zookeeper 127.0.0.1:2181 \
  --replication-factor 1 \
  --partitions 2 \
  --topic metric

# topic生产者
bin/kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic metric --property parse.key=false

# topic消费者
bin/kafka-console-consumer.sh --bootstrap-server 127.0.0.1:9092 --topic metric --from-beginning

# 查看topic信息
bin/kafka-topics.sh --describe --zookeeper 127.0.0.1:2181 --topic metric

# 删除topic
bin/kafka-topics  --delete --zookeeper 127.0.0.1:2181 --topic metric

# 查看group
bin/kafka-consumer-groups.sh --bootstrap-server 127.0.0.1:9092 --list

更多docker应用,请参考我的Github:
https://github.com/darebeat/docker-center/tree/master/deploy


文章作者: darebeat
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 darebeat !
  目录