Skip to content

Commit

Permalink
[FLINK-5635] [docker] Improvements for Docker on Flink experience
Browse files Browse the repository at this point in the history
Modifying Dockerfile to build from local flink-dist as well as release URLs.
Logging to stdout.
Adding scripts to deploy seamlessly on Docker Swarm.
Updating Docker Compose scripts to work correctly.
Parameterizing things so these Docker scripts are more generally useful.
  • Loading branch information
jgrier authored and uce committed Mar 15, 2017
1 parent 31ab4b2 commit 227478b
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 21 deletions.
24 changes: 11 additions & 13 deletions flink-contrib/docker-flink/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ FROM java:8-jre-alpine
# Install requirements
RUN apk add --no-cache bash snappy

# Configure Flink version
ARG FLINK_VERSION=1.1.3
ARG HADOOP_VERSION=27
ARG SCALA_VERSION=2.11

# Flink environment variables
ENV FLINK_INSTALL_PATH=/opt
ENV FLINK_HOME $FLINK_INSTALL_PATH/flink
Expand All @@ -36,22 +31,25 @@ ENV PATH $PATH:$FLINK_HOME/bin
EXPOSE 8081
EXPOSE 6123

# flink-dist can point to a directory, a tarball on the local system, or a url to a tarball
ARG flink_dist=NOT_SET

