如何在 Ubuntu 22.04 上安装 CRI-O 容器运行时
容器 CRI-O
![](/data/attachment/album/202304/03/105419akd0pr3ns2n50nz0.jpg)


CRI-O 是 Kubernetes 的开源轻量级容器运行时。它是使用 <ruby> 开放容器组织 <rt>  Open Container Initiative </rt></ruby>（OCI）兼容运行时的 Kubernetes <ruby> 容器运行时接口 <rt>  Container Runtime Interface </rt></ruby>（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

```

![](/data/attachment/album/202304/03/105732daan5rcnwu5whhdr.jpg)


此后，为 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 -

```

这会产生如下输出：


![](/data/attachment/album/202304/03/105740bb7nd2wz7iblixd4.jpg)


再次更新包索引，使系统与新添加的 CRI-O Kubic 仓库同步：



```
$ sudo apt update

```

### 步骤 3：在 Ubuntu 22.04 上安装 CRI-O


添加仓库后，使用 APT 包管理器安装 CRI-O 和运行时客户端：



```
$ sudo apt install cri-o cri-o-runc -y

```

![](/data/attachment/album/202304/03/105747niy2fiduhweir1fg.jpg)


安装后，启动并启用 CRI-O 守护程序：



```
$ sudo systemctl start crio
$ sudo systemctl enable crio

```

接下来，验证 CRI-O 服务是否正在运行：



```
$ sudo systemctl status crio

```

你应该看到以下输出，表明 CRI-O 服务正在按预期运行：


![](/data/attachment/album/202304/03/105756ih2eac22plnwpzb2.jpg)


### 步骤 4：为 CRI-O 安装 CNI 插件


接下来，你需要安装 CNI（<ruby> 容器网络接口 <rt>  Container Network Interface </rt></ruby>）以及 CNI 插件。请记住，环回和桥接配置已启用并且足以使用 CRI-O 运行 <ruby> 容器荚 <rt>  Pod </rt></ruby>：


因此，要安装 CNI 插件，请运行以下命令：



```
$ sudo apt install containernetworking-plugins -y

```

安装后，编辑 CRI-O 配置文件：



```
$ sudo nano /etc/crio/crio.conf

```

取消注释 `network_dir` 和 `plugin_dirs` 部分，并在 `plugin_dirs` 下添加 `/usr/lib/cni/`。


![](/data/attachment/album/202304/03/105811o8bmrmwslflzb1b4.jpg)


保存更改并退出配置文件。


接下来，重启 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

```

![](/data/attachment/album/202304/03/105821nxippxx9pm0xxumq.jpg)


一定要检查 CRI-O 是否准备好使用以下命令部署容器荚：



```
$ sudo crictl info

```

![](/data/attachment/album/202304/03/105830f8wdzwwi1nq1kqky.jpg)


`crictl` 命令提供自动补全功能，让你可以通过按 `TAB` 键自动补全命令。要启用命令补全，请运行以下命令：



```
$ sudo su -

# crictl completion > /etc/bash_completion.d/crictl

```

然后重新加载当前的 Bash 会话：



```
# source ~/.bashrc

```

![](/data/attachment/album/202304/03/105840of5hot1opfmomdf1.jpg)


要使用自动补全功能，你需要注销或启动新的终端会话。然后只需键入 `crictl` 命令并按 `TAB` 键即可查看所有选项。



```
$ crictl

```

![](/data/attachment/album/202304/03/105850az3h1l5yy9cyc58y.jpg)


### 步骤 6：使用 crictl 程序创建容器荚


至此，CRI-O 已完全安装和配置并准备好启动容器荚。在本节中，我们将在容器荚中创建一个 Apache Web 服务器并确认它是否正在处理请求。


首先，我们将使用容器荚配置文件设置一个沙箱或隔离环境，如下所示：



```
$ sudo nano apache_sandbox.json

```

然后我们将以下配置添加到文件中：



```
{
    "metadata": {
        "name": "apache-sandbox",
        "namespace": "default",
        "attempt": 1,
        "uid": "hdishd83djaidwnduwk28bcsb"
    },
    "linux": {
    },
    "log_directory": "/tmp"
}

```

保存并退出。接下来使用以下命令创建容器荚。这会打印出很长的字母数字，它是容器荚 ID。



```
$ sudo crictl runp apache_sandbox.json

```

要确认容器荚已创建，请运行命令：



```
$ sudo crictl pods

```

![](/data/attachment/album/202304/03/105903tkpwmmwejej7mv0u.jpg)


要检索有关创建的容器荚的更多信息，请运行以下命令：



```
$ sudo crictl inspectp --output table 05ba2f0704f22

```

这将打印出 ID、名称、UID、命名空间、创建日期、内部 IP 等详细信息。


![](/data/attachment/album/202304/03/105914m9cevk7devvca0cj.jpg)


### 步骤 7：在容器荚中创建容器


这部分中，我们将在容器荚中创建一个 Apache Web 服务器容器。因此，使用 `crictl` 程序从 Docker Hub 拉取 Apache Web 服务器镜像：



```
$ sudo crictl pull httpd

```

你可以如图所示验证拉取的镜像：



```
$ sudo crictl images

```

![](/data/attachment/album/202304/03/105928lrwlmzw4wbllaawa.jpg)


接下来，我们将为 Apache Web 服务器定义一个容器配置文件：



```
$ sudo nano container_apache.json

```

复制并粘贴以下代码：



```
{
  "metadata": {
      "name": "apache"
    },
  "image":{
      "image": "httpd"
    },
  "log_path":"apache.0.log",
  "linux": {
  }
}

```

保存并退出配置文件。


最后，要将容器连接到之前创建的沙盒容器荚，请运行以下命令：



```
$ sudo crictl create 05ba2f0704f22 container_apache.json apache_sandbox.json

```

这会向终端输出一长串字母数字 ID。请记下此 ID。


最后，使用 ID 启动 Apache Web 服务器容器，如下所示：



```
$ sudo crictl start 37f4d26510965452aa918f04d629f5332a1cd398d4912298c796942e22f964a7

```

![](/data/attachment/album/202304/03/105938czh1uh35hgkw3929.jpg)


要检查容器状态，请运行以下命令：



```
$ sudo crictl ps

```

![](/data/attachment/album/202304/03/105944d3en2xe8q5h7jwj6.jpg)


要验证 Apache Web 服务器是否正在运行，请使用 `curl` 命令和容器荚的内部 ID 向 Web 服务器发送 HTTP 请求：



```
$ curl -I 10.85.0.2

