From fdc5f3361303d72fd3c4ef917415dd8a7bbcf3c9 Mon Sep 17 00:00:00 2001 From: Shiming Zhang Date: Tue, 26 Mar 2024 16:46:48 +0800 Subject: [PATCH] Refactor the all-in-one image --- images/cluster/Dockerfile | 10 ++- images/cluster/entrypoint.sh | 61 ++++++++----------- site/content/en/docs/user/all-in-one-image.md | 47 ++++++-------- 3 files changed, 52 insertions(+), 66 deletions(-) diff --git a/images/cluster/Dockerfile b/images/cluster/Dockerfile index 0de41500b..14aa4272a 100644 --- a/images/cluster/Dockerfile +++ b/images/cluster/Dockerfile @@ -19,7 +19,9 @@ COPY --chmod=0755 bin/$TARGETPLATFORM/kwok /usr/local/bin/ COPY --chmod=0755 bin/$TARGETPLATFORM/kwokctl /usr/local/bin/ ENV KWOK_CONTROLLER_BINARY /usr/local/bin/kwok -ENV KWOK_KUBE_APISERVER_PORT 8080 +ENV KWOK_KUBE_APISERVER_INSECURE_PORT 8080 +ENV KWOK_KUBE_APISERVER_PORT 0 +ENV KWOK_RUNTIME binary ARG kube_version ENV KWOK_KUBE_VERSION $kube_version @@ -41,9 +43,13 @@ COPY --chmod=0755 bin/$TARGETPLATFORM/kwok /usr/local/bin/ COPY --chmod=0755 bin/$TARGETPLATFORM/kwokctl /usr/local/bin/ ENV KWOK_CONTROLLER_BINARY /usr/local/bin/kwok -ENV KWOK_KUBE_APISERVER_PORT 8080 +ENV KWOK_KUBE_APISERVER_INSECURE_PORT 8080 +ENV KWOK_KUBE_APISERVER_PORT 0 +ENV KWOK_RUNTIME binary ARG kube_version ENV KWOK_KUBE_VERSION $kube_version +EXPOSE 8080 + ENTRYPOINT ["/entrypoint.sh"] diff --git a/images/cluster/entrypoint.sh b/images/cluster/entrypoint.sh index 5418eb916..6d78c9726 100755 --- a/images/cluster/entrypoint.sh +++ b/images/cluster/entrypoint.sh @@ -13,45 +13,32 @@ # See the License for the specific language governing permissions and # limitations under the License. +export ALIVE=true + +catch_exit() { + ALIVE=false + kwokctl stop cluster || true + exit 0 +} + +keep_alive() { + while [ "${ALIVE}" = "true" ]; do + if ! output="$(kwokctl start cluster 2>&1)"; then + echo "Failed to start cluster: ${output}" + fi + sleep 10 + done +} + show_info() { - sleep 1 - - # Print some useful information - echo "###############################################################################" - echo "> kubectl -s :${KWOK_KUBE_APISERVER_PORT} version" - kwokctl kubectl version || true - - echo "###############################################################################" - echo "# The following kubeconfig can be used to connect to the Kubernetes API server" - cat < kubectl -s :${KWOK_KUBE_APISERVER_PORT} get ns" - kwokctl kubectl get ns || true - - echo "###############################################################################" - echo "# The above example works if your host's port is the same as the container's," - echo "# otherwise, change it to your host's port" + echo "Start cluster and keep alive at 0.0.0.0:${KWOK_KUBE_APISERVER_INSECURE_PORT}" + echo "See more https://kwok.sigs.k8s.io/docs/user/all-in-one-image/" } -# Create a cluster -KWOK_KUBE_APISERVER_PORT=0 kwokctl create cluster "$@" || exit 1 +trap catch_exit EXIT + +kwokctl create cluster "$@" || exit 1 -show_info & +show_info -# Start a proxy to the Kubernetes API server -kwokctl kubectl proxy --port="${KWOK_KUBE_APISERVER_PORT}" --accept-hosts='^*$' --address="0.0.0.0" +keep_alive diff --git a/site/content/en/docs/user/all-in-one-image.md b/site/content/en/docs/user/all-in-one-image.md index 80067df2c..043ecfbc9 100644 --- a/site/content/en/docs/user/all-in-one-image.md +++ b/site/content/en/docs/user/all-in-one-image.md @@ -16,24 +16,27 @@ This document walks through the steps to create a cluster with the all-in-one im docker run --rm -it -p 8080:8080 registry.k8s.io/kwok/cluster:v0.4.0-k8s.v1.28.0 ``` +### Quick Verification + +You can use the `kubectl` with the `-s` option to connect to the cluster. + +``` bash +kubectl -s :8080 get ns +``` + ``` log -Cluster is creating cluster=kwok -Cluster is created elapsed=0.3s cluster=kwok -Cluster is starting cluster=kwok -Cluster is started elapsed=0.7s cluster=kwok -You can now use your cluster with: - - kubectl cluster-info --context kwok-kwok - -Thanks for using kwok! -Starting to serve on [::]:8080 -############################################################################### -> kubectl -s :8080 version -Client Version: v1.28.0 -Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3 -Server Version: v1.28.0 -############################################################################### -# The following kubeconfig can be used to connect to the Kubernetes API server +NAME STATUS AGE +default Active 1s +kube-node-lease Active 1s +kube-public Active 1s +kube-system Active 1s +``` + +### Setting up kubeconfig + +You can set up the `kubeconfig` file to connect to the cluster. + +``` bash apiVersion: v1 clusters: - cluster: @@ -47,16 +50,6 @@ current-context: kwok kind: Config preferences: {} users: null -############################################################################### -> kubectl -s :8080 get ns -NAME STATUS AGE -default Active 1s -kube-node-lease Active 1s -kube-public Active 1s -kube-system Active 1s -############################################################################### -# The above example works if your host's port is the same as the container's, -# otherwise, change it to your host's port ``` ## Use in a pod