DevBoi

[Kafka] Kafka Zookeeper,Broker 세팅 및 실행 본문

Develop/[Kafka]

[Kafka] Kafka Zookeeper,Broker 세팅 및 실행

HiSmith 2023. 7. 27. 21:12
반응형

1. 카프카 바이너리 파일 다운로드

https://kafka.apache.org/downloads

 

Apache Kafka

Apache Kafka: A Distributed Streaming Platform.

kafka.apache.org

 

2.디렉토리 구조확인

kafka 2.8버전을 받았다. 2.12는 스칼라 버전이다.

bin은 바이너리나 쉘 스크립트가 포함되어있다.

config에는 설정에 필요한 여러 설정파일들이 

libs 브로커를 실행할떄 필요한 라이브러리가 있다.

해당 폴더 내에 추가로 data 디렉토리를 생성한다. 이는 데이터를 적재하기 위함이다.

 

3. 내용 살펴보기

config/server.properties는 브로커를 실행할때 필요한 설정 들이다.

 

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# see kafka.server.KafkaConfig for additional details and defaults

############################# Server Basics #############################

# The id of the broker. This must be set to a unique integer for each broker.
broker.id=0

############################# Socket Server Settings #############################

# The address the socket server listens on. It will get the value returned from 
# java.net.InetAddress.getCanonicalHostName() if not configured.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
#listeners=PLAINTEXT://:9092

# Hostname and port the broker will advertise to producers and consumers. If not set, 
# it uses the value for "listeners" if configured.  Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
#advertised.listeners=PLAINTEXT://your.host.name:9092

# Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details
#listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL

# The number of threads that the server uses for receiving requests from the network and sending responses to the network
num.network.threads=3

# The number of threads that the server uses for processing requests, which may include disk I/O
num.io.threads=8

# The send buffer (SO_SNDBUF) used by the socket server
socket.send.buffer.bytes=102400

# The receive buffer (SO_RCVBUF) used by the socket server
socket.receive.buffer.bytes=102400

# The maximum size of a request that the socket server will accept (protection against OOM)
socket.request.max.bytes=104857600


############################# Log Basics #############################

# A comma separated list of directories under which to store log files
log.dirs=/tmp/kafka-logs

# The default number of log partitions per topic. More partitions allow greater
# parallelism for consumption, but this will also result in more files across
# the brokers.
num.partitions=1

# The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
# This value is recommended to be increased for installations with data dirs located in RAID array.
num.recovery.threads.per.data.dir=1

############################# Internal Topic Settings  #############################
# The replication factor for the group metadata internal topics "__consumer_offsets" and "__transaction_state"
# For anything other than development testing, a value greater than 1 is recommended to ensure availability such as 3.
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1

############################# Log Flush Policy #############################

# Messages are immediately written to the filesystem but by default we only fsync() to sync
# the OS cache lazily. The following configurations control the flush of data to disk.
# There are a few important trade-offs here:
#    1. Durability: Unflushed data may be lost if you are not using replication.
#    2. Latency: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush.
#    3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to excessive seeks.
# The settings below allow one to configure the flush policy to flush data after a period of time or
# every N messages (or both). This can be done globally and overridden on a per-topic basis.

# The number of messages to accept before forcing a flush of data to disk
#log.flush.interval.messages=10000

# The maximum amount of time a message can sit in a log before we force a flush
#log.flush.interval.ms=1000

############################# Log Retention Policy #############################

# The following configurations control the disposal of log segments. The policy can
# be set to delete segments after a period of time, or after a given size has accumulated.
# A segment will be deleted whenever *either* of these criteria are met. Deletion always happens
# from the end of the log.

# The minimum age of a log file to be eligible for deletion due to age
log.retention.hours=168

# A size-based retention policy for logs. Segments are pruned from the log unless the remaining
# segments drop below log.retention.bytes. Functions independently of log.retention.hours.
#log.retention.bytes=1073741824

# The maximum size of a log segment file. When this size is reached a new log segment will be created.
log.segment.bytes=1073741824

# The interval at which log segments are checked to see if they can be deleted according
# to the retention policies
log.retention.check.interval.ms=300000

############################# Zookeeper #############################

# Zookeeper connection string (see zookeeper docs for details).
# This is a comma separated host:port pairs, each corresponding to a zk
# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
# You can also append an optional chroot string to the urls to specify the
# root directory for all kafka znodes.
zookeeper.connect=localhost:2181

# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=18000


############################# Group Coordinator Settings #############################