```

以下输出确认 Web 服务器正在运行：


![](/data/attachment/album/202304/03/105950w95bbas5pdq5bq5z.jpg)


### 结论


这就是全部的指南。我们已经在 Ubuntu 22.04 上成功安装了 CRI-O，并继续创建容器荚和容器。欢迎你提出意见和反馈。




---


via: <https://www.linuxtechi.com/install-crio-container-runtime-on-ubuntu/>


作者：[James Kiarie](https://www.linuxtechi.com/author/james/) 选题：[lkxed](https://github.com/lkxed/) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
[CRI-O](https://cri-o.io/) is an opensource and lightweight container runtime for Kubernetes. It is an implementation of the Kubernetes Container Runtime Interface (CRI) using Open Container Initiative (OCI) compatible runtimes. It’s a perfect alternative to Docker when running Kubernetes.

In this guide, we will demonstrate how to install CRI-O on Ubuntu 22.04 LTS step by step.

#### Prerequisites

Before you start out, here is what you need:

- An instance of Ubuntu 22.04 with SSH access
- A
[sudo user](https://www.linuxtechi.com/how-to-create-sudo-user-on-ubuntu/)configured on the instance - Fast and stable internet connectivity

With that out of the way, let us get started out.

## Step 1: Update the system and Install dependencies

Right off the bat, log into your server instance and update the package lists as follows.

$ sudo apt update

Once the local package index has been updated, install the dependencies as follows.

$ sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common -y

## Step 2: Add CRI-O repository

To install CRI-O, we need to add or enable its repository on Ubuntu. But first, you need to define the variables based on the operating systems and the CRI-O version that you want to install.

As such, define the variables as shown below.

$ export OS=xUbuntu_22.04 $ export CRIO_VERSION=1.24

Once that is done, run the following set of commands to add the CRI-O Kubic repository.

$ 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

Thereafter, import the GPG key for the CRI-O repository

$ 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 -

This yields the following output as shown below.

Once again update the package index to synchronize the system with the newly added CRI-O Kubic repositories.

$ sudo apt update

## Step 3: Install CRI-O On Ubuntu 22.04

With the repositories added, install CRI-O and the runtime client using the APT package manager.

$ sudo apt install cri-o cri-o-runc -y

Once installed, start and enable the CRI-O daemon.

$ sudo systemctl start crio $ sudo systemctl enable crio

Next, verify if the CRI-O service is running:

$ sudo systemctl status crio

You should get the following output which shows that the CRI-O service is running as expected.

## Step 4: Install CNI Plugins For CRI-O

Next, you need to install the CNI (Container Network Interface) as well as the CNI plugins. Keep in mind that the loopback and bridge configurations are enabled and sufficient for running pods using CRI-O.

Therefore, to install the CNI plugins, run the following command.

$ sudo apt install containernetworking-plugins -y

Once installed, edit the CRI-O configuration file

$ sudo nano /etc/crio/crio.conf

Uncomment network_dir & plugin_dirs section and also add ‘/usr/lib/cni/’ under plugin_dirs section.

Save the changes and exit the configuration file.

Next, restart the CRIO service.

$ sudo systemctl restart crio

## Step 5: Install CRI-O tools

In addition, you also need to install the cri-tools package which provides the crictl command-line utility which is used for interacting and managing containers and pods.

To do so, run the command:

$ sudo apt install -y cri-tools

Once installed, confirm the version of crictl and RunTimeVersion as follows.

$ sudo crictl --runtime-endpoint unix:///var/run/crio/crio.sock version

Be sure to also check if CRI-O is ready to deploy pods using the following command:

$ sudo crictl info

The crictl command provides an autocompletion feature that lets you autocomplete commands by pressing the TAB key. To enable command completion run the following command.

$ sudo su - # crictl completion > /etc/bash_completion.d/crictl

Then reload the current bash session.

# source ~/.bashrc

To use the auto-completion feature, you will need to log out or start a new terminal session. Then simply type the crictl command and press the TAB key to view all the options.

$ crictl

## Step 6: Create a Pod using crictl utility

Up to this point, CRI-O is fully installed and configured and ready to spin up a pod. In this section, we will create an Apache web server inside a pod and confirm if it is serving requests.

First, we are going to set up a pod sandbox or an isolated environment using a pod configuration file as follows.

$ sudo nano apache_sandbox.json

We will then add the following configuration to the file.

{ "metadata": { "name": "apache-sandbox", "namespace": "default", "attempt": 1, "uid": "hdishd83djaidwnduwk28bcsb" }, "linux": { }, "log_directory": "/tmp" }

Save and exit. Next create the pod using the following command. This prints out long alphanumeric number which is the pod ID.

$ sudo crictl runp apache_sandbox.json

To confirm that the pod has been created, run the command.

$ sudo crictl pods

To retrieve more information about the created pod, run the command:

$ sudo crictl inspectp --output table 05ba2f0704f22

This prints out the ID, Name, UID, Namespace, date of creation, internal pod IP among other details.

## Step 7: Create a container inside a pod

In section we are going to create an Apache web server container inside the pod. So, use the crictl utility to pull an Apache web server image from Docker Hub.

$ sudo crictl pull httpd

You can verify the image pulled as shown.

$ sudo crictl images

Next, we are going to define a container configuration file for the Apache web server.

$ sudo nano container_apache.json

Copy and paste the following code.

{ "metadata": { "name": "apache" }, "image":{ "image": "httpd" }, "log_path":"apache.0.log", "linux": { } }

Save and exit the configuration file.

Finally, to attach the container to the sand box pod created earlier, run the command:

$ sudo crictl create 05ba2f0704f22 container_apache.json apache_sandbox.json

This outputs a large alphanumeric ID to the terminal.. Take note of this ID.

Finally, use the ID to start the Apache web server container as follows.

$ sudo crictl start 37f4d26510965452aa918f04d629f5332a1cd398d4912298c796942e22f964a7

To check the container status, run the command:

$ sudo crictl ps

To verify that the Apache web server is running, send a HTTP request to the web server using the curl command and the pod’s internal ID.

$ curl -I 10.85.0.2

The following output confirms that the web server is running.

#### Conclusion

That’s all from this, guide. We have successfully installed CRI-O on Ubuntu 22.04 and gone ahead to create a pod and container. Your comments and feedback are welcome.

Also Read: [How to Install Docker on Ubuntu 22.04 / 20.04 LTS](https://www.linuxtechi.com/install-use-docker-on-ubuntu/)

Juan SilvaFantastic install, thank you so very much, CRI seems like a great alternative to both docker and snap…