Skip to content

Commit

Permalink
Merge pull request #1006 from wzshiming/clean/cluster-image
Browse files Browse the repository at this point in the history
Refactor the all-in-one image and keep it alive
  • Loading branch information
wzshiming authored Sep 23, 2024
2 parents 50246cc + fdc5f33 commit 0e9df27
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 66 deletions.
10 changes: 8 additions & 2 deletions images/cluster/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"]
61 changes: 24 additions & 37 deletions images/cluster/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOF
apiVersion: v1
clusters:
- cluster:
server: http://127.0.0.1:${KWOK_KUBE_APISERVER_PORT}
name: kwok
contexts:
- context:
cluster: kwok
name: kwok
current-context: kwok
kind: Config
preferences: {}
users: null
EOF

echo "###############################################################################"
echo "> 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
47 changes: 20 additions & 27 deletions site/content/en/docs/user/all-in-one-image.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit 0e9df27

Please sign in to comment.