121 lines
4.7 KiB
Bash
121 lines
4.7 KiB
Bash
|
|
#!/bin/bash
|
||
|
|
[ "$1" == "-h" ] && echo "install-containerd.sh install|update|remove|restart|scripts|config|reinstall" && exit 1
|
||
|
|
|
||
|
|
SERVICE_NAME="containerd"
|
||
|
|
SCRIPTS_DEPLOY_PATH="/etc/containerd"
|
||
|
|
|
||
|
|
[ -r "env-containerd" ] && . ./env-containerd
|
||
|
|
[ -r "common.sh" ] && . ./common.sh
|
||
|
|
|
||
|
|
ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')"
|
||
|
|
OS="$(uname | tr '[:upper:]' '[:lower:]')"
|
||
|
|
CONTAINERD_VERSION="${CONTAINERD_VERSION:-1.7.24}"
|
||
|
|
CONTAINERD_URL="https://github.com/containerd/containerd/releases/download/v${CONTAINERD_VERSION}/containerd-${CONTAINERD_VERSION}-${OS}-${ARCH}.tar.gz"
|
||
|
|
CRICTL_VERSION="${CRICTL_VERSION:-1.35.0}"
|
||
|
|
CRICTL_URL="https://github.com/kubernetes-sigs/cri-tools/releases/download"
|
||
|
|
CMD_TSK=${CMD_TSK:-${1:-install}}
|
||
|
|
export LC_CTYPE=C.UTF-8 LANG=C.UTF-8
|
||
|
|
|
||
|
|
ORG=$(pwd)
|
||
|
|
|
||
|
|
_clean_others() {
|
||
|
|
[ -d "/etc/cni" ] && sudo rm -r /etc/cni
|
||
|
|
[ -d "/var/lib/containers" ] && sudo rm -r /var/lib/containers
|
||
|
|
sudo rm -f /etc/systemd/system/podman* 2>/dev/null
|
||
|
|
}
|
||
|
|
|
||
|
|
_update_binary() {
|
||
|
|
[ -z "$CONTAINERD_VERSION" ] && return 1
|
||
|
|
local curr_vers has_containerd
|
||
|
|
has_containerd=$(type containerd 2>/dev/null)
|
||
|
|
if [ -n "$has_containerd" ]; then
|
||
|
|
curr_vers=$(containerd --version | awk '{print $3}' | sed 's/v//g')
|
||
|
|
else
|
||
|
|
_clean_others
|
||
|
|
fi
|
||
|
|
if [ "$curr_vers" != "$CONTAINERD_VERSION" ]; then
|
||
|
|
if ! curl -fsSL "$CONTAINERD_URL" -o /tmp/containerd.tar.gz; then
|
||
|
|
echo "error downloading containerd"; return 1
|
||
|
|
fi
|
||
|
|
tar xzf /tmp/containerd.tar.gz
|
||
|
|
if [ -r "bin/containerd" ]; then
|
||
|
|
_common_service_stop
|
||
|
|
cd bin || return 1
|
||
|
|
sudo cp * /usr/local/bin
|
||
|
|
cd "$ORG" || return 1
|
||
|
|
else
|
||
|
|
echo "error installing containerd"; return 1
|
||
|
|
fi
|
||
|
|
rm -fr bin /tmp/containerd.tar.gz
|
||
|
|
fi
|
||
|
|
local crictl_vers
|
||
|
|
crictl_vers=$(crictl --version 2>/dev/null | awk '{print $3}' | sed 's/v//g')
|
||
|
|
if [ "$crictl_vers" != "$CRICTL_VERSION" ]; then
|
||
|
|
if ! curl -fsSL "${CRICTL_URL}/v${CRICTL_VERSION}/crictl-v${CRICTL_VERSION}-${OS}-${ARCH}.tar.gz" -o /tmp/crictl.tar.gz; then
|
||
|
|
echo "error downloading crictl"; return 1
|
||
|
|
fi
|
||
|
|
tar xzf /tmp/crictl.tar.gz
|
||
|
|
[ -r "crictl" ] && chmod +x crictl && sudo mv crictl /usr/local/bin
|
||
|
|
rm -f /tmp/crictl.tar.gz
|
||
|
|
fi
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
_config_containerd() {
|
||
|
|
[ ! -d "/etc/containerd" ] && sudo mkdir -p /etc/containerd
|
||
|
|
# Always regenerate — Hetzner packages leave stale configs with SystemdCgroup=false
|
||
|
|
# and k8s.gcr.io sandbox_image that cause kubeadm preflight failures.
|
||
|
|
sudo containerd config default | sudo tee /etc/containerd/config.toml >/dev/null
|
||
|
|
sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
|
||
|
|
sudo sed -i 's|sandbox_image = ".*pause:.*"|sandbox_image = "registry.k8s.io/pause:3.10"|' /etc/containerd/config.toml
|
||
|
|
local youki_path
|
||
|
|
youki_path=$(type -p youki 2>/dev/null)
|
||
|
|
if [ -n "$youki_path" ] && [ -x "$youki_path" ]; then
|
||
|
|
grep -q "youki" /etc/containerd/config.toml || cat >> /etc/containerd/config.toml <<EOF
|
||
|
|
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.youki]
|
||
|
|
runtime_type = "io.containerd.runc.v2"
|
||
|
|
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.youki.options]
|
||
|
|
BinaryName = "${youki_path}"
|
||
|
|
EOF
|
||
|
|
fi
|
||
|
|
if [ -r "crictl.yaml" ] && [ ! -r "/etc/crictl.yaml" ]; then
|
||
|
|
sudo cp crictl.yaml /etc/containerd-crictl.yaml
|
||
|
|
sudo cp crictl.yaml /etc/crictl.yaml
|
||
|
|
fi
|
||
|
|
if [ -r "containerd.service" ] && [ ! -f "/lib/systemd/system/containerd.service" ]; then
|
||
|
|
sudo cp containerd.service /lib/systemd/system
|
||
|
|
fi
|
||
|
|
sudo timeout -k 10 20 systemctl daemon-reload
|
||
|
|
for mod in overlay br_netfilter; do
|
||
|
|
sudo grep -q "^${mod}" /etc/modules-load.d/containerd.conf 2>/dev/null \
|
||
|
|
|| echo "$mod" | sudo tee -a /etc/modules-load.d/containerd.conf
|
||
|
|
done
|
||
|
|
_common_service_start
|
||
|
|
}
|
||
|
|
|
||
|
|
case "$CMD_TSK" in
|
||
|
|
reinstall)
|
||
|
|
_common_service_stop
|
||
|
|
sudo rm -f /usr/local/bin/containerd /usr/local/bin/containerd-shim* ;&
|
||
|
|
install)
|
||
|
|
_update_binary || { echo "error containerd install"; exit 1; }
|
||
|
|
_config_containerd
|
||
|
|
;;
|
||
|
|
update) _common_update ;;
|
||
|
|
scripts) _common_deploy_scripts ;;
|
||
|
|
config) _config_containerd ;;
|
||
|
|
restart) _common_service_restart ;;
|
||
|
|
remove) _common_service_stop ;;
|
||
|
|
delete)
|
||
|
|
sudo systemctl stop containerd >/dev/null 2>&1 || true
|
||
|
|
sudo systemctl disable containerd >/dev/null 2>&1 || true
|
||
|
|
sudo rm -f /usr/local/bin/containerd /usr/local/bin/containerd-shim* /usr/local/bin/ctr
|
||
|
|
sudo rm -f /usr/local/bin/crictl /etc/crictl.yaml /etc/containerd-crictl.yaml
|
||
|
|
sudo rm -f /etc/modules-load.d/containerd.conf
|
||
|
|
sudo rm -rf /var/lib/containerd /run/containerd
|
||
|
|
sudo rm -f /lib/systemd/system/containerd.service /etc/systemd/system/containerd.service
|
||
|
|
sudo systemctl daemon-reload >/dev/null 2>&1
|
||
|
|
sudo rm -rf /etc/containerd
|
||
|
|
;;
|
||
|
|
esac
|