Kafka shell scripts

kafka-topics.sh : 토픽 생성, 조회, 수정 등 역할

  • --bootstrap-server : 토픽관련 명령어를 수행할 대상 카프카 클러스터
  • --replication-factor : 레플리카 개수 지정
  • --partitions : 파티션 개수 설정
  • --config : 각종 토픽 설정 가능(retention.ms, segment.byte 등)
  • --create : 토픽 생성
  • --delete : 토픽 제거
  • --describe: 토픽 상세 확인
  • --list : 카프카 클러스터의 토픽 리스트 확인
  • --version : 대상 카프카 클러스터 버전 확인

kafka-console-consumer.sh : 토픽의 레코드 즉시 조회

kafka-console-producer.sh : 토픽의 레코드를 전달

kafka-consumer-groups.sh : 컨슈머그룹 조회, 컨슈머 오프셋 확인, 수정

  • --bootstrap-server
  • --describe
  • --group

 

자주 사용하는 명령

토픽 생성

kafka-topics.sh --create 
    --zookeeper localhost:2181 
    --replication-factor 1 
    --partitions 1 
    --topic mytopic

 

토픽 리스트 조회

bash-4.4# kafka-topics.sh --list --zookeeper 192.168.0.1:2181
__consumer_offsets
micro-service-topic-0
micro-service-topic-1

 

토픽 정보 조회

bash-4.4# kafka-topics.sh --zookeeper 192.168.0.1:2181 --topic micro-service-0 --describe
Topic: micro-service-0	PartitionCount: 2	ReplicationFactor: 2	Configs: retention.ms=1000
	Topic: micro-service-0	Partition: 0	Leader: 1	Replicas: 1,2	Isr: 1,2
	Topic: micro-service-0	Partition: 1	Leader: 2	Replicas: 2,1	Isr: 1,2

 

Retention 변경

kafka-topics.sh --zookeeper 192.168.0.1:2181 --alter 
    --topic micro-service-0 
    --config retention.ms=1000

 

컨슈머 그룹 조회

bash-4.4# kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
micro-service-group

 

컨슈머 그룹 상세 조회

kafka-consumer-groups.sh --bootstrap-server localhost:9092 
   --group micro-service-group --describe
   
GROUP                      TOPIC              PARTITION  CURRENT-OFFSET  LOG-END-OFFSET LAG  CONSUMER-ID                            HOST            CLIENT-ID
micro-service-group        micro-service-0    2          334             334            0    consumer-micro-service-group-1-xxx     /192.168.0.3    consumer-micro-service-group-1

-. TOPIC: 토픽 이름
-. PARTITION: consumer group 내의 각 consumer가 할당된 파티션 번호
-. CURRENT-OFFSET: 현재 consumer group의 consumer가 각 파티션에서 마지막으로 offset을 commit한 값
-. LOG-END-OFFSET: producer쪽에서 마지막으로 생성한 레코드의 offset
-. LAG: LOG-END-OFFSET에서 CURRENT-OFFSET를 뺀 값

 

Replication factor 변경

변경전

bash-4.4# kafka-topics.sh --zookeeper 192.168.0.1:2181 --topic micro-service-0 --describe
Topic: micro-service-0	PartitionCount: 1	ReplicationFactor: 1	Configs: 
	Topic: micro-service-0	Partition: 0	Leader: 1	Replicas: 1	Isr: 1

변경후

// rf.json
{
   "version": 1,
   "partitions": [{
       "topic":"micro-service-0", "partition": 0, "replicas":[1,2]
    }]
}
bash-4.4# kafka-reassign-partitions.sh --zookeeper 192.168.0.1:2181 --reassignment-json-file rf.json --execute
Warning: --zookeeper is deprecated, and will be removed in a future version of Kafka.
Current partition replica assignment

{"version":1,"partitions":[{"topic":"micro-service-0","partition":0,"replicas":[1],"log_dirs":["any"]}]}

Save this to use as the --reassignment-json-file option during rollback
Successfully started partition reassignment for micro-service-0
bash-4.4# kafka-topics.sh --zookeeper 192.168.0.1:2181 --topic micro-service-0 --describe
Topic: micro-service-0	PartitionCount: 1	ReplicationFactor: 2	Configs: 
	Topic: micro-service-0	Partition: 0	Leader: 1	Replicas: 1,2	Isr: 1,2

 

파티션 개수 증가

bash-4.4# kafka-topics.sh --alter --topic micro-service-0 --partitions 4 --zookeeper 192.168.0.1:2181
WARNING: If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected
Adding partitions succeeded!
bash-4.4# kafka-topics.sh --zookeeper 192.168.0.1:2181 --topic micro-service-0 --describe
Topic: micro-service-0	PartitionCount: 4	ReplicationFactor: 2	Configs: 
	Topic: micro-service-0	Partition: 0	Leader: 1	Replicas: 1,2	Isr: 1,2
	Topic: micro-service-0	Partition: 1	Leader: 2	Replicas: 2,1	Isr: 2,1
	Topic: micro-service-0	Partition: 2	Leader: 1	Replicas: 1,2	Isr: 1,2
	Topic: micro-service-0	Partition: 3	Leader: 2	Replicas: 2,1	Isr: 2,1

 

'빅데이터 > Kafka Cluster' 카테고리의 다른 글

