Podman 使用

在无根环境中的基本设置和使用Podman

目录

在无根环境中的基本设置和使用Podman

cgroup V2 支持

管理员操作

安装 slirp4netns

启用非特权 ping

用户操作


在允许没有 root 权限的用户运行 Podman 之前,管理员必须安装或构建 Podman 并完成以下配置。

cgroup V2 支持

cgroup V2 Linux 内核功能允许用户限制无根容器可以使用的资源量。如果您运行 Podman 的 Linux 发行版启用了 cgroup V2,那么您可能需要更改默认的 OCI 运行时。一些旧版本runc不能与 cgroup V2 一起使用,您可能必须切换到替代的 OCI 运行时crun。

 //默认是有crun软件包,没有grep到需要
 yum -y install crun
 ​
 [root@podman ~]# yum list all|grep crun
 Repository salt-latest-repo is listed more than once in the configuration
 ------------------

也可以在命令行中使用以下--runtime选项打开对 cgroup V2 的替代 OCI 运行时支持:

 podman --runtime crun

或用于通过改变用于在“默认OCI运行时”的值的所有的命令containers.conf在系统级或在任一文件的用户级别runtime = "runc"runtime = "crun"

 [root@podman ~]# vim /usr/share/containers/containers.conf 
 .....以上多行省略
 runtime = "crun"      # 找到此行取消注释
 ......以下多行省略

管理员操作

安装 slirp4netns

 //默认是有slirp4netns软件包
 yum -y install slirp4netns
 ​
 [root@podman ~]# yum list all|grep slirp4netns
 Repository salt-latest-repo is listed more than once in the configuration
 slirp4netns.x86_64                                     -----------    

所述slirp4netns包提供了一种用于无特权的网络名称空间用户模式网络和必须在机器上,以便安装用于Podman在一个无根环境中运行。该包通过他们的包分发软件,如可在大多数Linux发行版yum,dnf,apt,zypper,等等。如果没有可用的package,您可以构建和安装slirp4netns从GitHub上。

确保fuse-overlayfs已安装

在rootless环境下使用Podman时,建议使用fuse-overlayfs而不是VFS文件系统。对于你所需要的fuse-overlayfs可执行文件提供$PATH。

//默认是有fuse-overlayfs软件包
yum -y install fuse-overlayfs
[root@podman ~]# yum list all|grep fuse-overlayfs
Repository salt-latest-repo is listed more than once in the configuration
fuse-overlayfs.x86_64                                     -------

配置 storage.conf文件

[root@podman ~]# vim /etc/containers/storage.conf
-------
# Path to an helper program to use for mounting the file system instead of mounting it
# directly.
mount_program = "/usr/bin/fuse-overlayfs" //取消注释
----

/etc/subuid和/etc/subgid配置

Rootless Podman 要求运行它的用户在文件/etc/subuid和/etc/subgid. 的shadow-utils或newuid包提供关于不同分布这些文件和它们必须在系统上安装。在这些文件中添加或更新条目需要 root 权限。以下是从无根 Podman是如何工作的

对于将被允许创建容器,更新每一个用户/etc/subuid,并/etc/subgid与看起来像以下字段用户。请注意,每个用户的值必须是唯一的。如果存在重叠,用户就有可能使用另一个用户的命名空间,并且他们可能会破坏它。

[root@podman ~]# useradd tom
[root@podman ~]# useradd jerry
[root@podman ~]# cat /etc/subuid
tom:100000:65536
jerry:165536:65536
[root@podman ~]# cat /etc/subgid
tom:100000:65536
jerry:165536:65536

这个文件的格式是 USERNAME:UID:RANGE

  • 中/etc/passwd或输出中列出的用户名getpwent。

  • 为用户分配的初始 UID。

  • 为用户分配的 UID 范围的大小。

这意味着为用户johndoe分配了 UID 100000-165535 以及/etc/passwd文件中的标准 UID 。注意:网络安装当前不支持此功能;这些文件必须在主机本地可用。无法使用 LDAP 或 Active Directory 进行配置。

如果您更新/etc/subuid或/etc/subgid,则需要停止该用户拥有的所有正在运行的容器并终止该用户在系统上运行的暂停进程。这可以通过使用podman system migrate命令自动完成,该命令将为用户停止所有容器并终止暂停进程。

该usermod程序可用于为用户分配 UID 和 GID,而不是直接更新文件。

usermod --add-subuids 200000-201000 --add-subgids 200000-201000 johndoe
grep johndoe /etc/subuid /etc/subgid
/etc/subuid:johndoe:200000:1001
/etc/subgid:johndoe:200000:1001
//更新文件方法,直接修改内容
[root@podman ~]# vim /etc/subuid

启用非特权 ping

在非特权容器中运行的用户可能无法使用该ping容器中的实用程序。

如果需要,管理员必须验证用户的 UID 是否在/proc/sys/net/ipv4/ping_group_range文件范围内。

要改变它的值,管理员可以使用类似的电话:sysctl -w "net.ipv4.ping_group_range=0 2000000"。

为了使更改保持不变,管理员需要添加一个文件.conf扩展名为/etc/sysctl.dcontains的文件net.ipv4.ping_group_range=0 $MAX_GID,其中$MAX_GID是运行容器的用户的最高可分配 GID。

//永久配置
[root@podman ~]# vim /etc/sysctl.conf
....以上多行忽略
sysctl -w "net.ipv4.ping_group_range=0 2000000" //添加此行到最后

用户操作

在无root环境中运行 Podman 所需的大部分工作都由机器管理员承担。

一旦管理员完成了机器上的设置,然后在/etc/subuid和/etc/subgid中完成了用户的配置,用户就可以开始使用他们想要的任何 Podman 命令。

用户配置文件

根驻留在Podman配置文件/usr/share/containers与覆盖/etc/containers。在无root环境中,它们驻留在${XDG_CONFIG_HOME}/containers(通常~/.config/containers)中并由每个单独的用户拥有。

三个主要的配置文件是container.conf、storage.conf和registries.conf。用户可以根据需要修改这些文件。

containers.conf 容器配置

Podman reads

  1. /usr/share/containers/containers.conf

  2. /etc/containers/containers.conf

  3. $HOME/.config/containers/containers.conf

如果它们以该顺序存在。每个文件都可以覆盖特定字段的前一个文件。

storage.conf 存储配置

在storage.conf顺序是

  1. /etc/containers/storage.conf

  2. $HOME/.config/containers/storage.conf

在无root Podman 中,某些字段/etc/containers/storage.conf被忽略。这些字段是:

 graphroot=""
  container storage graph dir (default: "/var/lib/containers/storage")
  Default directory to store all writable content created by container storage programs.
 ​
 runroot=""
  container storage run dir (default: "/run/containers/storage")
  Default directory to store all temporary writable content created by container storage programs.

在无root Podman 中,这些字段默认为

 graphroot="$HOME/.local/share/containers/storage"
 runroot="$XDG_RUNTIME_DIR/containers"

registries.conf 注册表配置

[root@podman ~]# vim /etc/containers/registries.conf
...以上多行省略
# # An array of host[:port] registries to try when pulling an unqualified image, in order.
unqualified-search-registries = ["docker.io"]  //修改此行

[[registry]]
prefix="docker.io"
location = "docker.mirrors.ustc.edu.cn" //修改此三行
...以下多行省略

registries 注册表

注册表配置按此顺序读入

  1. /etc/containers/registries.conf

  2. /etc/containers/registries.d/*

  3. HOME/.config/containers/registries.conf

主目录中的文件应该用于根据个人需要配置无root Podman。默认情况下不会创建这些文件。用户可以从中复制/usr/share/containers或/etc/containers修改文件。

Authorization files 授权文件

podman login和podman logout命令使用的默认授权文件位于${XDG_RUNTIME_DIR}/containers/auth.json.

[root@podman ~]# cat /run/user/0/containers/auth.json 
{
	"auths": {
		"docker.io": {
			"auth": "aHloeHkwMjA2OjEzODg2NjAwMxxxxx" //授权key
		}
	}

使用卷 无根 Podman 不是,也永远不会是根;它不是setuid二进制文件,运行时不会获得任何特权。相反,Podman 使用用户命名空间来移动它在主机上(通过newuidmap和newgidmap可执行文件)和您自己的用户在 Podman 创建的容器中有权访问的用户块的 UID 和 GID 。

如果您的容器以 root 用户运行,那么root在容器中实际上是您在主机上的用户。UID / GID 1是第一个UID / GID在用户在地图中指定/etc/subuid和/etc/subgid等,如果你安装来自主机的目录放入容器中作为一个无根的用户,并在容器中创建该目录中的文件作为根,你会看到它实际上由您的用户在主机上拥有。

所以,例如,

//普通用户和root用户,创建容器都有root权限
[tom@103 ~]$ mkdir data
[tom@103 ~]$ podman run -it -v /home/tom/data/:/data busybox /bin/sh
/ # ls
bin   dev   home  root  sys   usr
data  etc   proc  run   tmp   var
/ # cd data/
/data # touch abc
/data # ls -l
total 0
-rw-r--r--    1 root     root             0 Dec 15 09:02 abc
[root@podman ~]# ll /home/tom/data/
总用量 0
[root@podman ~]# ll /home/tom/data/
总用量 0
-rw-r--r-- 1 tom tom 0 12月 15 17:02 abc
//容器使用普通权限--userns=keep-id -v
[tom@103 ~]$ podman run -it --userns=keep-id -v /home/tom/data/:/data busybox /bin/sh
~ $ id
uid=1000(tom) gid=1000(tom)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值