Skip to content

Commit c357e3f

Browse files
zwangshengulysses-you
authored andcommitted
[KYUUBI #1396][K8S] Add docker image tools and replace Dockerfile
<!-- Thanks for sending a pull request! Here are some tips for you: 1. If this is your first time, please read our contributor guidelines: https://kyuubi.readthedocs.io/en/latest/community/contributions.html 2. If the PR is related to an issue in https://github.com/apache/incubator-kyuubi/issues, add '[KYUUBI #XXXX]' in your PR title, e.g., '[KYUUBI #XXXX] Your PR title ...'. 3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][KYUUBI #XXXX] Your PR title ...'. --> ### _Why are the changes needed?_ <!-- Please clarify why the changes are needed. For instance, 1. If you add a feature, you can talk about the use case of it. 2. If you fix a bug, you can clarify why it is a bug. --> At the beginning, you just want to add a script to help the user build the image.In the process, the Dockerfile of the existing master branch was discovered. Although the multi-layer build method can help to build quickly and easily, it is only limited to source code. In other words, when the user uses the binary package officially released, the Dockerfile is deactivated. So, In this PR, we move it(the multi-layer Dockerfile) to dev. And use the previous Dockerfile. ### _How was this patch tested?_ - [ ] Add some test cases that check the changes thoroughly including negative and positive cases if possible - [ ] Add screenshots for manual tests if appropriate - [x] [Run test](https://kyuubi.readthedocs.io/en/latest/develop_tools/testing.html#running-tests) locally before make a pull request Closes #1435 from zwangsheng/add-docker-image-tools. Closes #1396 83d593a [zwangsheng] fix note 30772c5 [zwangsheng] fix 499b396 [zwangsheng] fix e64ffb7 [zwangsheng] fix 27b5411 [zwangsheng] fix fcdfc55 [zwangsheng] dix 3aea0a0 [zwangsheng] fix SPARK_HOME print 67a9708 [zwangsheng] remove help code 74efb24 [zwangsheng] move and modify workflow f7649e8 [zwangsheng] move and modify workflow 4672a09 [zwangsheng] move and modify workflow 7447ce2 [zwangsheng] move 567478d [zwangsheng] move a224ac0 [zwangsheng] move cadeef2 [zwangsheng] workflow use dev/Dockerfile 354bc14 [zwangsheng] fix Dockerfile 83561d8 [zwangsheng] build spark into docker image 7d4323c [zwangsheng] add kyuubi verison help build tag 4748d09 [zwangsheng] use offical image 4f107d8 [zwangsheng] use offical iamhge f8c8ba9 [zwangsheng] for test cecd048 [zwangsheng] modify 0991732 [zwangsheng] add docker-image Authored-by: zwangsheng <2213335496@qq.com> Signed-off-by: ulysses-you <ulyssesyou@apache.org>
1 parent 78c9cf3 commit c357e3f

File tree

6 files changed

+365
-41
lines changed

6 files changed

+365
-41
lines changed

.github/workflows/docker-image.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
username: ${{ secrets.DOCKERHUB_USER }}
2323
password: ${{ secrets.DOCKERHUB_TOKEN }}
2424
- name: Build Kyuubi Docker Image
25-
run: docker build --tag apache/kyuubi:master-snapshot --file docker/Dockerfile .
25+
run: docker build --tag apache/kyuubi:master-snapshot --file build/Dockerfile .
2626
- name: Docker image
2727
run: docker images
2828
- name: Push Docker image

.github/workflows/master.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ jobs:
168168
# passthrough CI into build container
169169
build-args: CI=${CI}
170170
context: .
171-
file: docker/Dockerfile
171+
file: build/Dockerfile
172172
load: true
173173
tags: apache/kyuubi:latest
174174
# from https://github.com/marketplace/actions/setup-minikube-kubernetes-cluster

bin/docker-image-tool.sh

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
#!/usr/bin/env bash
2+
3+
# Licensed to the Apache Software Foundation (ASF) under one or more
4+
# contributor license agreements. See the NOTICE file distributed with
5+
# this work for additional information regarding copyright ownership.
6+
# The ASF licenses this file to You under the Apache License, Version 2.0
7+
# (the "License"); you may not use this file except in compliance with
8+
# the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
# This script builds and pushes docker images when run from a release of Kyuubi
20+
# with Kubernetes support.
21+
22+
function error {
23+
echo "$@" 1>&2
24+
exit 1
25+
}
26+
27+
if [ -z "${KYUUBI_HOME}" ]; then
28+
KYUUBI_HOME="$(cd "`dirname "$0"`"/..; pwd)"
29+
fi
30+
31+
CTX_DIR="$KYUUBI_HOME/target/tmp/docker"
32+
33+
function is_dev_build {
34+
[ ! -f "$KYUUBI_HOME/RELEASE" ]
35+
}
36+
37+
function cleanup_ctx_dir {
38+
if is_dev_build; then
39+
rm -rf "$CTX_DIR"
40+
fi
41+
}
42+
trap cleanup_ctx_dir EXIT
43+
44+
function image_ref {
45+
local image="$1"
46+
local add_repo="${2:-1}"
47+
if [ $add_repo = 1 ] && [ -n "$REPO" ]; then
48+
image="$REPO/$image"
49+
fi
50+
if [ -n "$TAG" ]; then
51+
image="$image:$TAG"
52+
fi
53+
echo "$image:$KYUUBI_VERSION"
54+
}
55+
56+
function docker_push {
57+
local image_name="$1"
58+
if [ ! -z $(docker images -q "$(image_ref ${image_name})") ]; then
59+
docker push "$(image_ref ${image_name})"
60+
if [ $? -ne 0 ]; then
61+
error "Failed to push $image_name Docker image."
62+
fi
63+
else
64+
echo "$(image_ref ${image_name}) image not found. Skipping push for this image."
65+
fi
66+
}
67+
68+
function resolve_file {
69+
local FILE=$1
70+
if [ -n "$FILE" ]; then
71+
local DIR=$(dirname $FILE)
72+
DIR=$(cd $DIR && pwd)
73+
FILE="${DIR}/$(basename $FILE)"
74+
fi
75+
echo $FILE
76+
}
77+
78+
# Create a smaller build context for docker in dev builds to make the build faster. Docker
79+
# uploads all of the current directory to the daemon, and it can get pretty big with dev
80+
# builds that contain test log files and other artifacts.
81+
#
82+
# Note: docker does not support symlinks in the build context.
83+
function create_dev_build_context {(
84+
set -e
85+
local BASE_CTX="$CTX_DIR/base"
86+
mkdir -r "$BASE_CTX/docker"
87+
cp -r "docker/" "$BASE_CTX/docker"
88+
89+
cp -r "kyuubi-assembly/target/scala-${KYUUBI_SCALA_VERSION}/jars" "$BASE_CTX/jars"
90+
91+
mkdir -p "$BASE_CTX/externals/engines/spark"
92+
cp "$KYUUBI_HOME/externals/kyuubi-spark-sql-engine/target/kyuubi-spark-sql-engine_${KYUUBI_SCALA_VERSION}-${KYUUBI_VERSION}.jar" "$BASE_CTX/externals/engines/spark"
93+
94+
for other in bin conf; do
95+
cp -r "$other" "$BASE_CTX/$other"
96+
done
97+
)}
98+
99+
function img_ctx_dir {
100+
if is_dev_build; then
101+
echo "$CTX_DIR/$1"
102+
else
103+
echo "$KYUUBI_HOME"
104+
fi
105+
}
106+
107+
function build {
108+
local BUILD_ARGS
109+
local KYUUBI_ROOT="$KYUUBI_HOME"
110+
111+
if is_dev_build; then
112+
create_dev_build_context || error "Failed to create docker build context."
113+
KYUUBI_ROOT="$CTX_DIR/base"
114+
fi
115+
116+
# cp spark for kyuubi as submit client
117+
# if user set -s(spark-provider), use if
118+
# else use builtin spark
119+
if [[ ! -d "$KYUUBI_ROOT/spark-binary" ]]; then
120+
mkdir "$KYUUBI_ROOT/spark-binary"
121+
fi
122+
cp -r "$SPARK_HOME/" "$KYUUBI_ROOT/spark-binary/"
123+
124+
# Verify that the Docker image content directory is present
125+
if [ ! -d "$KYUUBI_ROOT/docker" ]; then
126+
error "Cannot find docker image. This script must be run from a runnable distribution of Apache Kyuubi."
127+
fi
128+
129+
# Verify that Kyuubi has actually been built/is a runnable distribution
130+
# i.e. the Kyuubi JARs that the Docker files will place into the image are present
131+
local TOTAL_JARS=$(ls $KYUUBI_ROOT/jars/kyuubi-* | wc -l)
132+
TOTAL_JARS=$(( $TOTAL_JARS ))
133+
if [ "${TOTAL_JARS}" -eq 0 ]; then
134+
error "Cannot find Kyuubi JARs. This script assumes that Apache Kyuubi has first been built locally or this is a runnable distribution."
135+
fi
136+
137+
local BUILD_ARGS=(${BUILD_PARAMS})
138+
139+
# If a custom Kyuubi_UID was set add it to build arguments
140+
if [ -n "$KYUUBI_UID" ]; then
141+
BUILD_ARGS+=(--build-arg kyuubi_uid=$KYUUBI_UID)
142+
fi
143+
144+
local BINDING_BUILD_ARGS=(
145+
${BUILD_ARGS[@]}
146+
--build-arg
147+
base_img=$(image_ref kyuubi)
148+
)
149+
150+
local BASEDOCKERFILE=${BASEDOCKERFILE:-"docker/Dockerfile"}
151+
local ARCHS=${ARCHS:-"--platform linux/amd64,linux/arm64"}
152+
153+
(cd $(img_ctx_dir base) && docker build $NOCACHEARG "${BUILD_ARGS[@]}" \
154+
-t $(image_ref kyuubi) \
155+
-f "$BASEDOCKERFILE" .)
156+
if [ $? -ne 0 ]; then
157+
error "Failed to build Kyuubi JVM Docker image, please refer to Docker build output for details."
158+
fi
159+
if [ "${CROSS_BUILD}" != "false" ]; then
160+
(cd $(img_ctx_dir base) && docker buildx build $ARCHS $NOCACHEARG "${BUILD_ARGS[@]}" --push \
161+
-t $(image_ref kyuubi) \
162+
-f "$BASEDOCKERFILE" .)
163+
fi
164+
}
165+
166+
function push {
167+
docker_push "kyuubi"
168+
}
169+
170+
function usage {
171+
cat <<EOF
172+
Usage: $0 [options] [command]
173+
Builds or pushes the built-in Kyuubi Docker image.
174+
175+
Commands:
176+
build Build image. Requires a repository address to be provided if the image will be
177+
pushed to a different registry.
178+
push Push a pre-built image to a registry. Requires a repository address to be provided.
179+
180+
Options:
181+
-f Dockerfile to build for JVM based Jobs. By default builds the Dockerfile shipped with Kyuubi.
182+
-r Repository address.
183+
-t Tag to apply to the built image, or to identify the image to be pushed.
184+
-n Build docker image with --no-cache
185+
-u UID to use in the USER directive to set the user the main Kyuubi process runs as inside the
186+
resulting container
187+
-X Use docker buildx to cross build. Automatically pushes.
188+
See https://docs.docker.com/buildx/working-with-buildx/ for steps to setup buildx.
189+
-b Build arg to build or push the image. For multiple build args, this option needs to
190+
be used separately for each build arg.
191+
-s Put the specified Spark into the Kyuubi image to be used as the internal SPARK_HOME
192+
of the container.
193+
194+
Examples:
195+
196+
- Build and push image with tag "v1.4.0" to docker.io/myrepo
197+
$0 -r docker.io/myrepo -t v1.4.0 build
198+
$0 -r docker.io/myrepo -t v1.4.0 push
199+
200+
- Build and push with tag "v3.0.0" and Spark-3.1.2 as base image to docker.io/myrepo
201+
$0 -r docker.io/myrepo -t v3.0.0 -b BASE_IMAGE=repo/spark:3.1.2 build
202+
$0 -r docker.io/myrepo -t v3.0.0 push
203+
204+
- Build and push for multiple archs to docker.io/myrepo
205+
$0 -r docker.io/myrepo -t v3.0.0 -X build
206+
207+
# Note: buildx, which does cross building, needs to do the push during build
208+
# So there is no separate push step with -X
209+
210+
- Build with Spark placed "/path/spark"
211+
$0 -s /path/spark build
212+
213+
EOF
214+
}
215+
216+
if [[ "$@" = *--help ]] || [[ "$@" = *-h ]]; then
217+
usage
218+
exit 0
219+
fi
220+
221+
REPO=
222+
TAG=
223+
BASEDOCKERFILE=
224+
NOCACHEARG=
225+
BUILD_PARAMS=
226+
KYUUBI_UID=
227+
CROSS_BUILD="false"
228+
while getopts f:r:t:Xnb:u:s: option
229+
do
230+
case "${option}"
231+
in
232+
f) BASEDOCKERFILE=$(resolve_file ${OPTARG});;
233+
r) REPO=${OPTARG};;
234+
t) TAG=${OPTARG};;
235+
n) NOCACHEARG="--no-cache";;
236+
b) BUILD_PARAMS=${BUILD_PARAMS}" --build-arg "${OPTARG};;
237+
X) CROSS_BUILD=1;;
238+
u) KYUUBI_UID=${OPTARG};;
239+
s) SPARK_HOME=${OPTARG};;
240+
esac
241+
done
242+
243+
. "${KYUUBI_HOME}/bin/load-kyuubi-env.sh"
244+
case "${@: -1}" in
245+
build)
246+
build
247+
;;
248+
push)
249+
if [ -z "$REPO" ]; then
250+
usage
251+
exit 1
252+
fi
253+
push
254+
;;
255+
*)
256+
usage
257+
exit 1
258+
;;
259+
esac