# The following configuration specifies the time, in milliseconds, that the GroupCoordinator will delay the initial consumer rebalance.
# The rebalance will be further delayed by the value of group.initial.rebalance.delay.ms as new members join the group, up to a maximum of max.poll.interval.ms.
# The default value for this is 3 seconds.
# We override this to 0 here as it makes for a better out-of-the-box experience for development and testing.
# However, in production environments the default value of 3 seconds is more suitable as this will help to avoid unnecessary, and potentially expensive, rebalances during application startup.
group.initial.rebalance.delay.ms=0

 

 

4. 조각조각으로 보기

broker.id=0

브로커 아이디이다. 같은 클러스터라면, 브로커 아이디는 고유한 아이디로

하나씩 증가한다.

 

num.network.threads=3

# The number of threads that the server uses for processing requests, which may include disk I/O
num.io.threads=8

# The send buffer (SO_SNDBUF) used by the socket server
socket.send.buffer.bytes=102400

# The receive buffer (SO_RCVBUF) used by the socket server
socket.receive.buffer.bytes=102400

# The maximum size of a request that the socket server will accept (protection against OOM)
socket.request.max.bytes=104857600

네트워크 관련 설정 값이다. 이건 뭐 따로 설명할 필요가 없다.

 

log.dirs=/tmp/kafka-logs

카프카는 파일 시스템을 이용해서 로그를 저장하는데, 이때 저장되는 경로를 지정할 수있다.

log.retention.hours=168

# A size-based retention policy for logs. Segments are pruned from the log unless the remaining
# segments drop below log.retention.bytes. Functions independently of log.retention.hours.
#log.retention.bytes=1073741824

# The maximum size of a log segment file. When this size is reached a new log segment will be created.
log.segment.bytes=1073741824

# The interval at which log segments are checked to see if they can be deleted according
# to the retention policies
log.retention.check.interval.ms=300000

로그에 대한 보관 주기, 삭제 주기에 대한 설정이다.

해당 세그먼트 값을 설정하는것에 따라서 얼마나 로그를 유지하는 것인지 확인이 가능하다.

retention.hour을 보면, 168시간이 지난 뒤에는 삭제를 하는 것으로 알 수 있다.

 

zookeeper.connect=localhost:2181

# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=18000

주키퍼에 대한 설정 값이다.

 

 

주키퍼 띄우기

카프카를 실행하기 위해서는 주키퍼를 실행해야한다(3버전 이하이기 때문에)
앙상블로 따로 진행 하는 것이 일반적이지만 로컬이니까 주키퍼를 따로 실행할 수있다.

 

브로커 띄우기

 

카프카 정상 실행 여부 확인

카프카 정상 실행 여부를 확인할 수 있는 쉘 스크립트가 제공된다.

kafka-broker-api-versions.sh를 사용하면, 확인이 가능하다.

 

추가적으로 topics라는 쉘 스크립트로, 카프카 리스트를 가져올수 있다.

이 리스트를 통해서 해당 브로커에서 사용하는 토픽 리스트를 받아올수 있다.

 

참고로, 카프카를 호스트에 추가하면 테스트 하기 편하다.

 

 

이제 실행을 위해 아래의 파일을 수정하자!

 

1. config/server.properties수정

 

리스너 두개 주석해제 및 호스트 localhost로 수정

이렇게 하면, 브로커의 위치를 알릴수있게 되고, 메타데이터를 받게 된다.

이는 카프카 클러스터가 브로커의 위치를 인식하기 위함이다.

 

추가로, 로그 저장 위치에 대한 설정도 진행해준다. 아래의 사진처럼 경로를 바꿔준다.

주키퍼를 실행해보자

아래의 명령어를 하면, 실행이 되고 로그가 뜬다.

 bin/zookeeper-server-start.sh config/zookeeper.properties

 

주키퍼 실행과 동시에 브로커도 실행

bin/kafka-server-start.sh config/server.properties

 

 

정상적으로 실행이 되었다.

 

이제 정상적으로 되었는지 확인해보자

 

bin/kafka-broker-api-versions.sh --bootstrap-server localhost:9092

 

정상적으로 실행됨을 보여준다.

 

토픽 리스트도 확인해보자

bin/kafka-topics.sh --bootstrap-server localhost:9092 --list

 

아직 아무 토픽도 입력하지 않아서, 해당 빈 리스트가 노출된다.

무튼 잘 실행되고 있는 것을 확인했다.

 

 

마지막으로 호스트 파일을 수정해보자

/etc/hosts에서 my-kafka를 추가해준다.

반응형