# Docker-compose file for Kafka

로컬 개발 환경에서 kafka 실습을 위한 docker compose 파일 입니다. 보통의 경우 docker의 internal host로 설정하여 직접 접속이 어려운 점이 있었는데 localhost로 직접 호출 할 수 있도록 작성하였습니다.

* 작업 순서
  - 아래의 파일을 복사하여 docker-compose.yml 파일을 생성합니다.
  - docker-compose up -d  명령어를 실행합니다.
  - http://localhost:10000 으로 접속하면 실행 내용을 보실 수 있습니다.


```yml
version: '3.8'
services:
  zookeeper1:
    container_name: zookeeper1
    image: confluentinc/cp-zookeeper:latest
    environment:
      ZOOKEEPER_SERVER_ID: 1
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
      ZOOKEEPER_INIT_LIMIT: 10
      ZOOKEEPER_SYNC_LIMIT: 5
      ZOOKEEPER_SERVERS: "zookeeper1:2888:3888;zookeeper2:2888:3888;zookeeper3:2888:3888"
    ports:
      - "2181:2181"

  zookeeper2:
    container_name: zookeeper2
    image: confluentinc/cp-zookeeper:latest
    environment:
      ZOOKEEPER_SERVER_ID: 2
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
      ZOOKEEPER_INIT_LIMIT: 10
      ZOOKEEPER_SYNC_LIMIT: 5
      ZOOKEEPER_SERVERS: "zookeeper1:2888:3888;zookeeper2:2888:3888;zookeeper3:2888:3888"
    ports:
      - "2182:2181"

  zookeeper3:
    container_name: zookeeper3
    image: confluentinc/cp-zookeeper:latest
    environment:
      ZOOKEEPER_SERVER_ID: 3
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
      ZOOKEEPER_INIT_LIMIT: 10
      ZOOKEEPER_SYNC_LIMIT: 5
      ZOOKEEPER_SERVERS: "zookeeper1:2888:3888;zookeeper2:2888:3888;zookeeper3:2888:3888"
    ports:
      - "2183:2181"

  kafka1:
    container_name: kafka1
    image: confluentinc/cp-kafka:latest
    depends_on:
      - zookeeper1
      - zookeeper2
      - zookeeper3
    ports:
      - "9092:9092"
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: "zookeeper1:2181,zookeeper2:2181,zookeeper3:2181"
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
      KAFKA_ADVERTISED_LISTENERS: INSIDE://kafka1:9093,OUTSIDE://localhost:9092
      KAFKA_LISTENERS: INSIDE://0.0.0.0:9093,OUTSIDE://0.0.0.0:9092
      KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE

  kafka2:
    container_name: kafka2
    image: confluentinc/cp-kafka:latest
    depends_on:
      - zookeeper1
      - zookeeper2
      - zookeeper3
    ports:
      - "9093:9093"
    environment:
      KAFKA_BROKER_ID: 2
      KAFKA_ZOOKEEPER_CONNECT: "zookeeper1:2181,zookeeper2:2181,zookeeper3:2181"
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
      KAFKA_ADVERTISED_LISTENERS: INSIDE://kafka2:9094,OUTSIDE://localhost:9093
      KAFKA_LISTENERS: INSIDE://0.0.0.0:9094,OUTSIDE://0.0.0.0:9093
      KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE

  kafka3:
    container_name: kafka3
    image: confluentinc/cp-kafka:latest
    depends_on:
      - zookeeper1
      - zookeeper2
      - zookeeper3
    ports:
      - "9094:9094"
    environment:
      KAFKA_BROKER_ID: 3
      KAFKA_ZOOKEEPER_CONNECT: "zookeeper1:2181,zookeeper2:2181,zookeeper3:2181"
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
      KAFKA_ADVERTISED_LISTENERS: INSIDE://kafka3:9095,OUTSIDE://localhost:9094
      KAFKA_LISTENERS: INSIDE://0.0.0.0:9095,OUTSIDE://0.0.0.0:9094
      KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE

  kafka-connect:
    container_name: kafka-connect
    image: debezium/connect:latest
    depends_on:
      - kafka1
      - kafka2
      - kafka3
    environment:
      BOOTSTRAP_SERVERS: kafka1:9093,kafka1:9094,kafka1:9095
      GROUP_ID: "1"
      CONFIG_STORAGE_TOPIC: "connect-configs"
      OFFSET_STORAGE_TOPIC: "connect-offsets"
      STATUS_STORAGE_TOPIC: "connect-statuses"
    ports:
      - "8083:8083"

  mysql:
    image: mysql:8.0.31
    environment:
      MYSQL_ROOT_PASSWORD: 1q2w3e
      MYSQL_DATABASE: kafka
    ports:
      - "3306:3306"
    command: --default-authentication-plugin=mysql_native_password

  mysql-initializer:
    image: mysql:8.0.31
    depends_on:
      - mysql
    environment:
      MYSQL_ROOT_PASSWORD: 1q2w3e
    command: >
      bash -c "
      sleep 20;
      mysql -h mysql -uroot -p1q2w3e -e \"
        CREATE USER 'debezium'@'%' IDENTIFIED BY 'dbz';
        GRANT ALL PRIVILEGES ON *.* TO 'debezium'@'%';
        FLUSH PRIVILEGES;
        USE kafka;
        CREATE TABLE users (
            id INT AUTO_INCREMENT PRIMARY KEY,
            name VARCHAR(255) NOT NULL,
            email VARCHAR(255) NOT NULL UNIQUE
        );
        INSERT INTO users (name, email) VALUES ('John Doe', 'john.doe@example.com');
        INSERT INTO users (name, email) VALUES ('Jane Doe', 'jane.doe@example.com');
      \"
      "

  kafka-ui:
    image: provectuslabs/kafka-ui
    container_name: kafka-ui
    ports:
      - "10000:8080"
    restart: always
    environment:
      - KAFKA_CLUSTERS_0_NAME=local
      - KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS=kafka1:9093,kafka1:9094,kafka1:9095
      - KAFKA_CLUSTERS_0_ZOOKEEPER=zookeeper1:2181,zookeeper2:2182,zookeeper3:2183
```