# Install build dependencies and flink
ADD $flink_dist $FLINK_INSTALL_PATH
RUN set -x && \
mkdir -p $FLINK_INSTALL_PATH && \
apk --update add --virtual build-dependencies curl && \
curl -s $(curl -s https://www.apache.org/dyn/closer.cgi\?preferred\=true)flink/flink-${FLINK_VERSION}/flink-${FLINK_VERSION}-bin-hadoop${HADOOP_VERSION}-scala_${SCALA_VERSION}.tgz | \
tar xvz -C $FLINK_INSTALL_PATH && \
ln -s $FLINK_INSTALL_PATH/flink-$FLINK_VERSION $FLINK_HOME && \
ln -s $FLINK_INSTALL_PATH/flink-* $FLINK_HOME && \
addgroup -S flink && adduser -D -S -H -G flink -h $FLINK_HOME flink && \
chown -R flink:flink $FLINK_INSTALL_PATH/flink-$FLINK_VERSION && \
chown -R flink:flink $FLINK_INSTALL_PATH/flink-* && \
chown -h flink:flink $FLINK_HOME && \
sed -i -e "s/echo \$mypid >> \$pid/echo \$mypid >> \$pid \&\& wait/g" $FLINK_HOME/bin/flink-daemon.sh && \
apk del build-dependencies && \
rm -rf /var/cache/apk/*
sed -i -e "s/echo \$mypid >> \$pid/echo \$mypid >> \$pid \&\& wait/g" $FLINK_HOME/bin/flink-daemon.sh

# Configure container
USER flink
ADD docker-entrypoint.sh $FLINK_HOME/bin/

# Overwrite default logging settings. This will additionally log to stdout so we can use 'docker logs'
ADD log4j.properties $FLINK_HOME/conf/

ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["sh", "-c"]
80 changes: 79 additions & 1 deletion flink-contrib/docker-flink/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,82 @@
# limitations under the License.
################################################################################

docker build -t "flink" .
usage() {
cat <<HERE
Usage:
build.sh --from-local-dist [--image-name <image>]
build.sh --from-release --flink-version <x.x.x> --hadoop-version <x.x> --scala-version <x.xx> [--image-name <image>]
build.sh --help
If the --image-name flag is not used the built image name will be 'flink'.
HERE
exit 1
}

while [[ $# -ge 1 ]]
do
key="$1"
case $key in
--from-local-dist)
FROM_LOCAL="true"
;;
--from-release)
FROM_RELEASE="true"
;;
--image-name)
IMAGE_NAME="$2"
shift
;;
--flink-version)
FLINK_VERSION="$2"
shift
;;
--hadoop-version)
HADOOP_VERSION="$(echo "$2" | sed 's/\.//')"
shift
;;
--scala-version)
SCALA_VERSION="$2"
shift
;;
--help)
usage
;;
*)
# unknown option
;;
esac
shift
done

IMAGE_NAME=${IMAGE_NAME:-flink}

TMPDIR=_TMP_
mkdir -p "${TMPDIR}"

if [ -n "${FROM_RELEASE}" ]; then

[[ -n "${FLINK_VERSION}" ]] && [[ -n "${HADOOP_VERSION}" ]] && [[ -n "${SCALA_VERSION}" ]] || usage

FLINK_BASE_URL="$(curl -s https://www.apache.org/dyn/closer.cgi\?preferred\=true)flink/flink-${FLINK_VERSION}/"
FLINK_DIST_FILE_NAME="flink-${FLINK_VERSION}-bin-hadoop${HADOOP_VERSION}-scala_${SCALA_VERSION}.tgz"
CURL_OUTPUT="${TMPDIR}/${FLINK_DIST_FILE_NAME}"

echo "Downloading ${FLINK_DIST_FILE_NAME} from ${FLINK_BASE_URL}"
curl -s ${FLINK_BASE_URL}${FLINK_DIST_FILE_NAME} --output ${CURL_OUTPUT}

FLINK_DIST="${CURL_OUTPUT}"

elif [ -n "${FROM_LOCAL}" ]; then

DIST_DIR="../../flink-dist/target/flink-*-bin"
FLINK_DIST="${TMPDIR}/flink.tgz"
echo "Using flink dist: ${DIST_DIR}"
tar -C ${DIST_DIR} -cvzf "${FLINK_DIST}" .
else
usage
fi

docker build --build-arg flink_dist="${FLINK_DIST}" -t "${IMAGE_NAME}" .

rm -rf "${TMPDIR}"
54 changes: 54 additions & 0 deletions flink-contrib/docker-flink/create-docker-swarm-service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/sh

################################################################################
# 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.
################################################################################

usage() {
cat <<HERE
Usage:
create-docker-swarm-service.sh [--image-name <image>] <service-name> <service-port>
If the --image-name flag is not used the service will use the 'flink' image.
HERE
exit 1
}

if [ "$1" == "--image-name" ]; then
IMAGE_NAME="$2"
shift; shift
else
IMAGE_NAME=flink
fi

[[ $# -ne 2 ]] && usage

SERVICE_BASE_NAME="$1"
SERVICE_PORT="${2}"
JOB_MANAGER_NAME=${SERVICE_BASE_NAME}-jobmanager
TASK_MANAGER_NAME=${SERVICE_BASE_NAME}-taskmanager
JOB_MANAGER_RPC_ADDRESS=${JOB_MANAGER_NAME}
OVERLAY_NETWORK_NAME=${SERVICE_BASE_NAME}

# Create overlay network
docker network create -d overlay ${OVERLAY_NETWORK_NAME}

# Create the jobmanager service
docker service create --name ${JOB_MANAGER_NAME} --env JOB_MANAGER_RPC_ADDRESS=${JOB_MANAGER_RPC_ADDRESS} -p ${SERVICE_PORT}:8081 --network ${OVERLAY_NETWORK_NAME} ${IMAGE_NAME} jobmanager

# Create the taskmanger service (scale this out as needed)
docker service create --name ${TASK_MANAGER_NAME} --env JOB_MANAGER_RPC_ADDRESS=${JOB_MANAGER_RPC_ADDRESS} --network ${OVERLAY_NETWORK_NAME} ${IMAGE_NAME} taskmanager
15 changes: 8 additions & 7 deletions flink-contrib/docker-flink/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@
# limitations under the License.
################################################################################

version: "2"
# Set the FLINK_DOCKER_IMAGE_NAME environment variable to override the image name to use

version: "2.1"
services:
jobmanager:
image: flink
container_name: "jobmanager"
image: ${FLINK_DOCKER_IMAGE_NAME:-flink}
expose:
- "6123"
ports:
- "48081:8081"
- "8081:8081"
command: jobmanager
environment:
- JOB_MANAGER_RPC_ADDRESS="jobmanager"
- JOB_MANAGER_RPC_ADDRESS=jobmanager

taskmanager:
image: flink
image: ${FLINK_DOCKER_IMAGE_NAME:-flink}
expose:
- "6121"
- "6122"
Expand All @@ -40,4 +41,4 @@ services:
links:
- "jobmanager:jobmanager"
environment:
- JOB_MANAGER_RPC_ADDRESS="jobmanager"
- JOB_MANAGER_RPC_ADDRESS=jobmanager
47 changes: 47 additions & 0 deletions flink-contrib/docker-flink/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
################################################################################
# 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.
################################################################################

# This affects logging for both user code and Flink
log4j.rootLogger=INFO, file, console

# Uncomment this if you want to _only_ change Flink's logging
#log4j.logger.org.apache.flink=INFO

# The following lines keep the log level of common libraries/connectors on
# log level INFO. The root logger does not override this. You have to manually
# change the log levels here.
log4j.logger.akka=INFO
log4j.logger.org.apache.kafka=INFO
log4j.logger.org.apache.hadoop=INFO
log4j.logger.org.apache.zookeeper=INFO

# Log all infos in the given file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.file=${log.file}
log4j.appender.file.append=false
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %-60c %x - %m%n

# Log to stdout as well
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.err
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %-60c %x - %m%n

# Suppress the irrelevant (wrong) warnings from the Netty channel handler
log4j.logger.org.jboss.netty.channel.DefaultChannelPipeline=ERROR, file, console
43 changes: 43 additions & 0 deletions flink-contrib/docker-flink/remove-docker-swarm-service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/sh

################################################################################
# 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.
################################################################################

usage() {
cat <<HERE
Usage:
remove-docker-swarm-service.sh <service-name>
HERE
exit 1
}

[[ $# -ne 1 ]] && usage

SERVICE_BASE_NAME="$1"
JOB_MANAGER_NAME=${SERVICE_BASE_NAME}-jobmanager
TASK_MANAGER_NAME=${SERVICE_BASE_NAME}-taskmanager
OVERLAY_NETWORK_NAME=${SERVICE_BASE_NAME}

# Remove taskmanager service
docker service rm ${TASK_MANAGER_NAME}

# Remove jobmanager service
docker service rm ${JOB_MANAGER_NAME}

# Remove overlay network
docker network rm ${OVERLAY_NETWORK_NAME}

0 comments on commit 227478b

Please sign in to comment.