From 9bcec025444017303ac8b3469426f9ec1aeaf788 Mon Sep 17 00:00:00 2001 From: Tim Dorr Date: Thu, 28 Mar 2019 13:24:18 -0400 Subject: [PATCH 1/2] Add a HEALTHCHECK --- Dockerfile | 6 ++++-- healthcheck.sh | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100755 healthcheck.sh diff --git a/Dockerfile b/Dockerfile index e73e23d3..9e63a1d9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,11 +22,11 @@ ENV KAFKA_VERSION=$kafka_version \ ENV PATH=${PATH}:${KAFKA_HOME}/bin -COPY download-kafka.sh start-kafka.sh broker-list.sh create-topics.sh versions.sh /tmp/ +COPY healthcheck.sh download-kafka.sh start-kafka.sh broker-list.sh create-topics.sh versions.sh /tmp/ RUN apk add --no-cache bash curl jq docker \ && chmod a+x /tmp/*.sh \ - && mv /tmp/start-kafka.sh /tmp/broker-list.sh /tmp/create-topics.sh /tmp/versions.sh /usr/bin \ + && mv /tmp/start-kafka.sh /tmp/broker-list.sh /tmp/create-topics.sh /tmp/versions.sh /tmp/healthcheck.sh /usr/bin \ && sync && /tmp/download-kafka.sh \ && tar xfz /tmp/kafka_${SCALA_VERSION}-${KAFKA_VERSION}.tgz -C /opt \ && rm /tmp/kafka_${SCALA_VERSION}-${KAFKA_VERSION}.tgz \ @@ -40,5 +40,7 @@ COPY overrides /opt/overrides VOLUME ["/kafka"] +HEALTHCHECK --interval=5s --timeout=10s --retries=5 CMD ["healthcheck.sh"] + # Use "exec" form so that it runs as PID 1 (useful for graceful shutdown) CMD ["start-kafka.sh"] diff --git a/healthcheck.sh b/healthcheck.sh new file mode 100755 index 00000000..4813c059 --- /dev/null +++ b/healthcheck.sh @@ -0,0 +1,20 @@ +#! /bin/bash + +r=`$KAFKA_HOME/bin/zookeeper-shell.sh zookeeper:2181 <<< "ls /brokers/ids" | tail -1 | jq '.[]'` +ids=( $r ) +function contains() { + local n=$# + local value=${!n} + for ((i=1;i < $#;i++)) { + if [ "${!i}" == "${value}" ]; then + echo "y" + return 0 + fi + } + echo "n" + return 1 +} + +LOG_DIR=$(awk -F= -v x="log.dirs" '$1==x{print $2}' /opt/kafka/config/server.properties) +x=`cat ${LOG_DIR}/meta.properties | awk 'BEGIN{FS="="}/^broker.id=/{print $2}'` +if [ $(contains "${ids[@]}" "$x") == "y" ]; then echo "ok"; exit 0; else echo "doh"; exit 1; fi From d8c5f6c4812ba89d0ed5de6ab7dd46bff624769a Mon Sep 17 00:00:00 2001 From: Tim Dorr Date: Thu, 28 Mar 2019 13:27:46 -0400 Subject: [PATCH 2/2] Use the ENV for the zookeeper instance --- healthcheck.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/healthcheck.sh b/healthcheck.sh index 4813c059..3d6e6d00 100755 --- a/healthcheck.sh +++ b/healthcheck.sh @@ -1,6 +1,6 @@ #! /bin/bash -r=`$KAFKA_HOME/bin/zookeeper-shell.sh zookeeper:2181 <<< "ls /brokers/ids" | tail -1 | jq '.[]'` +r=`$KAFKA_HOME/bin/zookeeper-shell.sh $KAFKA_ZOOKEEPER_CONNECT <<< "ls /brokers/ids" | tail -1 | jq '.[]'` ids=( $r ) function contains() { local n=$#