Last active
November 17, 2024 13:55
-
-
Save ralph-tice/bc8f66c4fe1d4238ce54c54272fc4ba2 to your computer and use it in GitHub Desktop.
Resizing a Kafka cluster via bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -xe | |
# requires jq and 3 brokers to start | |
which jq || echo 'no jq found, bye!' && exit 1 | |
ZK_HOST=zkhost1:2181/kafka | |
# record all topics | |
/usr/local/kafka/bin/kafka-topics.sh --zookeeper $ZK_HOST --list > all_topics | |
# generate json for listing which topics to reassign | |
grep -v deletion all_topics | tr '\n' ' ' | head --bytes -1 | jq -R 'split(" ") | reduce .[] as $topic ([]; . + [{"topic": $topic }]) | {"topics": . , "version": 1}' > all_topics.json | |
# generate the json for reassigning topics, the tool outputs current state then proposed state so take only the last line. For a real cluster resize, you'd include all the brokers, but in this case I'm retiring brokers 0, 1 and 2. | |
/usr/local/kafka/bin/kafka-reassign-partitions.sh --zookeeper $ZK_HOST --topics-to-move-json-file all_topics.json --broker-list "4,5,6" --generate | tail -n 1 > reassignment.json | |
# actually do the reassignment | |
/usr/local/kafka/bin/kafka-reassign-partitions.sh --zookeeper $ZK_HOST --topics-to-move-json-file all_topics.json --reassignment-json-file reassignment.json --execute | |
# watch to see when reassignment is completed | |
watch -n 5 /usr/local/kafka/bin/kafka-reassign-partitions.sh --zookeeper $ZK_HOST --topics-to-move-json-file all_topics.json --reassignment-json-file reassignment.json --verify | |
in line 17 you don't need this part --topics-to-move-json-file all_topics.json
in line 20 , we don't need this part --topics-to-move-json-file all_topics.json
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks.
Tip: Use the -s option with jq for large lists. It prevents in from splitting in between a line when a buffer fills. Ex