본문 바로가기

IT

docker compose로 kafka 설치 및 접속 방법

반응형
#kafka-compose.yml 파일
version: "3"
services:
  zookeeper:
    image: confluentinc/cp-zookeeper:latest
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
  kafka:
    image: confluentinc/cp-kafka:latest
    depends_on:
      - zookeeper
    ports:
      - 9092:9092
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1

 

 

#docker 이미지 다운로드 및 실행
$docker-compose -f kafka-compse.yml -p "project-name" up
$docker-compose -f ./kafka-compse.yaml ps
$docker-compose -f .\kafka-compose.yml exec kafka bash

#토픽 목록 확인
$kafka-topics --list --bootstrap-server localhost:9092

#토픽 생성
$kafka-topics --create --topic my-topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1

#새로운 터미널 열어서 접속
$docker-compose -f .\kafka-compose.yml exec kafka bash
#토픽 구독
$kafka-console-consumer --topic my-topic --bootstrap-server localhost:9092



#원래 기존에 열었던 터미널에서 메세지 전송
$kafka-console-producer --topic my-topic --bootstrap-server localhost:9092
>Hello, Kafka?