在允许没有root特权的用户运行Podman之前,管理员必须安装或构建Podman并完成以下配置
cgroup V2Linux内核功能允许用户限制普通用户容器可以使用的资源,如果使用cgroupV2启用了运行Podman的Linux发行版,则可能需要更改默认的OCI运行时。某些较旧的版本runc不适用于cgroupV2,必须切换到备用OCI运行时crun
[root@master ~]# yum -y install crun
[root@master ~]# vim /usr/share/containers/containers.conf
//修改
runtime = "crun"
[root@master ~]# podman run -d --name web -p 80:80 docker.io/library/nginx
[root@master ~]# podman inspect web | grep crun
"OCIRuntime": "crun",
"crun",
[root@master ~]# yum -y install slirp4netns^C
[root@master ~]# yum -y install fuse-overlayfs
[root@master ~]# vim /etc/containers/storage.conf
//取消注释
mount_program = "/usr/bin/fuse-overlayfs"
/etc/subuid和/etc/subgid配置
Podman要求运行它的用户在/ etc/subuid和/etc/subgid文件中列出一系列UID,shadow-utils或newuid包提供这些文件
[root@master ~]# yum -y install shadow-utils
//可以在/ etc / subuid和/ etc / subgid查看,每个用户的值必须唯一且没有任何重叠。
[root@master ~]# useradd yxt
[root@master ~]# cat /etc/subuid
mysql:100000:65536
yxt:165536:65536
[root@master ~]# cat /etc/subgid
mysql:100000:65536
yxt:165536:65536
// 启动非特权ping
//表示大于100000的用户以操作podman
[root@master ~]# vim /etc/sysctl.conf
net.ipv4.ping_group_range=0 200000
这个文件的格式是 USERNAME:UID:RANGE中/etc/passwd或输出中列出的用户名getpwent。
该usermod程序可用于为用户分配 UID 和 GID,而不是直接更新文件。
[root@master ~]# grep yxt /etc/subuid
yxt:165536:65536
[root@master ~]# grep yxt /etc/subgid
yxt:165536:65536
[root@master ~]# usermod --del-subuids 165536-231072 --del-subgids 165536-231072 yxt
[root@master ~]# cat /etc/subuid
mysql:100000:65536
[root@master ~]# usermod --add-subuids 200000-201000 --add-subgids 200000-201000 yxt
[root@master ~]# cat /etc/subuid
mysql:100000:65536
yxt:200000:1001
[root@master ~]#
[root@master ~]# cat /etc/subgid
mysql:100000:65536
yxt:200000:1001
三个主要的配置文件是container.conf、storage.conf和registries.conf。用户可以根据需要修改这些文件。
/usr/share/containers/containers.conf
/etc/containers/containers.conf
~/.config/containers/containers.conf //优先级最高
1./etc/containers/storage.conf
2.$HOME/.config/containers/storage.conf
在普通用户中/etc/containers/storage.conf的一些字段将被忽略
[root@master ~]# vim /etc/containers/storage.conf
driver = "overlay" #此处改为overlay
mount_program = "/usr/bin/fuse-overlayfs" #取消注释
//最大允许15000个用户能控制podman
[root@master ~]# vim /etc/sysctl.conf
user.max_user_namespaces = 15000
[root@master ~]#
在普通用户中这些字段默认
graphroot=“$ HOME/.local/share/containers/storage”
runroot=“$XDG_RUNTIME_DIR/containers”
配置按此顺序读入,这些文件不是默认创建的,可以从/usr/share/containers或复制文件/etc/containers并进行修改。
/etc/containers/registries.conf
/etc/containers/registries.d/*
HOME/.config/containers/registries.conf
授权文件
此文件里面写了docker账号的密码,以加密方式显示
[root@master ~]# podman login
Username: yexiaotian
Password:
Login Succeeded!
[root@master ~]# cat /run/user/0/containers/auth.json
{
"auths": {
"docker.io": {
"auth": "eWV4aWFvdGlhbjp5ZTIyMzc5NjY0NTE="
},
"master.example.com": {
"auth": "YWRtaW46SGFyYm9yMTIzNDU="
}
}
[root@master ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 0c60dbf09037 29 hours ago 1.47 MB
docker.io/library/busybox latest 7a80323521cc 2 weeks ago 1.47 MB
quay.io/yxt/busybox v0.1 7a80323521cc 2 weeks ago 1.47 MB
docker.io/library/nginx latest 605c77e624dd 7 months ago 146 MB
docker.io/library/httpd latest dabbfbe0c57b 7 months ago 148 MB
docker.io/library/registry latest b8604a3fe854 9 months ago 26.8 MB
quay.io/centos/centos latest 300e315adb2f 20 months ago 217 MB
//普通用户
[root@master ~]# su - yxt
Last login: Tue Aug 16 20:46:43 CST 2022 on pts/0
[yxt@yxt ~]$ podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
[yxt@yxt ~]$ podman run -dit --name wbe1 -p 8080:80 httpd
[yxt@yxt ~]$ podman inspect -l | grep -i address
"IPAddress": "",
"GlobalIPv6Address": "",
"MacAddress": "",
"LinkLocalIPv6Address": "",
[yxt@yxt ~]$ podman exec -it wbe1 /bin/bash
root@d27ac8d3431a:/usr/local/apache2# cd htdocs/
root@d27ac8d3431a:/usr/local/apache2/htdocs# echo "ni haols" > index.html
[root@master ~]# curl 192.168.160.137:8080
ni haols!
[yxt@master ~]$ podman rm -f wbe1
[yxt@master ~]$ mkdir abc
[yxt@master ~]$ podman run -dit --name web1 -v /home/yxt/abc/:/adb -p 8080:80 httpd
[yxt@master ~]$ podman exec -it web1 /bin/sh
# ls /
adb boot etc lib media opt root sbin sys usr
bin dev home lib64 mnt proc run srv tmp var
# touch /adb/123
# ls -l
total 0
-rw-r--r-- 1 root root 0 Aug 16 13:25 123
//在主机上查看
[yxt@master ~]$ ll /home/yxt/abc/
total 0
-rw-r--r-- 1 yxt yxt 0 Aug 16 21:25 123
//写入文件
[yxt@master ~]$ echo "hello world" >> /home/yxt/abc/123
[yxt@master ~]$ cat /home/yxt/abc/123
hello world
//容器里查看
# cat 123
hello worll
[yxt@master ~]$ podman run -d -p 80:80 httpd
Error: rootlessport cannot expose privileged port 80, you can add 'net.ipv4.ip_unprivileged_port_start=80' to /etc/sysctl.conf (currently 1024), or choose a larger port number (>= 1024): listen tcp 0.0.0.0:80: bind: permission denied
//普通用户可以映射>= 1024的端口
[yxt@master ~]$ podman run -d -p 1024:80 httpd
[yxt@master ~]$ ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 *:1024 *:*
LISTEN 0 128 *:8080 *:*
配置echo ‘net.ipv4.ip_unprivileged_port_start=80’ >> /etc/sysctl.conf后可以映射大于等于80的端口
[yxt@master ~]$ exit
[root@master ~]# vim /etc/sysctl.conf
net.ipv4.ip_unprivileged_port_start=80
[root@master ~]# sysctl -p
net.ipv4.ping_group_range = 0 200000
user.max_user_namespaces = 15000
net.ipv4.ip_unprivileged_port_start = 80
[yxt@master ~]$ podman run -d -p 81:80 httpd
c31d04aa089fc9bb3ef56653d16890d38fa0d5443b30f85e605e2fef1d2ea6fb
[yxt@master ~]$ ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 *:81 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 *:8080 *:*
[yxt@master ~]$ podman run -dit --name web1 -v /home/yxt/abc/:/abc:Z --userns=keep-id -p 80:80 busybox
d17d9cf7147b8502430328c403b118e0f66f6f67e95dc49d90eb4a3770df201d
[yxt@master ~]$ podman exec -it web1 /bin/sh
~ $ cd abc/
/abc $ touch 1 2 3 4 5
~ $ ls -l abc
total 0
-rw-r--r-- 1 yxt yxt 0 Aug 16 14:45 1
-rw-r--r-- 1 yxt yxt 0 Aug 16 14:45 2
-rw-r--r-- 1 yxt yxt 0 Aug 16 14:45 3
-rw-r--r-- 1 yxt yxt 0 Aug 16 14:45 4
-rw-r--r-- 1 yxt yxt 0 Aug 16 14:45 5