From a58d30444172148d52cd21aa33c9aaece53eed20 Mon Sep 17 00:00:00 2001 From: starComingup <1225067236@qq.com> Date: Fri, 12 Aug 2022 17:16:46 +0800 Subject: [PATCH] add k0s rootfs Signed-off-by: starComingup <1225067236@qq.com> --- context/k0s/rootfs/etc/dump-config.toml | 6 ++ context/k0s/rootfs/etc/registry.yml | 4 + context/k0s/rootfs/scripts/containerd.sh | 49 +++++++++++ context/k0s/rootfs/scripts/init-registry.sh | 94 +++++++++++++++++++++ context/k0s/rootfs/scripts/init.sh | 29 +++++++ 5 files changed, 182 insertions(+) create mode 100644 context/k0s/rootfs/etc/dump-config.toml create mode 100644 context/k0s/rootfs/etc/registry.yml create mode 100644 context/k0s/rootfs/scripts/containerd.sh create mode 100644 context/k0s/rootfs/scripts/init-registry.sh create mode 100644 context/k0s/rootfs/scripts/init.sh diff --git a/context/k0s/rootfs/etc/dump-config.toml b/context/k0s/rootfs/etc/dump-config.toml new file mode 100644 index 0000000..d276614 --- /dev/null +++ b/context/k0s/rootfs/etc/dump-config.toml @@ -0,0 +1,6 @@ +version = 2 +[plugins] + [plugins."io.containerd.grpc.v1.cri"] + sandbox_image = "sea.hub:5000/pause:3.7" + [plugins."io.containerd.grpc.v1.cri".registry] + config_path = "/etc/docker/certs.d/" \ No newline at end of file diff --git a/context/k0s/rootfs/etc/registry.yml b/context/k0s/rootfs/etc/registry.yml new file mode 100644 index 0000000..67d38f5 --- /dev/null +++ b/context/k0s/rootfs/etc/registry.yml @@ -0,0 +1,4 @@ +domain: sea.hub +port: "5000" +username: "" +password: "" diff --git a/context/k0s/rootfs/scripts/containerd.sh b/context/k0s/rootfs/scripts/containerd.sh new file mode 100644 index 0000000..1bd46ba --- /dev/null +++ b/context/k0s/rootfs/scripts/containerd.sh @@ -0,0 +1,49 @@ +#!/bin/bash +# Copyright © 2021 Alibaba Group Holding Ltd. +# +# Licensed 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. + +set -x +set -e + +# if [ $(systemctl status containerd > /dev/null 2>&1; echo $?) != 0 ]; then +# tar -xvzf ../cri/containerd.tar.gz -C / +# cp -rf ../lib64/lib* /usr/lib64/ +# systemctl enable containerd.service +# systemctl restart containerd.service +# fi + +rootfs=$(dirname "$(pwd)") +image_dir="$rootfs/images" +dump_config_dir="$rootfs/etc/dump-config.toml" +load_images() { +for image in "$image_dir"/* +do + if [ -f "${image}" ] + then + nerdctl load -i "${image}" + fi +done +} + +# mkdir -p /etc/containerd + +sed -i "s/sea.hub/${1:-sea.hub}/g" "$dump_config_dir" +sed -i "s/5000/${2:-5000}/g" "$dump_config_dir" + +#add cri sandbox image and sea.hub registry cert path +##sandbox_image = "sea.hub:5000/pause:3.6" custom setup +containerd --config "$dump_config_dir" config dump > /etc/containerd/config.toml + +systemctl restart containerd.service +load_images diff --git a/context/k0s/rootfs/scripts/init-registry.sh b/context/k0s/rootfs/scripts/init-registry.sh new file mode 100644 index 0000000..d937d69 --- /dev/null +++ b/context/k0s/rootfs/scripts/init-registry.sh @@ -0,0 +1,94 @@ +#!/bin/bash +# Copyright © 2021 Alibaba Group Holding Ltd. +# +# Licensed 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. + + +set -e +set -x +# prepare registry storage as directory +cd "$(dirname "$0")" + +# shellcheck disable=SC2034 +REGISTRY_PORT=${1-5000} +VOLUME=${2-/var/lib/registry} +REGISTRY_DOMAIN=${3-sea.hub} + +container=sealer-registry +rootfs=$(dirname "$(pwd)") +config="$rootfs/etc/registry_config.yml" +htpasswd="$rootfs/etc/registry_htpasswd" +certs_dir="$rootfs/certs" + +mkdir -p "$VOLUME" || true + +startRegistry() { + n=1 + while (( n <= 3 )) + do + echo "attempt to start registry" + # shellcheck disable=SC2106 + (nerdctl start $container && break) || (( n < 3)) + (( n++ )) + sleep 3 + done +} + +check_registry() { + n=1 + while (( n <= 3 )) + do + (nerdctl inspect sealer-registry | grep "\"Status\": \"running\"") && break + if [[ $n -eq 3 ]]; then + # shellcheck disable=SC2154 + echo "sealer-registry is not running, status: $registry_status" + exit 1 + fi + (( n++ )) + sleep 3 + done +} + + +## rm container if exist. +! nerdctl ps -a |grep sealer-registry || nerdctl rmi -f sealer-registry +## +rm -rf /var/lib/nerdctl/1935db59/names/default/$container + +regArgs="-d --restart=always \ +--net=host \ +--name $container \ +-v $certs_dir:/certs \ +-v $VOLUME:/var/lib/registry \ +-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/$REGISTRY_DOMAIN.crt \ +-e REGISTRY_HTTP_TLS_KEY=/certs/$REGISTRY_DOMAIN.key" + +if [ -f "$config" ]; then + sed -i "s/5000/$1/g" "$config" + regArgs="$regArgs \ + -v $config:/etc/docker/registry/config.yml" +fi + +if [ -f "$htpasswd" ]; then + # shellcheck disable=SC2086 + nerdctl run $regArgs \ + -v $htpasswd:/htpasswd \ + -e REGISTRY_AUTH=htpasswd \ + -e REGISTRY_AUTH_HTPASSWD_PATH=/htpasswd \ + -e REGISTRY_AUTH_HTPASSWD_REALM="Registry Realm" registry:2.7.1 || startRegistry +else + nerdctl run "$regArgs" registry:2.7.1 || startRegistry +fi + +sleep 1 +check_registry \ No newline at end of file diff --git a/context/k0s/rootfs/scripts/init.sh b/context/k0s/rootfs/scripts/init.sh new file mode 100644 index 0000000..9217473 --- /dev/null +++ b/context/k0s/rootfs/scripts/init.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# Copyright © 2021 Alibaba Group Holding Ltd. +# +# Licensed 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. + +set -e +set -x + +#STORAGE=${1:-/var/lib/docker} compatible docker +REGISTRY_DOMAIN=${2-sea.hub} +REGISTRY_PORT=${3-5000} + +chmod -R 755 ../bin/* +chmod 644 ../bin +cp ../bin/* /usr/bin + +# Install containerd +chmod a+x containerd.sh +/bin/bash containerd.sh "$REGISTRY_DOMAIN" "$REGISTRY_PORT"