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



在允许没有root权限的用户运行Podman之前,管理员必须安装或构建Podman并完成以下配置。
cgroup V2Linux内核功能允许用户限制普通用户容器可以使用的资源,如果使用cgroupV2启用了运行Podman的Linux发行版,则可能需要更改默认的OCI运行时。某些较旧的版本runc不适用于cgroupV2,必须切换到备用OCI运行时crun。

[root@localhost ~]# dnf -y install crun  //centos8系统自带
[root@localhost ~]# vim /usr/share/containers/containers.conf
# Default OCI runtime
#
runtime = "crun"//更改为这个
#runtime = "runc"//注释掉
[root@localhost ~]# podman run -d --name web nginx
//拉取镜像并启动web容器
Resolving "nginx" using unqualified-search registries (/etc/containers/registries.conf)
Trying to pull docker.io/library/nginx:latest...
Getting image source signatures
Copying blob 186b1aaa4aa6 done  
Copying blob b4df32aa5a72 done  
Copying blob a9edb18cadd1 done  
Copying blob 589b7251471a done  
Copying blob a2abf6c4d29d done  
Copying blob a0bcbecc962e done  
Copying config 605c77e624 done  
Writing manifest to image destination
Storing signatures
2ec76edba3d4bd735765edde236783d0ede72cbd81cc26be96dd9045fc4fd75a
[root@localhost ~]# podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS             PORTS       NAMES
2ec76edba3d4  docker.io/library/nginx:latest  nginx -g daemon o...  59 seconds ago  Up 58 seconds ago              web
[root@localhost ~]# podman inspect web |grep -i ociruntime
        "OCIRuntime": "crun",

安装slirp4netns和fuse-overlayfs

在普通用户环境中使用Podman时,建议使用fuse-overlayfs而不是VFS文件系统,至少需要版本0.7.6。现在新版本默认就是了。

[root@localhost ~]# dnf -y install slirp4netns
[root@localhost ~]# dnf -y install fuse-overlayfs
[root@localhost ~]# vim /etc/containers/storage.conf
mount_program = "/usr/bin/fuse-overlayfs" //取消注释

subuid和subgid配置

Podman要求运行它的用户在/ etc / subuid和/ etc / subgid文件中列出一系列UID,shadow-utils或newuid包提供这些文件

[root@localhost ~]# dnf -y install shadow-utils
//可以在/etc/subuid和/etc/subgid查看,每个用户的值必须唯一且没有任何重叠。
[root@localhost ~]# useradd lnh
[root@localhost ~]# useradd lnh2
[root@localhost ~]# cat /etc/subuid
lnh:100000:65536
lnh2:165536:65536
[root@localhost ~]# cat /etc/subgid
lnh:100000:65536
lnh2:165536:65536

启动非特权ping

[root@localhost ~]# sysctl -w "net.ipv4.ping_group_range=0 200000"
net.ipv4.ping_group_range = 0 200000
//0 200000就是从100000开始,到200000区间内的用户都可使用podman
也可以在/etc/sysctl.conf里面修改配置文件net.ipv4.ping_group_range = 0 200000使其永久生效

这个文件的格式是 USERNAME:UID:RANGE中/etc/passwd或输出中列出的用户名getpwent。

为用户分配的初始 UID。
为用户分配的 UID 范围的大小。
该usermod程序可用于为用户分配 UID 和 GID,而不是直接更新文件。

[root@localhost ~]# usermod --add-subuids 200000-201000 --add-subgids 200000-201000 lnh
[root@localhost ~]# cat /etc/subuid
lnh:100000:65536
lnh2:165536:65536
lnh:200000:1001
[root@localhost ~]# cat /etc/subgid
lnh:100000:65536
lnh2:165536:65536
lnh:200000:100
也可以删除:
[root@localhost ~]# usermod --del-subuids 100000-165536 --del-subgids 100000-165536 lnh
[root@localhost ~]# cat /etc/subuid
lnh2:165536:65536
lnh:200000:1001
[root@localhost ~]# cat /etc/subgid
lnh2:165536:65536
lnh:200000:1001

用户配置文件

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

/usr/share/containers/containers.conf 
/etc/containers/containers.conf
$HOME/.config/containers/containers.conf //优先级最高
//如果它们以该顺序存在。每个文件都可以覆盖特定字段的前一个文件

配置storage.conf文件

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

在普通用户中/etc/containers/storage.conf的一些字段将被忽略

[root@localhost ~]# vim /etc/containers/storage.conf
# Default Storage Driver, Must be set for proper operation.
driver = "overlay"  //此处改为overlay
mount_program = "/usr/bin/fuse-overlayfs" //取消注释
如果版本为8以下,则需要做以下操作:
[root@localhost ~]# sysctl user.max_user_namespaces=15000  
或者
[root@localhost ~]# vim /etc/sysctl.conf 	
user.max_user_namepaces=15000			

在普通用户中这些字段默认

[root@localhost ~]# vim /etc/containers/storage.conf
# Temporary storage location
runroot = "/run/containers/storage"

# Primary Read/Write location of container storage
graphroot = "/var/lib/containers/storage"

registries.conf
配置按此顺序读入,这些文件不是默认创建的,可以从/usr/share/containers或复制文件/etc/containers并进行修改。

1./etc/containers/registries.conf
2./etc/containers/registries.d/*
3.HOME/.config/containers/registries.conf

授权文件

此文件里面写了docker账号的密码,以加密方式显示

[root@localhost ~]# podman login
Username: lvnanhai66
Password: 
Login Succeeded!
[root@localhost ~]# cat /run/user/0/containers/auth.json 
{
        "auths": {
                "docker.io": {
                        "auth": "bHZuYW5oYWk2NjoxOTk5MzI0cXdlcnQxMjM0"
                }
        }
}

普通用户是无法看见root用户的镜像的

[root@localhost ~]# podman images
REPOSITORY               TAG         IMAGE ID      CREATED       SIZE
docker.io/library/nginx  latest      605c77e624dd  7 months ago  146 MB
[root@localhost ~]# su - lnh
Last login: Wed Aug 17 11:28:29 CST 2022 on pts/0
[lnh@localhost ~]$ podman images
REPOSITORY  TAG         IMAGE ID    CREATED     SIZE
[lnh@localhost ~]$ podman ps -a
CONTAINER ID  IMAGE       COMMAND     CREATED     STATUS      PORTS       NAMES

容器与root用户一起运行,则root容器中的用户实际上就是主机上的用户。
UID GID是在/etc/subuid和/etc/subgid等中用户映射中指定的第一个UID GID。
如果普通用户的身份从主机目录挂载到容器中,并在该目录中以根用户身份创建文件,则会看到它实际上是你的用户在主机上拥有的。

使用卷

[lnh2@localhost ~]$ mkdir /home/lnh2/data
[lnh2@localhost ~]$ podman run -dit --name web -v /home/lnh2/data/:/data:Z -p 8080:80 httpd
Resolving "httpd" using unqualified-search registries (/etc/containers/registries.conf)
Trying to pull docker.io/library/httpd:latest...
Getting image source signatures
Copying blob dcc4698797c8 done  
Copying blob d982c879c57e done  
Copying blob a2abf6c4d29d done  
Copying blob 41c22baa66ec done  
Copying blob 67283bbdd4a0 done  
Copying config dabbfbe0c5 done  
Writing manifest to image destination
Storing signatures
b8d77d3d51351a8de4d2ae1eaada927230db901184bac7463b62db24d256d272
[lnh2@localhost ~]$ podman exec -it web /bin/bash
root@b8d77d3d5135:/usr/local/apache2# cd
root@b8d77d3d5135:~# cd /
root@b8d77d3d5135:/# ls
bin   data  etc   lib    media  opt   root  sbin  sys  usr
boot  dev   home  lib64  mnt    proc  run   srv   tmp  var
root@b8d77d3d5135:/# cd data/
root@b8d77d3d5135:/data# touch 123
root@b8d77d3d5135:/data# ls -l
total 0
-rw-r--r--. 1 root root 0 Aug 17 05:05 123

在主机上查看

[lnh2@localhost ~]$ ll data/
total 0
-rw-r--r--. 1 lnh2 lnh2 0 Aug 17 13:05 123
[lnh2@localhost ~]$ echo "hello lnh" >> 123
[lnh2@localhost ~]$ cat 123
hello lnh

容器里查看

root@b8d77d3d5135:/data# cat 123
hello lnh
内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/tushanbu/p/16594817.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!