bin/load-kyuubi-env.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,16 @@ fi
7171
export KYUUBI_SCALA_VERSION="${KYUUBI_SCALA_VERSION:-"2.12"}"
7272

7373
if [[ -f ${KYUUBI_HOME}/RELEASE ]]; then
74+
KYUUBI_VERSION="$(grep "Kyuubi " "$KYUUBI_HOME/RELEASE" | awk -F ' ' '{print $2}')"
7475
SPARK_VERSION_BUILD="$(grep "Spark " "$KYUUBI_HOME/RELEASE" | awk -F ' ' '{print $2}')"
7576
HADOOP_VERSION_BUILD="$(grep "Hadoop " "$KYUUBI_HOME/RELEASE" | awk -F ' ' '{print $2}')"
7677
SPARK_BUILTIN="${KYUUBI_HOME}/externals/spark-$SPARK_VERSION_BUILD-bin-hadoop${HADOOP_VERSION_BUILD:0:3}"
7778
else
7879
MVN="${MVN:-"${KYUUBI_HOME}/build/mvn"}"
80+
KYUUBI_VERSION=$("$MVN" help:evaluate -Dexpression=project.version 2>/dev/null\
81+
| grep -v "INFO"\
82+
| grep -v "WARNING"\
83+
| tail -n 1)
7984
SPARK_VERSION_BUILD=$("$MVN" help:evaluate -Dexpression=spark.version 2>/dev/null\
8085
| grep -v "INFO"\
8186
| grep -v "WARNING"\
@@ -94,6 +99,7 @@ if [ $silent -eq 0 ]; then
9499
echo "JAVA_HOME: ${JAVA_HOME}"
95100

96101
echo "KYUUBI_HOME: ${KYUUBI_HOME}"
102+
echo "KYUUBI_VERSION: ${KYUUBI_VERSION}"
97103
echo "KYUUBI_CONF_DIR: ${KYUUBI_CONF_DIR}"
98104
echo "KYUUBI_LOG_DIR: ${KYUUBI_LOG_DIR}"
99105
echo "KYUUBI_PID_DIR: ${KYUUBI_PID_DIR}"

build/Dockerfile

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
# Usage:
19+
# Run the docker command below
20+
# docker build \
21+
# --build-arg MVN_ARG="-Pspark-3.1,spark-hadoop-3.2" \
22+
# --file build/Dockerfile \
23+
# --tag apache/kyuubi:tagname \
24+
# .
25+
# Options:
26+
# -f, --file this docker file
27+
# -t, --tag the target repo and tag name
28+
# more options can be found with -h, --help
29+
30+
# Declare the BASE_IMAGE argument in the first line, for more detail
31+
# see: https://github.com/moby/moby/issues/38379
32+
ARG BASE_IMAGE=openjdk:8-jdk
33+
34+
FROM maven:3.6-jdk-8 as builder
35+
36+
ARG MVN_ARG
37+
38+
# Pass the environment variable `CI` into container, for internal use only.
39+
#
40+
# Continuous integration(aka. CI) services like GitHub Actions, Travis always provide
41+
# an environment variable `CI` in runners, and we detect this variable to run some
42+
# specific actions, e.g. run `mvn` in batch mode to suppress noisy logs.
43+
ARG CI
44+
ENV CI ${CI}
45+
46+
ADD . /workspace/kyuubi
47+
WORKDIR /workspace/kyuubi
48+
49+
RUN ./build/dist ${MVN_ARG} && \
50+
mv /workspace/kyuubi/dist /opt/kyuubi && \
51+
# Removing stuff saves time because docker creates a temporary layer
52+
rm -rf ~/.m2 && \
53+
rm -rf /workspace/kyuubi
54+
55+
FROM ${BASE_IMAGE}
56+
57+
ARG kyuubi_uid=10009
58+
59+
USER root
60+
61+
ENV KYUUBI_HOME /opt/kyuubi
62+
ENV KYUUBI_LOG_DIR ${KYUUBI_HOME}/logs
63+
ENV KYUUBI_PID_DIR ${KYUUBI_HOME}/pid
64+
ENV KYUUBI_WORK_DIR_ROOT ${KYUUBI_HOME}/work
65+
66+
COPY --from=builder /opt/kyuubi ${KYUUBI_HOME}
67+
68+
RUN set -ex && \
69+
apt-get update && \
70+
DEBIAN_FRONTEND=noninteractive \
71+
apt install -y bash tini libc6 libpam-modules krb5-user libnss3 procps && \
72+
useradd -u ${kyuubi_uid} -g root kyuubi && \
73+
mkdir -p ${KYUUBI_HOME} ${KYUUBI_LOG_DIR} ${KYUUBI_PID_DIR} ${KYUUBI_WORK_DIR_ROOT} && \
74+
chmod ug+rw -R ${KYUUBI_HOME} && \
75+
chmod a+rwx -R ${KYUUBI_WORK_DIR_ROOT} && \
76+
rm -rf /var/cache/apt/*
77+
78+
WORKDIR ${KYUUBI_HOME}
79+
80+
CMD [ "./bin/kyuubi", "run" ]
81+
82+
USER ${kyuubi_uid}

0 commit comments

Comments
 (0)