containerd安装和使用

文章目录

      • containerd简介
      • 安装
            • 下载安装包
            • 准备contained.service文件
            • 准备配置文件
            • 部署runc
            • 测试
      • containerd命令行工具
            • 安装nerdctl
            • 安装cni
            • 使用nerdct下载镜像启动容器

containerd简介

containerd是一个标准的容器运行时,它强调简单性、健壮性和可移植性,containerd主要可以实现以下功能:

  • 容器生命周期管理(从创建到删除)
  • 容器镜像pull和push
  • 存储管理(管理镜像和容器数据的存储)
  • 管理容器网络接口及网络
  • 与容器运行时进行交互(比如调用runc运行容器)

containerd的目的并不是面向最终用户,而是为了集成到更上层的系统中,例如Swarm、Kubernetes、Mesos等容器编排系统。

安装

下载安装包

这里采用二进制安装的方式,首先到github下载containerd的安装包:https://github.com/containerd/containerd/releases

wget https://github.com/containerd/containerd/releases/download/v1.6.6/containerd-1.6.6-linux-amd64.tar.gz
mkdir /tmp/containerd/
tar xvf containerd-1.6.6-linux-amd64.tar.gz -C /tmp/containerd/
cp /tmp/containerd/bin/* /usr/bin/
准备contained.service文件
cat /lib/systemd/system/containerd.service
[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target local-fs.target

[Service]
#uncomment to enable the experimental sbservice (sandboxed) version of containerd/cri integration
#Environment="ENABLE_CRI_SANDBOXES=sandboxed"
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/bin/containerd

Type=notify
Delegate=yes
KillMode=process
Restart=always
RestartSec=5
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity
LimitCORE=infinity
LimitNOFILE=infinity
# Comment TasksMax if your systemd version does not supports it.
# Only systemd 226 and above support this version.
TasksMax=infinity
OOMScoreAdjust=-999

[Install]
WantedBy=multi-user.target
准备配置文件

containerd的默认配置文件为/etc/containerd/config.toml,可以通过下面的命令生成一个默认配置,然后配置一下镜像加速:

mkdir /etc/containerd
containerd config default > /etc/containerd/config.toml
cat /etc/containerd/config.toml	#添加了镜像加速配置
153       [plugins."io.containerd.grpc.v1.cri".registry.mirrors]
154         [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
155           endpoint=[
156             "http://docker.mirrors.ustc.edu.cn",
157             "http://hub-mirror.c.163.com"
158           ]
部署runc
wget https://github.com/opencontainers/runc/releases/download/v1.1.3/runc.amd64 -O /usr/bin/runc
chmod +x /usr/bin/runc
测试

启动服务

systemctl daemon-reload
systemctl start containerd

下载镜像启动容器

ctr images pull docker.io/library/alpine:latest
ctr run -t --net-host docker.io/library/alpine:latest test-container sh

containerd命令行工具

因为containerd提供的ctr命令比较简单,所以推荐使用nerdctl命令来代替ctr命令,nerdctl的github地址是:https://github.com/containerd/nerdctl

安装nerdctl
wget https://github.com/containerd/nerdctl/releases/download/v0.22.0/nerdctl-0.22.0-linux-amd64.tar.gz
tar xvf nerdctl-0.22.0-linux-amd64.tar.gz
cp nerdctl /usr/bin/
nerdctl version
安装cni
wget https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgz
mkdir -p /opt/cni/bin
tar xvf cni-plugins-linux-amd64-v1.1.1.tgz -C /opt/cni/bin/
使用nerdct下载镜像启动容器
nerdctl pull nginx
nerdctl run -d -p 80:80 --name nginx --restart=always nginx
nerdctl exec -it nginx sh

你可能感兴趣的:(Docker,容器)