安装nerdctl和buildkitd脚本命令

#!/bin/bash

set -euo pipefail

检查是否以root权限运行

if [ "$(id -u)" -ne 0 ]; then

echo "错误:请使用root权限或sudo运行本脚本" >&2

exit 1

fi

检测openEuler系统(兼容大小写)

detect_distribution() {

if [ -f /etc/os-release ]; then

. /etc/os-release

匹配ID为openEuler(兼容大小写,如"openEuler"或"openeuler")

if [[ "ID" =\~ \^\[Oo\]pen\[Ee\]uler ]]; then

echo "openeuler"

else

echo "unsupported"

fi

else

echo "unsupported"

fi

}

DISTRO=$(detect_distribution)

if [[ "$DISTRO" != "openeuler" ]]; then

echo "错误:本脚本仅适用于openEuler系统" >&2

exit 1

fi

安装前置依赖(openEuler 25.03使用dnf)

install_dependencies() {

dnf update -y

dnf install -y \

curl \

gnupg \

ca-certificates \

jq \

tar \

gzip \

device-mapper-persistent-data \

lvm2 # 容器存储依赖

}

安装containerd(使用openEuler官方仓库)

install_containerd() {

安装最新稳定版containerd

dnf install -y containerd

优化containerd配置(适配openEuler 25.03的systemd和cgroupv2)

sed -i '/SystemdCgroup =/c\SystemdCgroup = true' /etc/containerd/config.toml

sed -i 's/cri\.containerd\.runtime\.v1\.linux/cri.containerd.untrusted-workload.v1.linux/g' /etc/containerd/config.toml # 启用非信任工作负载支持

重启并启用服务

systemctl daemon-reload

systemctl restart containerd

systemctl enable containerd

}

安装最新版nerdctl(适配openEuler架构)

install_nerdctl() {

获取最新稳定版(排除预发布)

LATEST_NERDCTL=$(curl -s https://api.github.com/repos/containerd/nerdctl/releases/latest | jq -r '.tag_name')

if [[ -z "LATEST_NERDCTL" \|\| "LATEST_NERDCTL" == "null" ]]; then

echo "获取nerdctl最新版本失败,可能是网络问题" >&2

exit 1

fi

识别架构(支持x86_64/aarch64)

ARCH=(case (uname -m) in

x86_64) echo "amd64" ;;

aarch64) echo "arm64" ;;

*) echo "unsupported"; exit 1 ;;

esac)

echo "下载文件:https://github.com/containerd/nerdctl/releases/download/${LATEST_NERDCTL}/nerdctl-${LATEST_NERDCTL#v}-linux-${ARCH}.tar.gz"

下载并安装

curl -fsSL "https://github.com/containerd/nerdctl/releases/download/${LATEST_NERDCTL}/nerdctl-${LATEST_NERDCTL#v}-linux-${ARCH}.tar.gz" \

| tar -xz -C /usr/local/bin

验证安装

if ! command -v nerdctl &> /dev/null; then

echo "nerdctl安装失败" >&2

exit 1

fi

}

安装buildkit并配置systemd服务(适配openEuler)

install_buildkit() {

获取最新稳定版

LATEST_BUILDKIT=$(curl -s https://api.github.com/repos/moby/buildkit/releases/latest | jq -r '.tag_name')

if [[ -z "LATEST_BUILDKIT" \|\| "LATEST_BUILDKIT" == "null" ]]; then

echo "获取buildkit最新版本失败,可能是网络问题" >&2

exit 1

fi

识别架构

ARCH=(case (uname -m) in

x86_64) echo "amd64" ;;

aarch64) echo "arm64" ;;

*) echo "unsupported"; exit 1 ;;

esac)

下载并安装

curl -fL "https://github.com/moby/buildkit/releases/download/${LATEST_BUILDKIT}/buildkit-${LATEST_BUILDKIT}.linux-${ARCH}.tar.gz" \

| tar -xz -C /usr/local

创建buildkitd服务文件(优化openEuler集成)

cat > /etc/systemd/system/buildkitd.service <<EOF

Unit

Description=BuildKit Daemon for openEuler 25.03

Documentation=https://github.com/moby/buildkit

After=network.target containerd.service

Requires=containerd.service

Service

Type=simple

ExecStart=/usr/local/bin/buildkitd \

--oci-worker=true \

--containerd-worker=true \

--containerd-worker-namespace=k8s.io \

--addr=unix:///run/buildkit/buildkitd.sock \

--oci-worker-snapshotter=overlayfs

Restart=on-failure

RestartSec=5

LimitNOFILE=1048576

Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

Install

WantedBy=multi-user.target

EOF

启动服务并配置开机自启

systemctl daemon-reload

systemctl start buildkitd

systemctl enable buildkitd

}

主执行流程

echo "=== 检测到openEuler 25.03系统,开始安装 ==="

echo "=== 1/4 安装前置依赖 ==="

install_dependencies

echo "=== 2/4 安装并配置containerd ==="

install_containerd

echo "=== 3/4 安装最新版nerdctl ==="

install_nerdctl

echo "=== 4/4 安装并配置buildkit ==="

install_buildkit

echo "=== 安装验证 ==="

echo "nerdctl版本: $(nerdctl --version)"

echo "buildkitd状态: $(systemctl is-active buildkitd)"

echo "containerd状态: $(systemctl is-active containerd)"

echo "安装完成!nerdctl和buildkit已配置为开机启动"

相关推荐
e***877037 分钟前
Node.js 与 Docker 深度整合:轻松部署与管理 Node.js 应用
docker·容器·node.js
q***71012 小时前
从零到上线:Node.js 项目的完整部署流程(包含 Docker 和 CICD)
docker·容器·node.js
好奇的菜鸟4 小时前
在 WSL 中安装 Docker
运维·docker·容器
芥子沫8 小时前
日记应用推荐-Docker安装DailyNotes应用
docker·容器·日记
2501_9418814016 小时前
Kubernetes 容器集群资源调度与弹性扩容高可用架构在互联网业务实战经验总结
云原生·容器·kubernetes
究極の法則に通じた野犬16 小时前
k8s设计理念-k8s中哪些服务要部署成StatefulSet哪些部署成Deployment
云原生·容器·kubernetes
wuxingge16 小时前
k8s集群误删node节点,怎么添加回去
云原生·容器·kubernetes
观测云19 小时前
Kubernetes CRD 方式配置容器日志采集最佳实践
容器·kubernetes·日志分析
tianyuanwo1 天前
多平台容器化RPM构建流水线全指南:Fedora、CentOS与Anolis OS
linux·运维·容器·centos·rpm
敲上瘾1 天前
【探索实战】:Kurator分布式统一应用分发平台的全面解析与实践指南
分布式·容器·kubernetes·serverless