mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-23 21:20:42 +08:00
339 lines
9.3 KiB
Markdown
339 lines
9.3 KiB
Markdown
|
[#]: subject: "How to Install CRI-O (Container Runtime) on Ubuntu 22.04"
|
|||
|
[#]: via: "https://www.linuxtechi.com/install-crio-container-runtime-on-ubuntu/"
|
|||
|
[#]: author: "James Kiarie https://www.linuxtechi.com/author/james/"
|
|||
|
[#]: collector: "lkxed"
|
|||
|
[#]: translator: "geekpi"
|
|||
|
[#]: reviewer: " "
|
|||
|
[#]: publisher: " "
|
|||
|
[#]: url: " "
|
|||
|
|
|||
|
如何在 Ubuntu 22.04 上安装 CRI-O(容器运行时)
|
|||
|
======
|
|||
|
|
|||
|
CRI-O 是 Kubernetes 的开源轻量级容器运行时。它是使用 Open Container Initiative (OCI) 兼容运行时的 Kubernetes 容器运行时接口 (CRI) 的实现。在运行 Kubernetes 时,它是 Docker 的完美替代品。
|
|||
|
|
|||
|
在本指南中,我们将逐步演示如何在 Ubuntu 22.04 LTS 上安装 CRI-O。
|
|||
|
|
|||
|
##### 先决条件
|
|||
|
|
|||
|
在开始之前,这是你需要的:
|
|||
|
|
|||
|
- 具有 SSH 访问权限的 Ubuntu 22.04 实例
|
|||
|
- 在实例上配置的 sudo 用户
|
|||
|
- 快速稳定的互联网连接
|
|||
|
|
|||
|
有了这个,让我们开始吧。
|
|||
|
|
|||
|
### 步骤 1:更新系统并安装依赖
|
|||
|
|
|||
|
立即登录你的服务器实例并按如下方式更新包列表。
|
|||
|
|
|||
|
```
|
|||
|
$ sudo apt update
|
|||
|
```
|
|||
|
|
|||
|
更新本地包索引后,按如下方式安装依赖项。
|
|||
|
|
|||
|
```
|
|||
|
$ sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common -y
|
|||
|
```
|
|||
|
|
|||
|
### 步骤 2:添加 CRI-O 存储库
|
|||
|
|
|||
|
要安装 CRI-O,我们需要在 Ubuntu 上添加或启用它的仓库。但首先,你需要根据操作系统和要安装的 CRI-O 版本定义变量。
|
|||
|
|
|||
|
因此,定义如下变量。
|
|||
|
|
|||
|
```
|
|||
|
$ export OS=xUbuntu_22.04
|
|||
|
$ export CRIO_VERSION=1.24
|
|||
|
```
|
|||
|
|
|||
|
完成后,运行以下命令集以添加 CRI-O Kubic 仓库。
|
|||
|
|
|||
|
```
|
|||
|
$ echo "deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/ /"| sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list
|
|||
|
$ echo "deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable:/cri-o:/$CRIO_VERSION/$OS/ /"|sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable:cri-o:$CRIO_VERSION.list
|
|||
|
```
|
|||
|
|
|||
|
![][1]
|
|||
|
|
|||
|
此后,为 CRI-O 仓库导入 GPG 密钥。
|
|||
|
|
|||
|
```
|
|||
|
$ curl -L https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable:cri-o:$CRIO_VERSION/$OS/Release.key | sudo apt-key add -
|
|||
|
$ curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/Release.key | sudo apt-key add -
|
|||
|
```
|
|||
|
|
|||
|
这会产生如下输出。
|
|||
|
|
|||
|
![][2]
|
|||
|
|
|||
|
再次更新包索引,使系统与新添加的 CRI-O Kubic 仓库同步。
|
|||
|
|
|||
|
```
|
|||
|
$ sudo apt update
|
|||
|
```
|
|||
|
|
|||
|
### 步骤 3:在 Ubuntu 22.04 上安装 CRI-O
|
|||
|
|
|||
|
添加仓库后,使用 APT 包管理器安装 CRI-O 和运行时客户端。
|
|||
|
|
|||
|
```
|
|||
|
$ sudo apt install cri-o cri-o-runc -y
|
|||
|
```
|
|||
|
|
|||
|
![][3]
|
|||
|
|
|||
|
安装后,启动并启用 CRI-O 守护程序。
|
|||
|
|
|||
|
```
|
|||
|
$ sudo systemctl start crio
|
|||
|
$ sudo systemctl enable crio
|
|||
|
```
|
|||
|
|
|||
|
接下来,验证 CRI-O 服务是否正在运行:
|
|||
|
|
|||
|
```
|
|||
|
$ sudo systemctl status crio
|
|||
|
```
|
|||
|
|
|||
|
你应该看到以下输出,表明 CRI-O 服务正在按预期运行。
|
|||
|
|
|||
|
![][4]
|
|||
|
|
|||
|
### 步骤 4:为 CRI-O 安装 CNI 插件
|
|||
|
|
|||
|
接下来,你需要安装 CNI(容器网络接口)以及 CNI 插件。请记住,环回和桥接配置已启用并且足以使用 CRI-O 运行 pod。
|
|||
|
|
|||
|
因此,要安装 CNI 插件,请运行以下命令。
|
|||
|
|
|||
|
```
|
|||
|
$ sudo apt install containernetworking-plugins -y
|
|||
|
```
|
|||
|
|
|||
|
安装后,编辑 CRI-O 配置文件。
|
|||
|
|
|||
|
```
|
|||
|
$ sudo nano /etc/crio/crio.conf
|
|||
|
```
|
|||
|
|
|||
|
取消注释 network_dir 和 plugin_dirs 部分,并在 plugin_dirs 下添加 “/usr/lib/cni/”。
|
|||
|
|
|||
|
![][5]
|
|||
|
|
|||
|
保存更改并退出配置文件。
|
|||
|
|
|||
|
接下来,重启 CRIO 服务。
|
|||
|
|
|||
|
```
|
|||
|
$ sudo systemctl restart crio
|
|||
|
```
|
|||
|
|
|||
|
### 步骤 5:安装 CRI-O 工具
|
|||
|
|
|||
|
此外,你还需要安装 cri-tools 包,它提供了 crictl 命令行程序,用于交互和管理容器和 pod。
|
|||
|
|
|||
|
为此,请运行以下命令:
|
|||
|
|
|||
|
```
|
|||
|
$ sudo apt install -y cri-tools
|
|||
|
```
|
|||
|
|
|||
|
安装后,确认 crictl 和 RunTimeVersion 的版本如下。
|
|||
|
|
|||
|
```
|
|||
|
$ sudo crictl --runtime-endpoint unix:///var/run/crio/crio.sock version
|
|||
|
```
|
|||
|
|
|||
|
![][6]
|
|||
|
|
|||
|
一定要检查 CRI-O 是否准备好使用以下命令部署 pod:
|
|||
|
|
|||
|
```
|
|||
|
$ sudo crictl info
|
|||
|
```
|
|||
|
|
|||
|
![][7]
|
|||
|
|
|||
|
crictl 命令提供自动补全功能,让你可以通过按 TAB 键自动补全命令。要启用命令补全,请运行以下命令。
|
|||
|
|
|||
|
```
|
|||
|
$ sudo su -
|
|||
|
|
|||
|
# crictl completion > /etc/bash_completion.d/crictl
|
|||
|
```
|
|||
|
|
|||
|
然后重新加载当前的 bash 会话。
|
|||
|
|
|||
|
```
|
|||
|
# source ~/.bashrc
|
|||
|
```
|
|||
|
|
|||
|
![][8]
|
|||
|
|
|||
|
要使用自动补全功能,你需要注销或启动新的终端会话。然后只需键入 crictl 命令并按 TAB 键即可查看所有选项。
|
|||
|
|
|||
|
```
|
|||
|
$ crictl
|
|||
|
```
|
|||
|
|
|||
|
![][9]
|
|||
|
|
|||
|
### 步骤 6:使用 crictl 程序创建 Pod
|
|||
|
|
|||
|
至此,CRI-O 已完全安装和配置并准备好启动 pod。在本节中,我们将在 pod 中创建一个 Apache Web 服务器并确认它是否正在处理请求。
|
|||
|
|
|||
|
首先,我们将使用 pod 配置文件设置一个 pod 沙箱或隔离环境,如下所示。
|
|||
|
|
|||
|
```
|
|||
|
$ sudo nano apache_sandbox.json
|
|||
|
```
|
|||
|
|
|||
|
然后我们将以下配置添加到文件中。
|
|||
|
|
|||
|
```
|
|||
|
{
|
|||
|
"metadata": {
|
|||
|
"name": "apache-sandbox",
|
|||
|
"namespace": "default",
|
|||
|
"attempt": 1,
|
|||
|
"uid": "hdishd83djaidwnduwk28bcsb"
|
|||
|
},
|
|||
|
"linux": {
|
|||
|
},
|
|||
|
"log_directory": "/tmp"
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
保存并退出。接下来使用以下命令创建 pod。这会打印出很长的字母数字,它是 pod ID。
|
|||
|
|
|||
|
```
|
|||
|
$ sudo crictl runp apache_sandbox.json
|
|||
|
```
|
|||
|
|
|||
|
要确认 Pod 已创建,请运行命令。
|
|||
|
|
|||
|
```
|
|||
|
$ sudo crictl pods
|
|||
|
```
|
|||
|
|
|||
|
![][10]
|
|||
|
|
|||
|
要检索有关创建的 pod 的更多信息,请运行以下命令:
|
|||
|
|
|||
|
```
|
|||
|
$ sudo crictl inspectp --output table 05ba2f0704f22
|
|||
|
```
|
|||
|
|
|||
|
这将打印出 ID、名称、UID、命名空间、创建日期、内部 pod IP 等详细信息。
|
|||
|
|
|||
|
![][11]
|
|||
|
|
|||
|
### 步骤 7:在 pod 中创建容器
|
|||
|
|
|||
|
这部分中,我们将在 pod 中创建一个 Apache Web 服务器容器。因此,使用 crictl 程序从 Docker Hub 拉取 Apache Web 服务器镜像。
|
|||
|
|
|||
|
```
|
|||
|
$ sudo crictl pull httpd
|
|||
|
```
|
|||
|
|
|||
|
你可以如图所示验证拉取的镜像。
|
|||
|
|
|||
|
```
|
|||
|
$ sudo crictl images
|
|||
|
```
|
|||
|
|
|||
|
![][12]
|
|||
|
|
|||
|
接下来,我们将为 Apache Web 服务器定义一个容器配置文件。
|
|||
|
|
|||
|
```
|
|||
|
$ sudo nano container_apache.json
|
|||
|
```
|
|||
|
|
|||
|
复制并粘贴以下代码。
|
|||
|
|
|||
|
```
|
|||
|
{
|
|||
|
"metadata": {
|
|||
|
"name": "apache"
|
|||
|
},
|
|||
|
"image":{
|
|||
|
"image": "httpd"
|
|||
|
},
|
|||
|
"log_path":"apache.0.log",
|
|||
|
"linux": {
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
保存并退出配置文件。
|
|||
|
|
|||
|
最后,要将容器连接到之前创建的沙盒 pod,请运行以下命令:
|
|||
|
|
|||
|
```
|
|||
|
$ sudo crictl create 05ba2f0704f22 container_apache.json apache_sandbox.json
|
|||
|
```
|
|||
|
|
|||
|
这会向终端输出一长串字母数字 ID。请记下此 ID。
|
|||
|
|
|||
|
最后,使用 ID 启动 Apache Web 服务器容器,如下所示。
|
|||
|
|
|||
|
```
|
|||
|
$ sudo crictl start 37f4d26510965452aa918f04d629f5332a1cd398d4912298c796942e22f964a7
|
|||
|
```
|
|||
|
|
|||
|
![][13]
|
|||
|
|
|||
|
要检查容器状态,请运行以下命令:
|
|||
|
|
|||
|
```
|
|||
|
$ sudo crictl ps
|
|||
|
```
|
|||
|
|
|||
|
![][14]
|
|||
|
|
|||
|
要验证 Apache Web 服务器是否正在运行,请使用 curl 命令和 pod 的内部 ID 向 Web 服务器发送 HTTP 请求。
|
|||
|
|
|||
|
```
|
|||
|
$ curl -I 10.85.0.2
|
|||
|
```
|
|||
|
|
|||
|
以下输出确认 Web 服务器正在运行。
|
|||
|
|
|||
|
![][15]
|
|||
|
|
|||
|
##### 结论
|
|||
|
|
|||
|
这就是全部的指南。我们已经在 Ubuntu 22.04 上成功安装了 CRI-O,并继续创建 pod 和容器。欢迎你提出意见和反馈。
|
|||
|
|
|||
|
|
|||
|
--------------------------------------------------------------------------------
|
|||
|
|
|||
|
via: https://www.linuxtechi.com/install-crio-container-runtime-on-ubuntu/
|
|||
|
|
|||
|
作者:[James Kiarie][a]
|
|||
|
选题:[lkxed][b]
|
|||
|
译者:[geekpi](https://github.com/geekpi)
|
|||
|
校对:[校对者ID](https://github.com/校对者ID)
|
|||
|
|
|||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|||
|
|
|||
|
[a]: https://www.linuxtechi.com/author/james/
|
|||
|
[b]: https://github.com/lkxed/
|
|||
|
[1]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Set-Crio-Repository-Ubuntu-Linux.png
|
|||
|
[2]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Import-GPG-Keys-for-Crio-Repository-Ubuntu-Linux.png?ezimgfmt=ng:webp/ngcb22
|
|||
|
[3]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Apt-Install-Crio-Ubuntu-Linux.png?ezimgfmt=ng:webp/ngcb22
|
|||
|
[4]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Start-Enable-Crio-Service-Ubuntu.png?ezimgfmt=ng:webp/ngcb22
|
|||
|
[5]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Crio-Conf-Network-Plugins-Directory-Ubuntu.png?ezimgfmt=ng:webp/ngcb22
|
|||
|
[6]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Crictl-Crio-Version-Check-Ubuntu-Linux.png?ezimgfmt=ng:webp/ngcb22
|
|||
|
[7]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Crictl-Info-Command-Ubuntu.png?ezimgfmt=ng:webp/ngcb22
|
|||
|
[8]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Enable-Bash-Completion-Crictl-Command-Ubuntu.png?ezimgfmt=ng:webp/ngcb22
|
|||
|
[9]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Crictl-Command-Options-Ubuntu.png?ezimgfmt=ng:webp/ngcb22
|
|||
|
[10]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Crictl-Pods-Status-Ubuntu.png?ezimgfmt=ng:webp/ngcb22
|
|||
|
[11]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Crictl-Inspect-Pod-Ubuntu-Linux.png?ezimgfmt=ng:webp/ngcb22
|
|||
|
[12]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Crictl-pull-image-ubuntu.png?ezimgfmt=ng:webp/ngcb22
|
|||
|
[13]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Create-Container-Inside-Pod-Ubuntu-Linux.png?ezimgfmt=ng:webp/ngcb22
|
|||
|
[14]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Pods-Status-Crictl-Command-Ubuntu.png?ezimgfmt=ng:webp/ngcb22
|
|||
|
[15]: https://www.linuxtechi.com/wp-content/uploads/2023/03/Curl-Command-Httpd-Pod-Ubuntu.png?ezimgfmt=ng:webp/ngcb22
|