洞悉 發表於 2022-12-21 15:35:00

CentOS 7.9 安装 containerd(转载)

<p></p><div class="toc"><div class="toc-container-header">目录</div><ul><li><code>containerd</code> 安装及使用<ul><li><code>containerd</code> 安装<ul><li>安装 <code>containerd</code></li><li>生成默认配置</li><li>配置 <code>systemd cgroup</code> 驱动程序</li><li>配置代理(如果需要则配置,默认无需修改)</li><li>启动</li></ul></li><li><code>containerd</code> 使用<ul><li>镜像操作<ul><li>拉取镜像</li><li>列出本地镜像</li><li>检测本地镜像</li><li>重新打标签</li><li>删除镜像</li><li>将镜像挂载到主机目录</li><li>将镜像从主机目录上卸载</li><li>将镜像导出为压缩包</li><li>从压缩包导入镜像</li></ul></li><li>容器操作<ul><li>创建容器</li><li>列出容器</li><li>查看容器详细配置</li><li>删除容器</li></ul></li><li>任务<ul><li>启动容器</li><li>查看正在运行的容器</li><li>进入容器进行操作</li><li>暂停容器</li><li>恢复容器:</li><li>杀死容器</li><li>删除 Task</li><li>获取容器信息</li></ul></li><li>命名空间</li></ul></li><li>Containerd 高级命令行工具 nerdctl</li><li>参考文档</li></ul></li></ul></div><p></p>
<h2 id="containerd-安装及使用"><code>containerd</code> 安装及使用</h2>
<h3 id="containerd-安装"><code>containerd</code> 安装</h3>
<h4 id="安装-containerd">安装 <code>containerd</code></h4>
<pre><code class="language-shell">yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum -y install containerd.io
</code></pre>
<h4 id="生成默认配置">生成默认配置</h4>
<pre><code class="language-shell">containerd config default &gt; /etc/containerd/config.toml
</code></pre>
<h4 id="配置-systemd-cgroup-驱动程序">配置 <code>systemd cgroup</code> 驱动程序</h4>
<pre><code class="language-shell">sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml
</code></pre>
<h4 id="配置代理如果需要则配置默认无需修改">配置代理(如果需要则配置,默认无需修改)</h4>
<blockquote>
<p>这步主要是为了 <code>kubeadm</code> 初始化时能从 <code>k8s.gcr.io</code> 拉取镜像。</p>
</blockquote>
<pre><code class="language-shell">vim /usr/lib/systemd/system/containerd.service

# 在 段添加以下参数

Environment="HTTPS_PROXY=http://代理IP:代理端口"
Environment="NO_PROXY=127.0.0.1/8,172.16.0.0/16"
</code></pre>
<h4 id="启动">启动</h4>
<pre><code class="language-shell">systemctl daemon-reload
systemctl enable containerd --now
systemctl status containerd
</code></pre>
<h3 id="containerd-使用"><code>containerd</code> 使用</h3>
<h4 id="镜像操作">镜像操作</h4>
<h5 id="拉取镜像">拉取镜像</h5>
<pre><code class="language-shell">$ ctr image pull docker.io/library/nginx:alpine
</code></pre>
<h5 id="列出本地镜像">列出本地镜像</h5>
<pre><code class="language-shell">$ ctr image ls
REF                            TYPE                                                      DIGEST                                                                  SIZE    PLATFORMS                                                                              LABELS
docker.io/library/nginx:alpine application/vnd.docker.distribution.manifest.list.v2+json sha256:bead42240255ae1485653a956ef41c9e458eb077fcb6dc664cbc3aa9701a05ce 9.5 MiB linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x -

$ ctr image ls -q
docker.io/library/nginx:alpine
</code></pre>
<blockquote>
<p>使用 <code>-q(--quiet)</code> 选项可以只打印镜像名称。</p>
</blockquote>
<h5 id="检测本地镜像">检测本地镜像</h5>
<pre><code class="language-shell">$ ctr image check
REF                            TYPE                                                      DIGEST                                                                  STATUS         SIZE            UNPACKED
docker.io/library/nginx:alpine application/vnd.docker.distribution.manifest.list.v2+json sha256:bead42240255ae1485653a956ef41c9e458eb077fcb6dc664cbc3aa9701a05ce complete (7/7) 9.5 MiB/9.5 MiB true
</code></pre>
<blockquote>
<p>主要查看其中的 <code>STATUS,complete</code> 表示镜像是完整可用的状态</p>
</blockquote>
<h5 id="重新打标签">重新打标签</h5>
<pre><code class="language-shell">$ ctr image tag docker.io/library/nginx:alpine nginx:v1
harbor.k8s.local/course/nginx:alpine

$ ctr image ls -q
docker.io/library/nginx:alpine
nginx:v1
</code></pre>
<h5 id="删除镜像">删除镜像</h5>
<pre><code class="language-shell">$ ctr image rm harbor.k8s.local/course/nginx:alpine
harbor.k8s.local/course/nginx:alpine

$ ctr image ls -q
docker.io/library/nginx:alpine
</code></pre>
<blockquote>
<p>加上 <code>--sync</code> 选项可以同步删除镜像和所有相关的资源。</p>
</blockquote>
<h5 id="将镜像挂载到主机目录">将镜像挂载到主机目录</h5>
<pre><code class="language-shell">$ ctr image mount docker.io/library/nginx:alpine /mnt
sha256:c3554b2d61e3c1cffcaba4b4fa7651c644a3354efaafa2f22cb53542f6c600dc
/mnt

$ tree -L 1 /mnt
/mnt
├── bin
├── dev
├── docker-entrypoint.d
├── docker-entrypoint.sh
├── etc
├── home
├── lib
├── media
├── mnt
├── opt
├── proc
├── root
├── run
├── sbin
├── srv
├── sys
├── tmp
├── usr
└── var

18 directories, 1 file
</code></pre>
<h5 id="将镜像从主机目录上卸载">将镜像从主机目录上卸载</h5>
<pre><code class="language-shell">$ ctr image unmount /mnt
/mnt
</code></pre>
<h5 id="将镜像导出为压缩包">将镜像导出为压缩包</h5>
<pre><code class="language-shell">$ ctr image export --all-platforms nginx.tar.gz docker.io/library/nginx:alpine
</code></pre>
<h5 id="从压缩包导入镜像">从压缩包导入镜像</h5>
<pre><code class="language-shell">$ ctr image import nginx.tar.gz
</code></pre>
<h4 id="容器操作">容器操作</h4>
<h5 id="创建容器">创建容器</h5>
<pre><code class="language-shell">$ ctr container create docker.io/library/nginx:alpine nginx
</code></pre>
<h5 id="列出容器">列出容器</h5>
<pre><code class="language-shell">$ ctr container ls
CONTAINER    IMAGE                           RUNTIME
nginx      docker.io/library/nginx:alpine    io.containerd.runc.v2
</code></pre>
<blockquote>
<p>同样可以加上 <code>-q</code> 选项精简列表内容</p>
</blockquote>
<pre><code class="language-shell">$ ctr container ls -q
nginx
</code></pre>
<h5 id="查看容器详细配置">查看容器详细配置</h5>
<blockquote>
<p>类似于 <code>docker inspect</code> 功能。</p>
</blockquote>
<pre><code class="language-shell">$ ctr container info nginx
</code></pre>
<h5 id="删除容器">删除容器</h5>
<pre><code class="language-shell">$ ctr container rm nginx

$ ctr container ls
CONTAINER    IMAGE    RUNTIME
</code></pre>
<blockquote>
<p>除了使用 <code>rm</code> 子命令之外也可以使用 <code>delete</code> 或者 <code>del</code> 删除容器。</p>
</blockquote>
<h4 id="任务">任务</h4>
<p>通过 <code>container create</code> 命令创建的容器,并没有处于运行状态,只是一个静态的容器。</p>
<p>一个容器真正运行起来是由 <code>Task</code> 任务实现的,<code>Task</code> 可以为容器设置网卡,还可以配置工具来对容器进行监控等。</p>
<h5 id="启动容器">启动容器</h5>
<pre><code class="language-shell">$ ctr task start -d nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
</code></pre>
<h5 id="查看正在运行的容器">查看正在运行的容器</h5>
<pre><code class="language-shell">$ ctr task ls
TASK   PID   STATUS
nginx    3630    RUNNING
</code></pre>
<h5 id="进入容器进行操作">进入容器进行操作</h5>
<pre><code class="language-shell">$ ctr task exec --exec-id 0 -t nginx sh
/ #
</code></pre>
<blockquote>
<p>不过这里需要注意必须要指定 <code>--exec-id</code> 参数,这个 <code>id</code> 可以随便写,只要唯一就行。</p>
</blockquote>
<h5 id="暂停容器">暂停容器</h5>
<pre><code class="language-shell">$ ctr task pause nginx
</code></pre>
<blockquote>
<p>暂停后容器状态变成了 <code>PAUSED</code></p>
</blockquote>
<pre><code class="language-shell">$ ctr task ls
TASK   PID   STATUS
nginx    3630    PAUSED
</code></pre>
<h5 id="恢复容器">恢复容器:</h5>
<pre><code class="language-shell">$ ctr task resume nginx

