Consume๋กœ Kafka ํ† ํ”ฝ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด๊ณ , ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด

3 minute read

๋“ค์–ด๊ฐ€๋ฉฐ

ํšŒ์‚ฌ์—์„œ Confluent๋กœ Kafka ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์šด์˜ํ•˜๊ณ  ์žˆ๊ณ , CCDAK ์ž๊ฒฉ์ฆ ์‹œํ—˜์„ ์ค€๋น„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” bin/kafka-topics.sh ๋ช…๋ น์–ด์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์‹œ์ž‘ํ•˜๊ธฐ ์ „์—

๋กœ์ปฌ์—์„œ Kafka๋ฅผ ์‹คํ–‰ํ•ด ํ•ธ์ฆˆ์˜จ ํ•ด๋ณด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค! ๋กœ์ปฌ ๋งฅ๋ถ์— Kafka ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๋””ํ”Œ๋กœ์ด ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ โ€œDeploy Kafka on Kubernetes โ˜ธโ€ ํฌ์ŠคํŠธ์—์„œ ๊ธฐ์ˆ ํ•˜์˜€์œผ๋‹ˆ ์ฐธ๊ณ  ๋ฐ”๋ž๋‹ˆ๋‹ค! ๋ณธ ํฌ์ŠคํŠธ๋Š” ํ•ด๋‹น ํฌ์ŠคํŠธ์— ๊ธฐ์ˆ ํ•œ ๋ฐฉ์‹์œผ๋กœ ๋””ํ”Œ๋กœ์ดํ•œ Kafka ํด๋Ÿฌ์Šคํ„ฐ์˜ Broker ์ค‘ ํ•˜๋‚˜์— ์ ‘์†ํ•ด ์ง„ํ–‰ํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

ํด๋Ÿฌ์Šคํ„ฐ ์ ‘์†

bitnami-kafka๋ฅผ Authentication ์—†์ด ์ ‘์†ํ•˜๋Š” PLAINTEXT ๋ชจ๋“œ๋กœ ๋””ํ”Œ๋กœ์ด ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋””ํ”Œ๋กœ์ดํ•œ Broker ์ค‘ ํ•˜๋‚˜์— kubectl exec๋กœ ์ ‘์† ํ•ฉ๋‹ˆ๋‹ค.

$ kubectl exec -it bitnami-kafka-broker-2 -n kafka -- sh

์ฐธ๊ณ ๋กœ bitnami-kafka๋ฅผ ๋””ํ”Œ๋กœ์ด ํ•œ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— Kafka Shell์ด /opt/bitnami/kafka/bin ๊ฒฝ๋กœ์— ๋ชจ์—ฌ ์žˆ์Šต๋‹ˆ๋‹ค! cd ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ด ์ด ๊ฒฝ๋กœ๋กœ ํ•ด๋‹น ๊ฒฝ๋กœ๋กœ ์ด๋™ํ•ด๋„ ๋˜์ง€๋งŒ, Kafka Shell ๋„๊ตฌ๋“ค์ด ์ด๋ฏธ ๋“ฑ๋ก์ด ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ฌด ๊ฒฝ๋กœ์—์„œ ์ง„ํ–‰ํ•ด๋„ ์ƒ๊ด€ ์—†์Šต๋‹ˆ๋‹ค :)

Kafka Console Producer

$ kafka-console-producer.sh \
    --bootstrap-server bitnami-kafka:9092 \
    --topic test
> haha
> hoho

