一、创建虚拟网络

# 创建网络
docker network create --driver bridge --subnet 172.30.0.0/16 --gateway 172.30.0.1 xiaoqiangzai
# 查看网络
docker network ls

二、Docker-compose

说明:确保虚拟网络创建没问题,这里在同一个虚拟网络环境下kafka与zookeeper才能相互连接,否则则需要将kafka连接zookeeper的地址修改成宿主机的地址,下面的配置需要修改KAFKA_ADVERTISED_LISTENERS成宿主机的IP地址。

version: '3.0'
services:
  zookeeper1:
    image: zookeeper:latest
    container_name: zookeeper1
    hostname: zookeeper1
    ports:
      - "2181:2181"
    restart: always
    privileged: true
    environment:
      ZOO_MY_ID: 1
      ZOO_SERVERS: server.1=zookeeper1:2888:3888;2181 server.2=zookeeper2:2888:3888;2181 server.3=zookeeper3:2888:3888;2181
    volumes:
      - ./data/zookeeper1/data:/data
      - ./data/zookeeper1/datalog:/datalog
    networks:
      default:
        ipv4_address: 172.30.0.11
  zookeeper2:
    image: zookeeper:latest
    container_name: zookeeper2
    hostname: zookeeper2
    ports:
      - "2182:2181"
    restart: always
    privileged: true
    environment:
      ZOO_MY_ID: 2
      ZOO_SERVERS: server.1=zookeeper1:2888:3888;2181 server.2=zookeeper2:2888:3888;2181 server.3=zookeeper3:2888:3888;2181
    volumes:
      - ./data/zookeeper2/data:/data
      - ./data/zookeeper2/datalog:/datalog
    networks:
      default:
        ipv4_address: 172.30.0.12
  zookeeper3:
    image: zookeeper:latest
    container_name: zookeeper3
    hostname: zookeeper3
    ports:
      - "2183:2181"
    restart: always
    privileged: true
    environment:
      ZOO_MY_ID: 3
      ZOO_SERVERS: server.1=zookeeper1:2888:3888;2181 server.2=zookeeper2:2888:3888;2181 server.3=zookeeper3:2888:3888;2181
    volumes:
      - ./data/zookeeper3/data:/data
      - ./data/zookeeper3/datalog:/datalog
    networks:
      default:
        ipv4_address: 172.30.0.13
  kafka1:
    image: wurstmeister/kafka:latest
    container_name: kafka1
    hostname: kafka1
    ports:
      - "9092:9092"
    restart: always
    privileged: true
    depends_on:
      - zookeeper1
      - zookeeper2
      - zookeeper3
    environment:
      KAFKA_HOST_NAME: kafka1
      KAFKA_ADVERTISED_HOST_NAME: kafka1
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.17.101:9092
      KAFKA_ZOOKEEPER_CONNECT: zookeeper1:2181,zookeeper2:2182,zookeeper3:2183
      KAFKA_ADVERTISED_PORT: 9092
      KAFKA_BROKER_ID: 1
    volumes:
      - ./data/kafka1:/kafka
      - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime
    networks:
      default:
        ipv4_address: 172.30.0.21
  kafka2:
    image: wurstmeister/kafka:latest
    container_name: kafka2
    hostname: kafka2
    ports:
      - "9093:9092"
    restart: always
    privileged: true
    depends_on:
      - zookeeper1
      - zookeeper2
      - zookeeper3
    environment:
      KAFKA_HOST_NAME: kafka2
      KAFKA_ADVERTISED_HOST_NAME: kafka2
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.17.101:9093
      KAFKA_ZOOKEEPER_CONNECT: zookeeper1:2181,zookeeper2:2182,zookeeper3:2183
      KAFKA_ADVERTISED_PORT: 9093
      KAFKA_BROKER_ID: 2
    volumes:
      - ./data/kafka2:/kafka
      - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime
    networks:
      default:
        ipv4_address: 172.30.0.22
  kafka3:
    image: wurstmeister/kafka:latest
    container_name: kafka3
    hostname: kafka3
    ports:
      - "9094:9092"
    restart: always
    privileged: true
    depends_on:
      - zookeeper1
      - zookeeper2
      - zookeeper3
    environment:
      KAFKA_HOST_NAME: kafka3
      KAFKA_ADVERTISED_HOST_NAME: kafka3
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.17.101:9094
      KAFKA_ZOOKEEPER_CONNECT: zookeeper1:2181,zookeeper2:2182,zookeeper3:2183
      KAFKA_ADVERTISED_PORT: 9094
      KAFKA_BROKER_ID: 3
    volumes:
      - ./data/kafka3:/kafka
      - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime
    networks:
      default:
        ipv4_address: 172.30.0.23
networks:
  default:
    external:
      name: xiaoqiangzai

三、验证是否成功

验证zookeeper集群

docker exec -it zookeeper1 bash
bin/zkServer.sh status
exit

docker exec -it zookeeper2 bash
bin/zkServer.sh status
exit

docker exec -it zookeeper3 bash
bin/zkServer.sh status
exit

image-20201222171314194

验证kafka集群

docker exec -it kafka1 bash
kafka-topics.sh --create --zookeeper zookeeper1:2181 --replication-factor 1 --partitions 3 --topic test1
kafka-topics.sh --list --zookeeper zookeeper1:2181
kafka-topics.sh --list --zookeeper zookeeper2:2181
kafka-topics.sh --list --zookeeper zookeeper3:2181

docker exec -it kafka2 bash
kafka-topics.sh --create --zookeeper zookeeper2:2181 --replication-factor 1 --partitions 3 --topic test2
kafka-topics.sh --list --zookeeper zookeeper1:2181
kafka-topics.sh --list --zookeeper zookeeper2:2181
kafka-topics.sh --list --zookeeper zookeeper3:2181

docker exec -it kafka3 bash
kafka-topics.sh --create --zookeeper zookeeper3:2181 --replication-factor 1 --partitions 3 --topic test3
kafka-topics.sh --list --zookeeper zookeeper1:2181
kafka-topics.sh --list --zookeeper zookeeper2:2181
kafka-topics.sh --list --zookeeper zookeeper3:2181

image-20201222172111420

image-20201222172221924

image-20201222172309134

外部连接

连接任一个zookeeper宿主机集群IP的地址会自动出现kafka集群节点

image-20201222172552127