$ ctr task ls
TASK   PID   STATUS
nginx    3630    RUNNING
</code></pre>
<blockquote>
<p>不过需要注意 <code>ctr</code> 没有 <code>stop</code> 容器的功能,只能暂停或者杀死容器</p>
</blockquote>
<h5 id="杀死容器">杀死容器</h5>
<pre><code class="language-shell">$ ctr task kill nginx

$ ctr task ls
TASK   PID   STATUS
nginx    3630    STOPPED
</code></pre>
<h5 id="删除-task">删除 Task</h5>
<pre><code>$ ctr task rm nginx
$ ctr task ls
TASK    PID    STATUS
</code></pre>
<h5 id="获取容器信息">获取容器信息</h5>
<blockquote>
<p>除此之外我们还可以获取容器的 <code>cgroup</code> 相关信息,可以使用 <code>task metrics</code> 命令用来获取容器的内存、<code>CPU</code> 和 <code>PID</code> 的限额与使用量。</p>
</blockquote>
<pre><code class="language-shell"># 重新启动容器
$ ctr task metrics nginx
ID             TIMESTAMP                                 
nginx          2022-12-21 07:27:56.869059839 +0000 UTC   

METRIC                   VALUE                  
memory.usage_in_bytes    2121728               
memory.limit_in_bytes    9223372036854771712   
memory.stat.cache      20480                  
cpuacct.usage            67129078               
cpuacct.usage_percpu      
pids.current             3                     
pids.limit               0
</code></pre>
<p>还可以使用 <code>task ps</code> 命令查看容器中所有进程在宿主机中的 <code>PID</code></p>
<pre><code class="language-shell">$ ctr task ps nginx
PID   INFO
3984    -
4029    -
4030    -
4031    -
4032    -
4033    -
4034    -
4035    -
4036    -

$ ctr task ls
TASK   PID   STATUS
nginx    3984    RUNNING
</code></pre>
<blockquote>
<p>其中第一个 <code>PID 3984</code> 就是我们容器中的 <code>1</code> 号进程。</p>
</blockquote>
<h4 id="命名空间">命名空间</h4>
<p>另外 <code>Containerd</code> 中也支持命名空间的概念,比如查看命名空间:</p>
<pre><code class="language-shell">$ ctr ns ls
NAME    LABELS
default
</code></pre>
<p>如果不指定,<code>ctr</code> 默认使用的是 <code>default</code> 空间。同样也可以使用 <code>ns create</code> 命令创建一个命名空间:</p>
<pre><code class="language-shell">$ ctr ns create test

$ ctr ns ls
NAME    LABELS
default
test
</code></pre>
<p>使用 <code>remove</code> 或者 <code>rm</code> 可以删除 <code>namespace</code>:</p>
<pre><code class="language-shell">$ ctr ns rm test
test

$ ctr ns ls
NAME    LABELS
default
</code></pre>
<p>有了命名空间后就可以在操作资源的时候指定 <code>namespace</code>,比如查看 <code>test</code> 命名空间的镜像,可以在操作命令后面加上 <code>-n test</code> 选项:</p>
<pre><code class="language-shell">$ ctr -n test image ls
REF TYPE DIGEST SIZE PLATFORMS LABELS
</code></pre>
<p><code>Docker</code> 其实也是默认调用的 <code>containerd</code>,事实上 <code>Docker</code> 使用的 <code>containerd</code> 下面的命名空间默认是 <code>moby</code>,而不是 <code>default</code>,所以假如我们有用 <code>docker</code> 启动容器,那么我们也可以通过 <code>ctr -n moby</code> 来定位下面的容器:</p>
<pre><code class="language-shell">$ ctr -n moby container ls
</code></pre>
<p>同样 <code>Kubernetes</code> 下使用的 <code>containerd</code> 默认命名空间是 <code>k8s.io</code>,所以我们可以使用 <code>ctr -n k8s.io</code> 来查看 <code>Kubernetes</code> 下面创建的容器。</p>
<h3 id="containerd-高级命令行工具-nerdctl">Containerd 高级命令行工具 nerdctl</h3>
<p>参考文档</p>
<h3 id="参考文档">参考文档</h3>
<blockquote>
<p>https://www.jianshu.com/p/2d3ea652da9f<br>
https://www.qikqiak.com/k3s/runtime/usage/#使用<br>
https://www.qikqiak.com/k3s/runtime/nerdctl/</p>
</blockquote><br><br>
来源:https://www.cnblogs.com/evescn/p/16996386.html
頁: [1]
查看完整版本: CentOS 7.9 安装 containerd(转载)