Kafka restart과 retention  (0) 2022.02.28
Kafka 스트레스 테스트  (0) 2022.02.28
Kafka Consumer/Producer  (0) 2022.02.28
Kafka Overview  (0) 2022.02.28
Kafka cluster failover  (0) 2022.02.28

Terminologies

A few terms are used in Kafka you need to know.

  • Message: A stream of bytes. For simplicity, assume it as a string.
  • Producer: The application or client that produces messages which will be consumed by other clients.
  • Consumer: A client who receives the messages produced by the producers.
  • Broker: Receives the messages, stores it, and decides the consumers who should get these messages.
  • Topic: A unique name through which the data is streamed.
  • Partition: A Partition is a virtual division which allows parallelizing a topic by splitting the data in a particular topic across multiple brokers. A topic can have multiple partitions. Minimum of 1.
  • Offset: A unique number for a message in a topic’s partition.
  • Consumer Group: A group of consumers.
  • Leader: The responsible unit for the topic’s partitions to store messages.
  • Replica: The mirror/sync of your data to handle the faults. So that the data doesn’t get lost.
  • In-Sync Replica(ISR) (Replication Group): The in-sync replicas are replicas that are alive and are in sync with the leader.

 

중요 포인트

  • 토픽으로 통하는 모든 데이터의 read/write는 오직 리더와 이루어진다.
  • ISR 내의 모든 follower들은 누구라도 리더가 될 수 있다.
  • 자신이 읽고 있는 파티션에는 같은 그룹내 다른 컨슈머가 읽을 수 없다.
  • 한번 늘린 파티션은 절대로 줄일 수 없기 때문에 운영중에 파티션을 늘리는 것은 충분히 고려해야한다.
  • 파티션에 순차적으로 메세지가 쓰여지지 않는다. 곧 컨슈머가 메세지를 순서대로 읽지 못할수있다.
  • 파티션 개수 >= 컨슈머 개수
  • 여러개의 컨슈머 그룹은 동일한 레코드를 받는다. 이때 각 컨슈머 그룹마다 별도로 오프셋 관리를 한다.

 

토픽, 리플리케이션 팩터, 카프카 클러스터 한눈에 정리

Topic Peter-Topic01 Peter-Topic02
파티션 수 2 2
레플리케이션 팩터 3 2
브로커 수 3 3

토픽의 파티션 리더의 위치를 유심히 보자
Kafka replication example
파티션 개수는 컨슈머 개수보다 크거나 같다

Offset 이란?

Partition 의 특정 Consumer 가 메세지를 읽어들인 위치를 나타냄. Consumer 가 메세지 수신 처리를 재개할 때 어떤 메세지 부터 가져 가야 하는 지 알 수 있음.

 

Offset Topic

Kafka 에는 Consumer 의 offset 을 저장 하는 topic 이 존재함. (v0.9 release)

  • __consumer_offsets
    • 일반 토픽처럼 partitioning, replication 되어 있음(offsets.topic.replication.factor=1 (default=1)
      • Consumer 가 Kafka에 현재까지 읽은 메세지의 offset 정보를 알려주는 것을 commit 이라 한다.

 

 

'빅데이터 > Kafka Cluster' 카테고리의 다른 글

Kafka restart과 retention  (0) 2022.02.28
Kafka 스트레스 테스트  (0) 2022.02.28
Kafka Consumer/Producer  (0) 2022.02.28
Kafka 자주 사용하는 명령  (0) 2022.02.28
Kafka cluster failover  (0) 2022.02.28

Kafka에서는 토픽별로 leader와 follower가 존재한다.

토픽으로 통하는 모든 데이터의 read/write는 오직 leader와 이루어진다. leader가 다운되거나 leader가 있는 브로커가 다운된다면 flower 중의 하나가 leader가 된다.  이때, 일시적으로 follower가 leader가 되는 과정에서 read/write의 timeout이 발생할 수 있겠지만, retry가 일어나면 new leader와 read/write가 가능하기 때문에 장애 상황을 피할 수 있다.

 

ISR(In Sync Replica)

topic이 생성되고 옵션으로 replication factor를 설정하면 replication factor 수에 맞추어 ISR이 구성된다. 

leader : follower 중에서 자신보다 일정시간 동안 뒤쳐지면 leader가 될 자격이 없다고 판단하여 뒤쳐지는 follwer를 ISR에서 제외

follower : leader와 동일한 데이터 내용을 유지하기 위해서 짧은 주기로 leader로부터 데이터를 가져온다.

 

All down

1. 마지막까지 leader였던 broker1번이 up이 되고 다시 leader가 될 때까지 기다린다.

2. (default)ISR과 상관없이 누구라도 가장 빨리 up이 되는 topic이 leader가 된다(데이터 손실이 되더라도 장애 대응이 빠르다)


출처 : https://www.popit.kr/kafka-%EC%9A%B4%EC%98%81%EC%9E%90%EA%B0%80-%EB%A7%90%ED%95%98%EB%8A%94-topic-replication/

'빅데이터 > Kafka Cluster' 카테고리의 다른 글

Kafka restart과 retention  (0) 2022.02.28
Kafka 스트레스 테스트  (0) 2022.02.28
Kafka Consumer/Producer  (0) 2022.02.28
Kafka 자주 사용하는 명령  (0) 2022.02.28
Kafka Overview  (0) 2022.02.28

 


출처 : https://bamdule.tistory.com/51

+ Recent posts