Skip to content

Commit

Permalink
[DOCKER] add script to run 3 brokers quickly
Browse files Browse the repository at this point in the history
  • Loading branch information
chia7712 committed Jan 2, 2025
1 parent b8be055 commit 3cffb76
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ app.dockerfile
deps.dockerfile
worker.dockerfile
hadoop.dockerfile
kafka.dockerfile
# we don't put binary file to git repo
gradle-wrapper.jar
VersionUtils.java
138 changes: 138 additions & 0 deletions docker/start_kafka.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#!/bin/bash
# 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.

# ===============================[global variables]===============================
declare -r ADDRESS=$(ip -o -4 address show | awk ' NR==2 { gsub(/\/.*/, "", $4); print $4 } ')
declare -r KAFKA_VERSION=${KAFKA_VERSION:-2.1.1}
declare -r DOCKERFILE=/tmp/astraea/kafka.dockerfile
declare -r JMX0_OPTS="-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.port=11111 \
-Dcom.sun.management.jmxremote.rmi.port=11111 \
-Djava.rmi.server.hostname=$ADDRESS"
declare -r JMX1_OPTS="-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.port=11112 \
-Dcom.sun.management.jmxremote.rmi.port=11112 \
-Djava.rmi.server.hostname=$ADDRESS"
declare -r JMX2_OPTS="-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.port=11113 \
-Dcom.sun.management.jmxremote.rmi.port=11113 \
-Djava.rmi.server.hostname=$ADDRESS"
declare -r HEAP_OPTS="${HEAP_OPTS:-"-Xmx2G -Xms2G"}"
declare -r BROKER_PROPERTIES="/tmp/kafka-${BROKER_PORT}.properties"
declare -r IMAGE_NAME="ghcr.io/opensource4you/astraea/kafka:${KAFKA_VERSION,,}"

function generateDockerfileByVersion() {
echo "# this dockerfile is generated dynamically
FROM ubuntu:24.04 AS build
# install tools
RUN apt-get update && apt-get install -y wget
# download kafka
WORKDIR /tmp
RUN wget https://archive.apache.org/dist/kafka/${KAFKA_VERSION}/kafka_2.12-${KAFKA_VERSION}.tgz
RUN mkdir /opt/kafka
RUN tar -zxvf kafka_2.12-${KAFKA_VERSION}.tgz -C /opt/kafka --strip-components=1
FROM azul/zulu-openjdk:11-jre
# copy kafka
COPY --from=build /opt/kafka /opt/kafka
# add user
RUN groupadd $USER && useradd -ms /bin/bash -g $USER $USER
# change user
RUN chown -R $USER:$USER /opt/kafka
USER $USER
# export ENV
ENV KAFKA_HOME=/opt/kafka
WORKDIR /opt/kafka
# cp configs
RUN cp config/server.properties config/server0.properties
RUN cp config/server.properties config/server1.properties
RUN cp config/server.properties config/server2.properties
# update zk connect
RUN echo \"\nzookeeper.connect=$ADDRESS:2181\" >> config/server0.properties
RUN echo \"\nzookeeper.connect=$ADDRESS:2181\" >> config/server1.properties
RUN echo \"\nzookeeper.connect=$ADDRESS:2181\" >> config/server2.properties
# update advertised.listeners
RUN echo \"\nadvertised.listeners=PLAINTEXT://$ADDRESS:9092\" >> config/server0.properties
RUN echo \"\nadvertised.listeners=PLAINTEXT://$ADDRESS:9093\" >> config/server1.properties
RUN echo \"\nadvertised.listeners=PLAINTEXT://$ADDRESS:9094\" >> config/server2.properties
# update broker.id
RUN echo \"\nbroker.id=0\" >> config/server0.properties
RUN echo \"\nbroker.id=1\" >> config/server1.properties
RUN echo \"\nbroker.id=2\" >> config/server2.properties
" >"$DOCKERFILE"
}

mkdir -p /tmp/astraea
generateDockerfileByVersion
docker build -t "$IMAGE_NAME" -f "$DOCKERFILE" /tmp/astraea

# cleanup
docker rm -f zk bk-00 bk-01 bk-02

# start zk
docker run --rm \
--name zk \
-h zk \
-p 2181:2181 \
-d \
"$IMAGE_NAME" bin/zookeeper-server-start.sh config/zookeeper.properties

# start bk-00
docker run --rm \
--name bk-00 \
-h bk-00 \
-p 9092:9092 \
-p 11111:11111 \
-d \
-e KAFKA_JMX_OPTS="$JMX0_OPTS" \
"$IMAGE_NAME" bin/kafka-server-start.sh config/server0.properties

# start bk-01
docker run --rm \
--name bk-01 \
-h bk-01 \
-p 9093:9092 \
-p 11112:11112 \
-d \
-e KAFKA_JMX_OPTS="$JMX1_OPTS" \
"$IMAGE_NAME" bin/kafka-server-start.sh config/server1.properties

# start bk-02
docker run --rm \
--name bk-02 \
-h bk-02 \
-p 9094:9092 \
-p 11113:11113 \
-d \
-e KAFKA_JMX_OPTS="$JMX2_OPTS" \
"$IMAGE_NAME" bin/kafka-server-start.sh config/server2.properties

0 comments on commit 3cffb76

Please sign in to comment.