docker-compose搭建kafka单机集群环境

1.配置网络(复用已有网络)

1.1 创建docker虚拟网络

# 创建一个名为clickhouse的桥接(bridge)虚拟网络,网关172.26.0.1,网段为172.25.0.0
docker network create -d bridge --subnet=172.26.0.0/16 --gateway=172.26.0.1 clickhouse

2.启停命令

在docker-compose.yml路径下:

构建开启: docker-compose up -d

停止: docker-compose stop

开启:docker-compose start

2.编写docker-compose.yml

version: '3.7'
services:
  zoo1:
    image: wurstmeister/zookeeper
    #restart: always
    hostname: zoo1
    ports:
      - "12181:2181"
    container_name: kafka-zookeeper
    volumes:
     - /opt/DockerData/kafka-zookeeper:/opt/zookeeper-3.4.13/data
     - /opt/DockerData/kafka-zookeeper/myid:/opt/zookeeper-3.4.13/data/myid
    networks:
      clickhouse:
        ipv4_address: 172.26.0.106
  kafka1:
    image: wurstmeister/kafka
    #restart: always
    hostname: kafka1
    ports:
      - "9092:9092"
    container_name: kafka1
    environment:
      KAFKA_ADVERTISED_HOST_NAME: 192.168.0.102
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.0.102:9092
      KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181"
      KAFKA_ADVERTISED_PORT: 9092
      KAFKA_BROKER_ID: 1
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
    volumes:
      - /opt/DockerData/kafka1/logs:/kafka
    depends_on:
      - zoo1
    networks:
      clickhouse:
        ipv4_address: 172.26.0.107
  kafka2:
    image: wurstmeister/kafka
    #restart: always
    hostname: kafka2
    ports:
      - "9093:9092"
    container_name: kafka2
    environment:
      KAFKA_ADVERTISED_HOST_NAME: 192.168.0.102
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.0.102:9093
      KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181"
      KAFKA_ADVERTISED_PORT: 9093
      KAFKA_BROKER_ID: 2
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
    volumes:
      - /opt/DockerData/kafka2/logs:/kafka
    depends_on:
      - zoo1
    networks:
      clickhouse:
        ipv4_address: 172.26.0.108
  kafka3:
    image: wurstmeister/kafka
    #restart: always
    hostname: kafka3
    ports:
      - "9094:9092"
    container_name: kafka3
    environment:
      KAFKA_ADVERTISED_HOST_NAME: 192.168.0.102
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.0.102:9094
      KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181"
      KAFKA_ADVERTISED_PORT: 9094
      KAFKA_BROKER_ID: 3
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
    volumes:
      - /opt/DockerData/kafka3/logs:/kafka
    depends_on:
      - zoo1
    networks:
      clickhouse:
        ipv4_address: 172.26.0.109
##  kafka-manager:
##    image: hlebalbau/kafka-manager              ## 镜像:开源的web管理kafka集群的界面
##    environment:
##        ZK_HOSTS: 172.26.0.106
##    ports:
##      - "19000:9000"                               ## 暴露端口
##    networks:
##      clickhouse:
##        ipv4_address: 172.26.0.110

networks:
 clickhouse:
  external: true

客户端

  1. kafka-tool软件
  2. 命令行
  3. kafka-manager界面(新版的连zk有问题)

你可能感兴趣的:(docker-compose搭建kafka单机集群环境)