Kafka ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ํ…Œ์ŠคํŠธ ํ•  ๋•Œ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š” ๋ช…๋ น์–ด ์ž…๋‹ˆ๋‹ค. ์š” ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์•„์ฃผ ์‰ฝ๊ฒŒ ๋”๋ฏธ ๋ฐ์ดํ„ฐ๋ฅผ ํ† ํ”ฝ์— ์ ์žฌํ•˜๊ณ  ํ™•์ธํ•ด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค๋งŒ, ์œ„์˜ ๋ช…๋ น์–ด๋Š” ํ† ํ”ฝ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์„ ๋•Œ, key๋ฅผ ์„ค์ •ํ•˜์ง€ ์•Š๊ณ  NULL ๊ฐ’์œผ๋กœ ๋ณด๋‚ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ๋ ˆ์ฝ”๋“œ๋Š” Round Robin ๋ฐฉ์‹์œผ๋กœ ํ† ํ”ฝ์˜ ํŒŒํ‹ฐ์…˜ ์ค‘ ํ•˜๋‚˜์— ์ ์žฌ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ

$ kafka-console-producer.sh \
    --bootstrap-server bitnami-kafka:9092 \
    --topic test \
    --property key.separator=:
> haha:haha
> hoho:hoho

์š”๋ ‡๊ฒŒ key.separator๋ฅผ ์„ค์ •ํ•˜๋ฉด key ๊ฐ’์„ ํ•จ๊ป˜ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Kafka Console Consumer

$ kafka-console-consumer.sh \
    --bootstrap-server bitnami-kafka:9092 \
    --topic test \
    --from-beginning
haha
hoho
ha:ha
ho:ho

kafka-console-consumer.sh๋Š” ์ฝ˜์†”๋กœ Kafka ํ† ํ”ฝ์˜ ๋‚ด์šฉ์„ ํ™•์ธํ•˜๋Š” ๋„๊ตฌ ์ž…๋‹ˆ๋‹ค. --from-beginning ์˜ต์…˜์„ ์ฃผ๋ฉด, ํ† ํ”ฝ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ฝ์–ด์˜ต๋‹ˆ๋‹ค.

๊ทธ์™ธ์—๋„ --partition ์˜ต์…˜์œผ๋กœ ํŠน์ • ํŒŒํ‹ฐ์…˜์— ์ €์žฅ๋œ ๋ ˆ์ฝ”๋“œ๋งŒ ์ฝ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

$ kafka-console-consumer.sh \
    --bootstrap-server bitnami-kafka:9092 \
    --topic test \
    --group console
...

๋˜, ํ•˜๋‚˜ ์œ ์šฉํ•œ ์˜ต์…˜์„ Consoler Consumer๋ฅผ ์ปจ์Šˆ๋จธ ๊ทธ๋ฃน์œผ๋กœ ์ทจ๊ธ‰ํ•˜๊ฒŒ ํ•˜๋Š” --group ์˜ต์…˜ ์ž…๋‹ˆ๋‹ค. ์ด ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด, Console Consumer ์ž์ฒด๋กœ ์ด๋ค„์ง„ ์ปจ์Šˆ๋จธ ๊ทธ๋ฃน๊ฐ€ ์ƒ์„ฑ๋˜๋ฉฐ, ๊ทธ ์ปจ์Šˆ๋จธ ๊ทธ๋ฃน์ด ์–ด๋””๊นŒ์ง€ ์ฝ์—ˆ๋Š”์ง€ Offset ์ •๋ณด๊ฐ€ Kafka ํด๋Ÿฌ์Šคํ„ฐ์˜ Offset ํ† ํ”ฝ์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

์‹ค์ œ๋กœ ์ปจ์Šˆ๋จธ ๊ทธ๋ฃน์ด ์ƒ์„ฑ๋œ๊ฒŒ ๋งž๋Š”์ง€ ํ™•์ธํ•˜๋ ค๋ฉด kafka-consumer-groups.sh ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

$ kafka-consumer-groups.sh \
    --bootstrap-server bitnami-kafka:9092 \
    --list
console
console-consumer-9123
console-consumer-88809
$ kafka-consumer-groups.sh \
    --bootstrap-server bitnami-kafka:9092 \
    --describe \
    --group console
GROUP           TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID                                           HOST            CLIENT-ID
console         test            0          5               5               0               console-consumer-7c2f547f-2d90-48cc-9aab-19b779cec481 /10.42.0.78     console-consumer

Categories:

Updated: