如何在 Ubuntu 20.04 上安装和配置 Podman

什么是 Podman?

Podman(Pod Manager)是一个用于开发、管理和运行容器和镜像的工具。 它可以执行支持和修改容器图像以及检索和标记容器的功能。

Podman 和 Docker 是类似的软件包。 然而,关键的区别在于 Podman 不需要守护进程来运行容器,而 Docker 需要 Docker Engine 守护进程。 因此,Podman 需要 runComplete 来启动容器,而它是 Docker 引擎守护程序所固有的。 Podman 还使用 Skopeo(容器移植)和 Buildah(容器构建)库来实现一些功能,但没有使用 daemon 服务。

Podman 以 root 或非 root 身份运行,使用 libpod 库来管理整个容器生态系统和卷映像。 在 Podman libpod 库中,您将获得:

  • 支持多种镜像格式,包括镜像 OCI 和 Docker。
  • 加载图像的几种方法,包括检查图像。
  • 图像和图层管理。
  • 完整的容器生命周期管理。
  • 容器的资源隔离。
  • 还有更多。

在 Ubuntu 20.04 上安装 Podman

虽然本文重点介绍在 Ubuntu 20.04 上的安装,但 Podman 安装包包含在 Ubuntu 20.10 的本机存储库中。 Podman 可以安装在 Ubuntu 20.10 上,但请注意安装说明可能因 Ubuntu 版本而异。

安装要求

  • 服务器 4 GB RAM 和 2 个内核。
  • 操作系统 Ubuntu 20.04。
  • 根用户。 Podman 默认在没有 root 的情况下运行,但您需要 root 访问权限才能使用图像。 使用 sudo 如果您正在运行命令并且没有 root 权限,则使用命令。

注意:某些 Ubuntu 安装可能没有定义 VERSION_ID 变量,这会导致本教程中使用的命令出现问题。 使用 source /etc/os-release 解决出现的任何问题。

高级打包工具 (APT) 是一个命令行实用程序,用于在 Ubuntu、Debian 和 Linux 上管理软件包(安装、更新和删除)。 使用 apt update && apt upgrade -y 命令在安装前执行所需的系统和应用程序包更新。

root@host:~# apt update && apt upgrade -y
Hit:1 https://by.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 https://by.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 https://by.archive.ubuntu.com/ubuntu focal-backports InRelease
Hit:4 https://security.ubuntu.com/ubuntu focal-security InRelease
Reading package lists... Done         
Building dependency tree       
Reading state information... Done
All packages are up to date.
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
  libllvm10 linux-headers-5.4.0-42 linux-headers-5.4.0-42-generic linux-image-5.4.0-42-generic linux-modules-5.4.0-42-generic linux-modules-extra-5.4.0-42-generic
Use 'apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
root@host:~#

将存储库添加到 apt,这将允许您下载 Podman。

root@host:~# sh -c "echo 'deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_${VERSION_ID}/ /' > /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list"
root@host:~#

使用存储库密钥来验证包来自预期来源。

root@host:~# wget -nv https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable/xUbuntu_${VERSION_ID}/Release.key -O- | sudo apt-key add -
2021-04-14 22:49:24 URL:https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_20.04/Release.key [1093/1093] -> "-" [1]
OK
root@host:~# 

使用 apt-get update 再次更新软件包列表。

root@host:~# apt-get update
Hit:1 https://by.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 https://by.archive.ubuntu.com/ubuntu focal-updates InRelease                                            
Hit:3 https://by.archive.ubuntu.com/ubuntu focal-backports InRelease                                          
Hit:4 https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/xUbuntu_20.04  InRelease 
Hit:5 https://security.ubuntu.com/ubuntu focal-security InRelease                                             
Reading package lists... Done
root@host:~#

现在,使用命令 apt-get install podman -y 安装 Podman。

root@host:~# apt-get install podman -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
...

通过使用 podman –version 命令检查版本来验证 Podman 安装是否成功。

root@host:~# podman --version
podman version 3.1.0
root@host:~#

也可以使用 podman 命令 podman info 来查看配置信息和各个组件的版本,但是输出会比较长。

root@host:~# podman info
host:
  arch: amd64
  buildahVersion: 1.20.0
  cgroupManager: systemd
  cgroupVersion: v1
  conmon:
    package: 'conmon: /usr/libexec/podman/conmon'
    path: /usr/libexec/podman/conmon
    version: 'conmon version 2.0.27, commit: '
  cpus: 4
  distribution:
    distribution: ubuntu
    version: "20.04"
  eventLogger: journald
  hostname: host
...
...
version:
  APIVersion: 3.1.0
  Built: 0
  BuiltTime: Thu Jan  1 03:00:00 1970
  GitCommit: ""
  GoVersion: go1.15.2
  OsArch: linux/amd64
  Version: 3.1.0

root@host:~#

使用 Podman 命令

在终端中使用 Podman 由以下链组成。 这是 Unix 系统中大多数命令的标准顺序。

播客 [options] [command] [argument]

下表显示了 Podman 中最常用的命令。 大多数类似于 Docker 命令,除了一些 Docker 无法执行的功能。

查看表

命令描述
播客——帮助检索 Podman 帮助信息。
人播客检索详细的 Podman 信息。
podman search someimage查找图像(与大多数 Unix 程序一样)。 输入图像名称。
podman pull someregistry.org/someimage下载图像。 输入注册表和图像的名称。
podman someimagespodman search someimage. 使用图像的全名(例如 someregistry.org/someimages/version1.0.1),因为它们存储在不同的注册表中。
podman run -dt -p 8080: 80 / tcp someregistry.org/someimages启动容器。
播客ps查看正在运行的容器列表。
podman ps -aB查看所有容器的列表(已创建、已完成和已启动)。
podman 检查-l | grep ip地址查看正在运行的容器。
容器可以在非 root 模式下工作,因此不会为其分配 IP 地址。
播客日志 -l查看容器日志。
播客顶部 -l查看 pid 容器。
播客停止 -l停止一个容器。
播客 rm -l移除一个容器。

与 Podman 一起练习

在 Podman 中使用注册表

现在让我们练习一下。 Podman 使用多个容器注册表,并允许您通过注册表的顺序列表搜索图像。 如果您需要使用特定容器,请登录或使用全名图像。

使用以下命令打开文件。

root@host:~# nano /etc/containers/registries.conf

要添加您要搜索的注册表,请输入以下信息。 注册机构应出现在方括号内并用双引号括起来。 用逗号分隔每个注册表。

不合格的搜索注册表= [“docker.io”, “quay.io”, “example.registries.com”]

在上述 example,如果我们搜索 image001,它会首先搜索 docker.io 注册表。 如果没有找到,它将转到 quay.io 注册表,然后 example.registries.com 直到找到图像。

Podman 使用 Sudo

现在我们将看看如何将 Podman 与 Sudo 结合使用。 正如我们在文章开头所说,Podman 默认无需 root 即可运行,但您通常需要 root 权限才能使用图像。 如果您使用 Docker 镜像,通常需要 root 权限,因为已经在 root 环境中开发和运行了许多容器。 Docker Engine 19.03 及更高版本可以配置为在没有 root 访问权限的情况下运行。

所有使用的 Podman 命令都以 sudo,就像在 example 以下。

sudo podman run hello-world

使用 Podman 权限运行容器

根用户权限分为称为功能的单元。 Linux 功能 通过限制容器内启用的命名空间的数量来限制 root 的权力,禁用正常的安全协议。

要启动权限,请使用以下命令之一,具体取决于您的根级别访问权限。

sudo podman run --privileged ...
podman run --privileged

使用 –cap-add 标志而不是 –privileged 为容器的用户提供其中的所有功能。

sudo podman run --cap-add = all
podman run --cap-add = all

使用 –cap-add 允许用户访问容器的功能,但不会禁用正常的安全协议。 最好不要使用 –privilege 作为安全预防措施。

结论

Podman 在开发、管理和运行容器和镜像方面拥有巨大的力量。 我们回顾了如何在 Ubuntu 20.04 上安装 Podman,各种 Podman 命令,并在终端中进行了练习。 Podman 是您的开发人员或开发团队利用和改进其应用程序性能的绝佳工具。

在我们的产品页面上了解有关 Liquid Web 解决方案的更多信息。 我们有适用于任何项目的产品,无论大小。