update
This commit is contained in:
31
nerdctl/README.md
Normal file
31
nerdctl/README.md
Normal file
@@ -0,0 +1,31 @@
|
||||
## 配置 containerd 镜像
|
||||
|
||||
```shell
|
||||
sudo bash ./setup_containerd_mirror.sh
|
||||
```
|
||||
|
||||
## 更改containerd 的镜像存储位置
|
||||
|
||||
```shell
|
||||
|
||||
```
|
||||
|
||||
## 镜像使用
|
||||
|
||||
### 手动拉取镜像的命令
|
||||
|
||||
直接使用nerdctl pull 来拉取镜像总会用docker.io官方的源,所以这里直接使用ctr拉取镜像到指定空间。
|
||||
|
||||
sudo ctr --namespace=k8s.io image pull --hosts-dir /etc/containerd/certs.d docker.io/library/alpine:latest
|
||||
sudo ctr --namespace=k8s.io image pull --hosts-dir /etc/containerd/certs.d docker.io/library/alpine:latest
|
||||
|
||||
### 使用nerdctl 命令查看 ctr拉取的镜像。nerdctl 无法直接拉取镜像。
|
||||
|
||||
sudo nerdctl --namespace k8s.io images
|
||||
sudo nerdctl --namespace default images
|
||||
|
||||
### 删除所有镜像
|
||||
|
||||
sudo nerdctl --namespace k8s.io image prune -a
|
||||
sudo nerdctl --namespace default image prune -a
|
||||
sudo ctr --namespace=k8s.io images ls -q
|
||||
250
nerdctl/install_nerdctl_full_alluser.sh
Executable file
250
nerdctl/install_nerdctl_full_alluser.sh
Executable file
@@ -0,0 +1,250 @@
|
||||
#!/bin/bash
|
||||
|
||||
VERSION="2.0.0"
|
||||
FILENAME="nerdctl-full-${VERSION}-linux-amd64.tar.gz"
|
||||
# https://github.com/containerd/nerdctl/releases/download/v2.0.0/nerdctl-full-2.0.0-linux-amd64.tar.gz
|
||||
DOWNLOAD_URL="https://ghproxy.dockless.eu.org/https://github.com/containerd/nerdctl/releases/download/v${VERSION}/${FILENAME}"
|
||||
|
||||
install_nerdctl() {
|
||||
echo "正在安装必要的依赖包..."
|
||||
|
||||
# 删除 Docker 源
|
||||
if [ -f /etc/apt/sources.list.d/docker.list ]; then
|
||||
echo "删除 Docker APT 源..."
|
||||
sudo rm /etc/apt/sources.list.d/docker.list
|
||||
fi
|
||||
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y wget tar jq curl gnupg lsb-release uidmap
|
||||
|
||||
echo "下载 nerdctl-full..."
|
||||
wget ${DOWNLOAD_URL}
|
||||
|
||||
echo "解压并移动到 /usr/local..."
|
||||
sudo tar -C /usr/local -xzf ${FILENAME}
|
||||
|
||||
echo "启动并启用 containerd 服务..."
|
||||
sudo systemctl enable --now containerd
|
||||
|
||||
echo "验证安装..."
|
||||
nerdctl --version
|
||||
|
||||
# echo "清理下载文件..."
|
||||
# rm ${FILENAME}
|
||||
|
||||
echo "安装 nerdctl bash 自动补全..."
|
||||
sudo apt install bash-completion -y
|
||||
nerdctl completion bash > /etc/bash_completion.d/nerdctl
|
||||
nerdctl completion bash > /etc/bash_completion.d/docker
|
||||
source /etc/bash_completion.d/nerdctl
|
||||
source /etc/bash_completion.d/docker
|
||||
|
||||
echo "创建 docker 兼容命令..."
|
||||
sudo tee /usr/local/bin/docker > /dev/null <<EOT
|
||||
#!/bin/bash
|
||||
exec nerdctl "\$@"
|
||||
EOT
|
||||
sudo chmod +x /usr/local/bin/docker
|
||||
|
||||
echo "普通用户安装"
|
||||
/usr/local/bin/containerd-rootless-setuptool.sh install
|
||||
/usr/local/bin/containerd-rootless-setuptool.sh check
|
||||
nerdctl --version
|
||||
|
||||
echo "nerdctl-full 安装完成。"
|
||||
}
|
||||
|
||||
uninstall_nerdctl() {
|
||||
echo "停止 containerd 服务..."
|
||||
sudo systemctl disable --now containerd
|
||||
|
||||
echo "删除 nerdctl 二进制文件..."
|
||||
sudo rm -rf /usr/local/bin/nerdctl
|
||||
sudo rm -rf /usr/local/libexec/cni
|
||||
|
||||
echo "删除 containerd..."
|
||||
sudo rm -rf /usr/local/bin/containerd*
|
||||
sudo rm -rf /usr/local/bin/ctr
|
||||
sudo rm -rf /usr/local/bin/runc
|
||||
|
||||
echo "删除相关文件和目录..."
|
||||
sudo rm -rf /etc/containerd
|
||||
sudo rm -rf /var/lib/containerd
|
||||
sudo rm -rf /usr/local/libexec/cni
|
||||
|
||||
echo "删除 buildkit 服务和 socket 文件..."
|
||||
sudo systemctl stop buildkit
|
||||
sudo systemctl stop buildkit.socket
|
||||
sudo systemctl disable buildkit
|
||||
sudo systemctl disable buildkit.socket
|
||||
sudo rm -f /etc/systemd/system/buildkit.service
|
||||
sudo rm -f /etc/systemd/system/buildkit.socket
|
||||
sudo rm -f /usr/lib/systemd/system/buildkit.service
|
||||
sudo rm -f /usr/lib/systemd/system/buildkit.socket
|
||||
|
||||
echo "删除 nvidia-container-toolkit..."
|
||||
sudo apt-get remove --purge -y nvidia-container-toolkit
|
||||
|
||||
echo "删除 nerdctl bash 自动补全..."
|
||||
sudo rm -f /etc/bash_completion.d/nerdctl
|
||||
sudo rm -f /etc/bash_completion.d/docker
|
||||
|
||||
echo "删除 docker 兼容命令..."
|
||||
sudo rm -f /usr/local/bin/docker
|
||||
|
||||
echo "清理完成。"
|
||||
}
|
||||
|
||||
install_nvidia_docker() {
|
||||
echo "正在安装 NVIDIA Docker..."
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y curl gnupg lsb-release
|
||||
|
||||
if ! grep -q "^deb .\+nvidia-container-toolkit" /etc/apt/sources.list /etc/apt/sources.list.d/*; then
|
||||
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
|
||||
&& curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
|
||||
sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
|
||||
sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
|
||||
else
|
||||
echo "NVIDIA Docker 源已经配置,跳过此步骤。"
|
||||
fi
|
||||
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y nvidia-container-toolkit
|
||||
}
|
||||
|
||||
configure_containerd() {
|
||||
echo "正在配置 containerd 使用 NVIDIA runtime..."
|
||||
|
||||
# 创建 /etc/containerd 目录并生成默认配置文件
|
||||
sudo mkdir -p /etc/containerd
|
||||
# containerd config default > /etc/containerd/config.toml
|
||||
containerd config default | sudo tee /etc/containerd/config.toml
|
||||
|
||||
# 使用 sed 命令在特定位置添加配置项
|
||||
sudo sed -i '/\[plugins."io.containerd.grpc.v1.cri".registry.configs\]/a \\t[plugins."io.containerd.grpc.v1.cri".registry.configs."docker.io".tls]\n\t\tinsecure_skip_verify = true' /etc/containerd/config.toml
|
||||
|
||||
# 配置 NVIDIA runtime
|
||||
sudo nvidia-ctk runtime configure --runtime=containerd
|
||||
|
||||
# 重启 containerd 服务
|
||||
sudo systemctl restart containerd
|
||||
}
|
||||
|
||||
start_containerd() {
|
||||
echo "正在启动 containerd..."
|
||||
sudo systemctl enable --now containerd
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "启动 containerd 失败。"
|
||||
exit 1
|
||||
fi
|
||||
echo "containerd 启动成功。"
|
||||
}
|
||||
|
||||
configure_buildkitd() {
|
||||
echo "配置 buildkitd 服务..."
|
||||
|
||||
# 创建 buildkitd 配置文件目录
|
||||
sudo mkdir -p /etc/buildkit
|
||||
# https://github.com/moby/buildkit/blob/master/docs/buildkitd.toml.md
|
||||
sudo tee /etc/buildkit/buildkit.toml > /dev/null <<EOT
|
||||
[worker.oci]
|
||||
enabled = false
|
||||
|
||||
[worker.containerd]
|
||||
enabled = true
|
||||
# namespace should be "k8s.io" for Kubernetes (including Rancher Desktop)
|
||||
namespace = "buildkit"
|
||||
platforms = [ "linux/amd64", "linux/arm64" ]
|
||||
gc = true
|
||||
# gckeepstorage sets storage limit for default gc profile, in MB.
|
||||
gckeepstorage = 9000
|
||||
|
||||
# registry configures a new Docker register used for cache import or output.
|
||||
[registry."docker.io"]
|
||||
# mirror configuration to handle path in case a mirror registry requires a /project path rather than just a host:port
|
||||
mirrors = ["https://upnuemce.mirror.aliyuncs.com", "core.harbor.domain/proxy.docker.io"]
|
||||
http = true
|
||||
insecure = true
|
||||
#ca=["/etc/config/myca.pem"]
|
||||
#[[registry."docker.io".keypair]]
|
||||
#key="/etc/config/key.pem"
|
||||
#cert="/etc/config/cert.pem"
|
||||
EOT
|
||||
|
||||
sudo tee /etc/systemd/system/buildkit.service > /dev/null <<EOT
|
||||
[Unit]
|
||||
Description=BuildKit Daemon
|
||||
Documentation=https://github.com/moby/buildkit
|
||||
Requires=buildkit.socket
|
||||
After=network.target buildkit.socket
|
||||
|
||||
[Service]
|
||||
Type=notify
|
||||
ExecStart=/usr/local/bin/buildkitd --config /etc/buildkit/buildkit.toml --addr fd://
|
||||
Restart=always
|
||||
RestartSec=10s
|
||||
StartLimitInterval=0
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOT
|
||||
|
||||
sudo tee /etc/systemd/system/buildkit.socket > /dev/null <<EOT
|
||||
[Unit]
|
||||
Description=BuildKit
|
||||
Documentation=https://github.com/moby/buildkit
|
||||
|
||||
[Socket]
|
||||
ListenStream=%t/buildkit/buildkitd.sock
|
||||
SocketMode=0660
|
||||
|
||||
[Install]
|
||||
WantedBy=sockets.target
|
||||
EOT
|
||||
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable --now buildkit.socket
|
||||
sudo systemctl start buildkit
|
||||
|
||||
echo "buildkit 服务配置完成。"
|
||||
}
|
||||
|
||||
case "$1" in
|
||||
install)
|
||||
install_nerdctl
|
||||
install_nvidia_docker
|
||||
configure_containerd
|
||||
start_containerd
|
||||
configure_buildkitd
|
||||
;;
|
||||
uninstall)
|
||||
uninstall_nerdctl
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 {install|uninstall}"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# https://blog.csdn.net/u013522701/article/details/142153885
|
||||
# 跳过校验
|
||||
# [plugins]
|
||||
# [plugins."io.containerd.grpc.v1.cri"]
|
||||
# [plugins."io.containerd.grpc.v1.cri".registry]
|
||||
# [plugins."io.containerd.grpc.v1.cri".registry.configs]
|
||||
# [plugins."io.containerd.grpc.v1.cri".registry.configs."docker.io".tls]
|
||||
# insecure_skip_verify = true
|
||||
# 配置镜像
|
||||
# 在这里一行([plugins."io.containerd.grpc.v1.cri".registry])后面的一行加上:
|
||||
# config_path = "/etc/containerd/certs.d"
|
||||
# 然后在这个加上的路径上面添加文件夹(docker.io)并在这个文件里面添加hosts.toml文件
|
||||
# cd /etc/containerd/certs.d
|
||||
# mkdir -p docker.io
|
||||
# 内容如下:
|
||||
# server = "https://docker.io"
|
||||
# [host."https://docker.rainbond.cc"]
|
||||
# capabilities = ["pull", "resolve"]
|
||||
|
||||
# [host."https://docker.1panel.live"]
|
||||
# capabilities = ["pull", "resolve"]
|
||||
121
nerdctl/nerctl_mirror.md
Normal file
121
nerdctl/nerctl_mirror.md
Normal file
@@ -0,0 +1,121 @@
|
||||
## 普通用户安装
|
||||
|
||||
```shell
|
||||
sudo apt install uidmap
|
||||
containerd-rootless-setuptool.sh install
|
||||
containerd-rootless-setuptool.sh check
|
||||
nerdctl --version
|
||||
```
|
||||
|
||||
## 镜像加速
|
||||
|
||||
为containerd配置镜像加速(docker)
|
||||
参考:解决镜像加速问题
|
||||
|
||||
1、编辑/etc/containerd/config.toml文件,插入如下 config_path:
|
||||
|
||||
[plugins."io.containerd.grpc.v1.cri".registry]
|
||||
config_path = "/etc/containerd/certs.d"
|
||||
|
||||
2、在步骤1中指定的 **config_path **路径中创建 docker.io/hosts.toml 文件
|
||||
|
||||
server = "https://docker.io"
|
||||
[host."https://docker.m.daocloud.io"]
|
||||
capabilities = ["pull", "resolve"]
|
||||
|
||||
[host."https://reg-mirror.qiniu.com"]
|
||||
capabilities = ["pull", "resolve"]
|
||||
|
||||
# registry.k8s.io镜像加速
|
||||
mkdir -p /etc/containerd/certs.d/registry.k8s.io
|
||||
tee /etc/containerd/certs.d/registry.k8s.io/hosts.toml << 'EOF'
|
||||
server = "https://registry.k8s.io"
|
||||
|
||||
[host."https://k8s.m.daocloud.io"]
|
||||
capabilities = ["pull", "resolve", "push"]
|
||||
EOF
|
||||
|
||||
# docker.elastic.co镜像加速
|
||||
mkdir -p /etc/containerd/certs.d/docker.elastic.co
|
||||
tee /etc/containerd/certs.d/docker.elastic.co/hosts.toml << 'EOF'
|
||||
server = "https://docker.elastic.co"
|
||||
|
||||
[host."https://elastic.m.daocloud.io"]
|
||||
capabilities = ["pull", "resolve", "push"]
|
||||
EOF
|
||||
|
||||
# gcr.io镜像加速
|
||||
mkdir -p /etc/containerd/certs.d/gcr.io
|
||||
tee /etc/containerd/certs.d/gcr.io/hosts.toml << 'EOF'
|
||||
server = "https://gcr.io"
|
||||
|
||||
[host."https://gcr.m.daocloud.io"]
|
||||
capabilities = ["pull", "resolve", "push"]
|
||||
EOF
|
||||
|
||||
# ghcr.io镜像加速
|
||||
mkdir -p /etc/containerd/certs.d/ghcr.io
|
||||
tee /etc/containerd/certs.d/ghcr.io/hosts.toml << 'EOF'
|
||||
server = "https://ghcr.io"
|
||||
|
||||
[host."https://ghcr.m.daocloud.io"]
|
||||
capabilities = ["pull", "resolve", "push"]
|
||||
EOF
|
||||
|
||||
# k8s.gcr.io镜像加速
|
||||
mkdir -p /etc/containerd/certs.d/k8s.gcr.io
|
||||
tee /etc/containerd/certs.d/k8s.gcr.io/hosts.toml << 'EOF'
|
||||
server = "https://k8s.gcr.io"
|
||||
|
||||
[host."https://k8s-gcr.m.daocloud.io"]
|
||||
capabilities = ["pull", "resolve", "push"]
|
||||
EOF
|
||||
|
||||
# mcr.m.daocloud.io镜像加速
|
||||
mkdir -p /etc/containerd/certs.d/mcr.microsoft.com
|
||||
tee /etc/containerd/certs.d/mcr.microsoft.com/hosts.toml << 'EOF'
|
||||
server = "https://mcr.microsoft.com"
|
||||
|
||||
[host."https://mcr.m.daocloud.io"]
|
||||
capabilities = ["pull", "resolve", "push"]
|
||||
EOF
|
||||
|
||||
# nvcr.io镜像加速
|
||||
mkdir -p /etc/containerd/certs.d/nvcr.io
|
||||
tee /etc/containerd/certs.d/nvcr.io/hosts.toml << 'EOF'
|
||||
server = "https://nvcr.io"
|
||||
|
||||
[host."https://nvcr.m.daocloud.io"]
|
||||
capabilities = ["pull", "resolve", "push"]
|
||||
EOF
|
||||
|
||||
# quay.io镜像加速
|
||||
mkdir -p /etc/containerd/certs.d/quay.io
|
||||
tee /etc/containerd/certs.d/quay.io/hosts.toml << 'EOF'
|
||||
server = "https://quay.io"
|
||||
|
||||
[host."https://quay.m.daocloud.io"]
|
||||
capabilities = ["pull", "resolve", "push"]
|
||||
EOF
|
||||
|
||||
# registry.jujucharms.com镜像加速
|
||||
mkdir -p /etc/containerd/certs.d/registry.jujucharms.com
|
||||
tee /etc/containerd/certs.d/registry.jujucharms.com/hosts.toml << 'EOF'
|
||||
server = "https://registry.jujucharms.com"
|
||||
|
||||
[host."https://jujucharms.m.daocloud.io"]
|
||||
capabilities = ["pull", "resolve", "push"]
|
||||
EOF
|
||||
|
||||
# rocks.canonical.com镜像加速
|
||||
mkdir -p /etc/containerd/certs.d/rocks.canonical.com
|
||||
tee /etc/containerd/certs.d/rocks.canonical.com/hosts.toml << 'EOF'
|
||||
server = "https://rocks.canonical.com"
|
||||
|
||||
[host."https://rocks-canonical.m.daocloud.io"]
|
||||
capabilities = ["pull", "resolve", "push"]
|
||||
————————————————
|
||||
|
||||
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
|
||||
|
||||
原文链接:https://blog.csdn.net/terryzxy/article/details/141673546
|
||||
116
nerdctl/setup_containerd_mirror.sh
Executable file
116
nerdctl/setup_containerd_mirror.sh
Executable file
@@ -0,0 +1,116 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 生成默认的 config.toml 配置文件(覆盖可能存在的失败配置)
|
||||
echo "生成默认的 config.toml 配置..."
|
||||
sudo containerd config default | sudo tee /etc/containerd/config.toml > /dev/null
|
||||
|
||||
# 配置路径
|
||||
CONFIG_PATH="/etc/containerd/certs.d"
|
||||
NEW_STORAGE_PATH="/data/containerd"
|
||||
OLD_STORAGE_PATH="/var/lib/containerd"
|
||||
OLD_STATE_PATH="/run/containerd" # 旧的 state 目录
|
||||
|
||||
# 修改 root 和 state 路径
|
||||
echo "更新 containerd 存储路径配置..."
|
||||
sudo sed -i "s|^\s*root\s*=.*|root = \"$NEW_STORAGE_PATH\"|" /etc/containerd/config.toml
|
||||
sudo sed -i "s|^\s*state\s*=.*|state = \"$NEW_STORAGE_PATH/state\"|" /etc/containerd/config.toml
|
||||
|
||||
# 创建新的存储目录和状态目录
|
||||
echo "创建新的存储目录 $NEW_STORAGE_PATH 和状态目录..."
|
||||
sudo mkdir -p "$NEW_STORAGE_PATH"
|
||||
sudo mkdir -p "$NEW_STORAGE_PATH/state"
|
||||
sudo chown -R root:root "$NEW_STORAGE_PATH"
|
||||
sudo chmod -R 700 "$NEW_STORAGE_PATH"
|
||||
|
||||
# 迁移旧的存储数据到新的存储路径
|
||||
if [ -d "$OLD_STORAGE_PATH" ]; then
|
||||
echo "迁移旧存储数据到新路径..."
|
||||
sudo rsync -a "$OLD_STORAGE_PATH/" "$NEW_STORAGE_PATH/"
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "存储数据迁移完成,删除旧的存储路径..."
|
||||
sudo rm -rf "$OLD_STORAGE_PATH"
|
||||
echo "旧的存储路径已删除。"
|
||||
else
|
||||
echo "存储数据迁移失败,保留旧的存储路径。"
|
||||
fi
|
||||
else
|
||||
echo "旧的存储路径 $OLD_STORAGE_PATH 不存在,无需迁移。"
|
||||
fi
|
||||
|
||||
# 迁移旧的状态数据到新的状态路径
|
||||
if [ -d "$OLD_STATE_PATH" ]; then
|
||||
echo "迁移旧状态数据到新状态路径..."
|
||||
sudo rsync -a "$OLD_STATE_PATH/" "$NEW_STORAGE_PATH/state/"
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "状态数据迁移完成,删除旧的状态路径..."
|
||||
sudo rm -rf "$OLD_STATE_PATH"
|
||||
echo "旧的状态路径已删除。"
|
||||
else
|
||||
echo "状态数据迁移失败,保留旧的状态路径。"
|
||||
fi
|
||||
else
|
||||
echo "旧的状态路径 $OLD_STATE_PATH 不存在,无需迁移。"
|
||||
fi
|
||||
|
||||
# 获取迁移后的新目录大小
|
||||
initial_size=$(sudo du -sb "$NEW_STORAGE_PATH" | awk '{print $1}')
|
||||
|
||||
# 配置加速镜像的主机和路径
|
||||
declare -A mirrors
|
||||
mirrors=(
|
||||
["docker.io"]="https://docker.io https://docker.unsee.tech https://dockerhub.icu"
|
||||
["registry.k8s.io"]="https://registry.k8s.io https://k8s.m.daocloud.io"
|
||||
["docker.elastic.co"]="https://docker.elastic.co https://elastic.m.daocloud.io"
|
||||
["gcr.io"]="https://gcr.io https://gcr.m.daocloud.io"
|
||||
["ghcr.io"]="https://ghcr.io https://ghcr.m.daocloud.io"
|
||||
["k8s.gcr.io"]="https://k8s.gcr.io https://k8s-gcr.m.daocloud.io"
|
||||
["mcr.microsoft.com"]="https://mcr.microsoft.com https://mcr.m.daocloud.io"
|
||||
["nvcr.io"]="https://nvcr.io https://nvcr.m.daocloud.io"
|
||||
["quay.io"]="https://quay.io https://quay.m.daocloud.io"
|
||||
["registry.jujucharms.com"]="https://registry.jujucharms.com https://jujucharms.m.daocloud.io"
|
||||
["rocks.canonical.com"]="https://rocks.canonical.com https://rocks-canonical.m.daocloud.io"
|
||||
)
|
||||
|
||||
# 创建 hosts.toml 配置文件
|
||||
echo '正在配置镜像加速...'
|
||||
for registry in "${!mirrors[@]}"; do
|
||||
IFS=' ' read -r server host1 host2 host3 <<<"${mirrors[$registry]}"
|
||||
DIR="$CONFIG_PATH/$registry"
|
||||
sudo mkdir -p "$DIR"
|
||||
sudo tee "$DIR/hosts.toml" > /dev/null <<EOF
|
||||
server = "$server"
|
||||
|
||||
[host."$host1"]
|
||||
capabilities = ["pull", "resolve", "push"]
|
||||
|
||||
[host."$host2"]
|
||||
capabilities = ["pull", "resolve", "push"]
|
||||
|
||||
[host."$host3"]
|
||||
capabilities = ["pull", "resolve", "push"]
|
||||
EOF
|
||||
done
|
||||
|
||||
# 重启 containerd
|
||||
echo '重启 containerd 服务...'
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl restart containerd
|
||||
|
||||
# 测试配置是否生效并获取新的目录大小
|
||||
echo '测试配置是否生效...'
|
||||
if sudo ctr --namespace=default image pull --hosts-dir /etc/containerd/certs.d docker.io/library/alpine:latest; then
|
||||
echo "镜像加速配置成功!"
|
||||
|
||||
# 检查拉取后目录大小
|
||||
final_size=$(sudo du -sb "$NEW_STORAGE_PATH" | awk '{print $1}')
|
||||
|
||||
if [ "$final_size" -gt "$initial_size" ]; then
|
||||
echo "存储路径迁移成功,镜像数据已写入新路径。"
|
||||
else
|
||||
echo "存储路径迁移失败,新路径未见数据变化,请检查配置。"
|
||||
fi
|
||||
else
|
||||
echo "镜像加速配置失败,请检查配置。"
|
||||
fi
|
||||
132
nerdctl/setup_containerd_mirror_addall.sh
Executable file
132
nerdctl/setup_containerd_mirror_addall.sh
Executable file
@@ -0,0 +1,132 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 获取当前执行脚本的用户
|
||||
CURRENT_USER=$(whoami)
|
||||
|
||||
# 生成默认的 config.toml 配置文件(覆盖可能存在的失败配置)
|
||||
echo "生成默认的 config.toml 配置..."
|
||||
sudo containerd config default | sudo tee /etc/containerd/config.toml > /dev/null
|
||||
|
||||
# 配置路径
|
||||
CONFIG_PATH="/etc/containerd/certs.d"
|
||||
NEW_STORAGE_PATH="/data/containerd"
|
||||
OLD_STORAGE_PATH="/var/lib/containerd"
|
||||
OLD_STATE_PATH="/run/containerd" # 旧的 state 目录
|
||||
|
||||
# 修改 root 和 state 路径
|
||||
echo "更新 containerd 存储路径配置..."
|
||||
sudo sed -i "s|^\s*root\s*=.*|root = \"$NEW_STORAGE_PATH\"|" /etc/containerd/config.toml
|
||||
sudo sed -i "s|^\s*state\s*=.*|state = \"$NEW_STORAGE_PATH/state\"|" /etc/containerd/config.toml
|
||||
|
||||
# 创建新的存储目录和状态目录
|
||||
echo "创建新的存储目录 $NEW_STORAGE_PATH 和状态目录..."
|
||||
sudo mkdir -p "$NEW_STORAGE_PATH"
|
||||
sudo mkdir -p "$NEW_STORAGE_PATH/state"
|
||||
sudo chown -R root:root "$NEW_STORAGE_PATH"
|
||||
sudo chmod -R 700 "$NEW_STORAGE_PATH"
|
||||
|
||||
# 迁移旧的存储数据到新的存储路径
|
||||
if [ -d "$OLD_STORAGE_PATH" ]; then
|
||||
echo "迁移旧存储数据到新路径..."
|
||||
sudo rsync -a "$OLD_STORAGE_PATH/" "$NEW_STORAGE_PATH/"
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "存储数据迁移完成,删除旧的存储路径..."
|
||||
sudo rm -rf "$OLD_STORAGE_PATH"
|
||||
echo "旧的存储路径已删除。"
|
||||
else
|
||||
echo "存储数据迁移失败,保留旧的存储路径。"
|
||||
fi
|
||||
else
|
||||
echo "旧的存储路径 $OLD_STORAGE_PATH 不存在,无需迁移。"
|
||||
fi
|
||||
|
||||
# 迁移旧的状态数据到新的状态路径
|
||||
if [ -d "$OLD_STATE_PATH" ]; then
|
||||
echo "迁移旧状态数据到新状态路径..."
|
||||
sudo rsync -a "$OLD_STATE_PATH/" "$NEW_STORAGE_PATH/state/"
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "状态数据迁移完成,删除旧的状态路径..."
|
||||
sudo rm -rf "$OLD_STATE_PATH"
|
||||
echo "旧的状态路径已删除。"
|
||||
else
|
||||
echo "状态数据迁移失败,保留旧的状态路径。"
|
||||
fi
|
||||
else
|
||||
echo "旧的状态路径 $OLD_STATE_PATH 不存在,无需迁移。"
|
||||
fi
|
||||
|
||||
# 获取迁移后的新目录大小
|
||||
initial_size=$(sudo du -sb "$NEW_STORAGE_PATH" | awk '{print $1}')
|
||||
|
||||
# 配置加速镜像的主机和路径
|
||||
declare -A mirrors
|
||||
mirrors=(
|
||||
["docker.io"]="https://docker.io https://docker.unsee.tech https://dockerhub.icu"
|
||||
["registry.k8s.io"]="https://registry.k8s.io https://k8s.m.daocloud.io"
|
||||
["docker.elastic.co"]="https://docker.elastic.co https://elastic.m.daocloud.io"
|
||||
["gcr.io"]="https://gcr.io https://gcr.m.daocloud.io"
|
||||
["ghcr.io"]="https://ghcr.io https://ghcr.m.daocloud.io"
|
||||
["k8s.gcr.io"]="https://k8s.gcr.io https://k8s-gcr.m.daocloud.io"
|
||||
["mcr.microsoft.com"]="https://mcr.microsoft.com https://mcr.m.daocloud.io"
|
||||
["nvcr.io"]="https://nvcr.io https://nvcr.m.daocloud.io"
|
||||
["quay.io"]="https://quay.io https://quay.m.daocloud.io"
|
||||
["registry.jujucharms.com"]="https://registry.jujucharms.com https://jujucharms.m.daocloud.io"
|
||||
["rocks.canonical.com"]="https://rocks.canonical.com https://rocks-canonical.m.daocloud.io"
|
||||
)
|
||||
|
||||
# 创建 hosts.toml 配置文件
|
||||
echo '正在配置镜像加速...'
|
||||
for registry in "${!mirrors[@]}"; do
|
||||
IFS=' ' read -r server host1 host2 host3 <<<"${mirrors[$registry]}"
|
||||
DIR="$CONFIG_PATH/$registry"
|
||||
sudo mkdir -p "$DIR"
|
||||
sudo tee "$DIR/hosts.toml" > /dev/null <<EOF
|
||||
server = "$server"
|
||||
|
||||
[host."$host1"]
|
||||
capabilities = ["pull", "resolve", "push"]
|
||||
|
||||
[host."$host2"]
|
||||
capabilities = ["pull", "resolve", "push"]
|
||||
|
||||
[host."$host3"]
|
||||
capabilities = ["pull", "resolve", "push"]
|
||||
EOF
|
||||
done
|
||||
|
||||
# 重启 containerd
|
||||
echo '重启 containerd 服务...'
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl restart containerd
|
||||
|
||||
# 设置 containerd.sock 的权限
|
||||
echo "设置 containerd.sock 的访问权限..."
|
||||
sudo chown root:containerd /run/containerd/containerd.sock
|
||||
sudo chmod 660 /run/containerd/containerd.sock
|
||||
|
||||
# 将当前用户添加到 containerd 组,以允许使用 ctr
|
||||
echo "将用户 $CURRENT_USER 添加到 containerd 组..."
|
||||
sudo groupadd -f containerd # 确保 containerd 组存在
|
||||
sudo usermod -aG containerd "$CURRENT_USER"
|
||||
|
||||
# 提示用户重新登录以应用组权限更改
|
||||
echo "完成。请重新登录会话以应用对 group 的更改,使 $CURRENT_USER 可以使用 ctr。"
|
||||
|
||||
# 测试配置是否生效并获取新的目录大小
|
||||
echo '测试配置是否生效...'
|
||||
if sudo ctr --namespace=default image pull --hosts-dir /etc/containerd/certs.d docker.io/library/alpine:latest; then
|
||||
echo "镜像加速配置成功!"
|
||||
|
||||
# 检查拉取后目录大小
|
||||
final_size=$(sudo du -sb "$NEW_STORAGE_PATH" | awk '{print $1}')
|
||||
|
||||
if [ "$final_size" -gt "$initial_size" ]; then
|
||||
echo "存储路径迁移成功,镜像数据已写入新路径。"
|
||||
else
|
||||
echo "存储路径迁移失败,新路径未见数据变化,请检查配置。"
|
||||
fi
|
||||
else
|
||||
echo "镜像加速配置失败,请检查配置。"
|
||||
fi
|
||||
Reference in New Issue
Block a user