Skip to content

Instantly share code, notes, and snippets.

@CROSP
Created February 7, 2019 00:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CROSP/0d98ec1d5da389c679025a75260c7599 to your computer and use it in GitHub Desktop.
Save CROSP/0d98ec1d5da389c679025a75260c7599 to your computer and use it in GitHub Desktop.
Kafka Manager Auto Cluster Creation
version: '3.1'
services:
zookeeper:
image: zookeeper:3.4
volumes:
- "./zookeeper/data:/data"
- "./zookeeper/logs:/datalog"
ports:
- "2181:2181"
kafka:
image: wurstmeister/kafka:2.12-2.0.1
depends_on:
- zookeeper
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
- KAFKA_ADVERTISED_HOST_NAME=kafka.buslux
- JMX_PORT=9093
- KAFKA_ADVERTISED_PORT=9092
- KAFKA_AUTO_CREATE_TOPICS_ENABLE=true
- KAFKA_NUM_PARTITIONS=2
ports:
- "9092:9092"
- "9093:9093"
kafka-manager:
image: hlebalbau/kafka-manager:stable
depends_on:
- kafka
- zookeeper
command: -Dconfig.file=/kafka-manager/conf/application.conf -Dapplication.home=/kafkamanager
environment:
- ZK_HOSTS=zookeeper:2181
- APPLICATION_SECRET=randomAppSecretBulletProof
ports:
- "9594:9000"
bootrstrap-config:
image: ellerbrock/alpine-bash-curl-ssl
restart: "no"
environment:
- KAFKA_MANAGER_HOST=kafka-manager:9000
- KAFKA_CLUSTERS=zookeeper:2181#BusLux,zookeeper-2:2181#BusLux2
depends_on:
- kafka-manager
- kafka
- zookeeper
command: /bin/bash /config.sh
volumes:
- "./kafka-config.sh:/config.sh"
#!/bin/bash
RETRY_COUNT=20
SLEEP_TIME=5
GET_CLUSTER_INFO_URL="$KAFKA_MANAGER_HOST/clusters/cname"
CREATE_CLUSTER_COMMAND="curl 'http://$KAFKA_MANAGER_HOST/clusters' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Origin: http://$KAFKA_MANAGER_HOST' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Referer: http://$KAFKA_MANAGER_HOST/addCluster' -H 'Accept-Encoding: gzip, deflate, br' --data 'name=cname&zkHosts=zookeeper%3A2181&kafkaVersion=2.0.0&jmxUser=&jmxPass=&tuning.brokerViewUpdatePeriodSeconds=30&tuning.clusterManagerThreadPoolSize=2&tuning.clusterManagerThreadPoolQueueSize=100&tuning.kafkaCommandThreadPoolSize=2&tuning.kafkaCommandThreadPoolQueueSize=100&tuning.logkafkaCommandThreadPoolSize=2&tuning.logkafkaCommandThreadPoolQueueSize=100&tuning.logkafkaUpdatePeriodSeconds=30&tuning.partitionOffsetCacheTimeoutSecs=5&tuning.brokerViewThreadPoolSize=4&tuning.brokerViewThreadPoolQueueSize=1000&tuning.offsetCacheThreadPoolSize=4&tuning.offsetCacheThreadPoolQueueSize=1000&tuning.kafkaAdminClientThreadPoolSize=4&tuning.kafkaAdminClientThreadPoolQueueSize=1000&tuning.kafkaManagedOffsetMetadataCheckMillis=30000&tuning.kafkaManagedOffsetGroupCacheSize=1000000&tuning.kafkaManagedOffsetGroupExpireDays=7&securityProtocol=PLAINTEXT&saslMechanism=DEFAULT&jaasConfig=' --compressed"
if [[ -z "${KAFKA_CLUSTERS}" ]] || [[ -z "${KAFKA_MANAGER_HOST}" ]]; then
echo "Provide all configuration options"
exit
else
IFS=',' read -r -a clusters <<< "$KAFKA_CLUSTERS"
echo "Setting up clusters with default settings"
# Wait for KAFKA manager
# Check whether host is available
retries=0
while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' $KAFKA_MANAGER_HOST)" != "200" ]] && [[ $retries -lt $RETRY_COUNT ]]; do
echo "Connecting to $KAFKA_MANAGER_HOST , attempt #$retries"
retries=$((retries+1))
sleep $SLEEP_TIME;
done
echo "Connected to the Kafka Manager host"
# Set clusters
for i in "${clusters[@]}"
do
IFS='#' read -r -a clusterinfo <<< "$i"
address=${clusterinfo[0]}
name=${clusterinfo[1]}
echo "Setting up Cluster $address - $name"
echo "Checking if the cluster already defined"
effective_url=${GET_CLUSTER_INFO_URL/cname/$name}
echo "$effective_url"
result=$(curl -s $effective_url)
if [[ $result == *"Unknown cluster"* ]]; then
echo "Creating cluster $address - $name"
effective_command=${CREATE_CLUSTER_COMMAND/cname/$name}
result=$(eval $effective_command)
echo "Cluster has been created"
else
echo "Cluster already exists"
fi
done
fi
@bmagistro
Copy link

I was having issues with it creating the cluster defined in the variable so made some tweaks to the code.

https://gist.github.com/bmagistro/f136a6a1c2fc3bf6d58841c8ebf8a646

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment