一.podman的工作原理
Podman 是一个开源的容器运行时项目,可在大多数 Linux 平台上使用。Podman 提供与 Docker 非常相似的功能。正如前面提到的那样,它不需要在你的系统上运行任何守护进程,并且它也可以在没有 root 权限的情况下运行。
Podman 可以管理和运行任何符合 OCI(Open Container Initiative)规范的容器和容器镜像。Podman 提供了一个与 Docker 兼容的命令行前端来管理 Docker 镜像
二.与docker的区别
podman的定位也是与docker兼容,因此在使用上面尽量靠近docker。在使用方面,可以分成两个方面来说,一是系统构建者的角度,二是使用者的角度。
在系统构建者方面,用podman的默认软件,与docker的区别不大,只是在进程模型、进程关系方面有所区别。如果习惯了docker几个关联进程的调试方法,在podman中则需要适应。可以通过pstree命令查看进程的树状结构。总体来看,podman比docker要简单。由于podman比docker少了一层daemon,因此重启的机制也就不同了。
在使用者方面,podman与docker的命令基本兼容,都包括容器运行时(run/start/kill/ps/inspect),本地镜像(images/rmi/build)、镜像仓库(login/pull/push)等几个方面。因此podman的命令行工具与docker类似,比如构建镜像、只停容器等。甚至可以通过alias
docker=podman可以进行替换。因此,即便使用了podman,仍然可以使用http://docker.io作为镜像仓库,这也是兼容性最关键的部分
三.podman应用
1.安装podman
直接使用yum安装即可
[root@localhost ~]# yum -y install podman
2.配置加速器
[root@localhost ~]# cd /etc/containers/
[root@localhost containers]# ls
certs.d policy.json registries.conf.d storage.conf
oci registries.conf registries.d
[root@localhost containers]# vim registries.conf
unqualified-search-registries = ["docker.io"]
[[registry]]
prefix = "docker.io"
location = "onmth88j.mirror.aliyuncs.com" //直接到我们自己的阿里云账号上寻找地址,并且不需要http:// 直接写入地址
3.运行一个容器
[root@localhost ~]# podman run -d --name web -p 8080:80 httpd
Trying to pull docker.io/library/httpd:latest...
Getting image source signatures
Copying blob f646c69a26ec done
Copying blob ea2ca81c6d4c done
Copying blob 1fe172e4850f done
Copying blob 60dd7398e74e done
Copying blob e2fa1fe9b1ec done
Copying config c30a467716 done
Writing manifest to image destination
Storing signatures
e25fa57e1dbca4e4d1f56da53946af2a0e6594dbb024dd390b6a50101bb4fd2d
4.查看镜像
[root@localhost ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/httpd latest c30a46771695 2 weeks ago 148 MB
5.列出所有容器
[root@localhost ~]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f4343bc580bc docker.io/library/httpd:latest httpd-foreground 3 hours ago Up 3 hours ago 0.0.0.0:8080->80/tcp web
6.查看运行中容器的日志
[root@localhost ~]# podman logs --latest
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.88.0.4. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.88.0.4. Set the 'ServerName' directive globally to suppress this message
[Mon May 09 13:00:30.992017 2022] [mpm_event:notice] [pid 1:tid 140627538423104] AH00489: Apache/2.4.53 (Unix) configured -- resuming normal operations
[Mon May 09 13:00:31.010150 2022] [core:notice] [pid 1:tid 140627538423104] AH00094: Command line: 'httpd -D FOREGROUND'
10.88.0.1 - - [09/May/2022:13:04:53 +0000] "GET / HTTP/1.1" 200 45
192.168.12.1 - - [09/May/2022:13:05:04 +0000] "GET / HTTP/1.1" 200 45
192.168.12.1 - - [09/May/2022:13:05:05 +0000] "GET /favicon.ico HTTP/1.1" 404 196
192.168.12.1 - - [09/May/2022:13:05:56 +0000] "-" 408 -
7.查看一个运行容器中的进程资源使用情况
[root@localhost ~]# podman top web
USER PID PPID %CPU ELAPSED TTY TIME COMMAND
root 1 0 0.000 9m14.557254141s ? 0s httpd -DFOREGROUND
www-data 7 1 0.000 9m14.557764698s ? 0s httpd -DFOREGROUND
www-data 8 1 0.000 9m14.557828836s ? 0s httpd -DFOREGROUND
www-data 9 1 0.000 9m14.557959487s ? 0s httpd -DFOREGROUND
www-data 91 1 0.000 4m7.558020849s ? 0s httpd -DFOREGROUND
8.停止一个运行中的容器
[root@localhost ~]# podman stop web
web
[root@localhost ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9.删除容器
[root@localhost ~]# podman rm web
40e26221bfd7e8a4e1256775136ff085f761ed1bd46902a487941582c85e8ddc
[root@localhost ~]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
10.上传镜像
[root@localhost ~]# podman pull busybox
Resolved "busybox" as an alias (/etc/containers/registries.conf.d/000-shortnames.conf)
Trying to pull docker.io/library/busybox:latest...
Getting image source signatures
Copying blob 5cc84ad355aa done
Copying config beae173cca done
Writing manifest to image destination
Storing signatures
beae173ccac6ad749f76713cf4440fe3d21d1043fe616dfbe30775815d1d0f6a
[root@localhost ~]# cd test/
[root@localhost test]# cat podmanfile
FROM busybox
ENV a 10
[root@localhost test]# podman build -f podmanfile -t test:v0.1 .
STEP 1/2: FROM busybox
STEP 2/2: ENV a 10
COMMIT test:v0.1
--> c739e70ec08
Successfully tagged localhost/test:v0.1
c739e70ec08fb9a028df0053cf9c8f79a019dc7fbfcbd0efed0f23329e5663ae
[root@localhost test]# podman run -it test:v0.1 /bin/sh
/ # echo $a
10
[root@localhost ~]# podman login docker.io
Username: yukun629
Password:
Login Succeeded!
[root@localhost ~]# podman tag c739e70ec08f docker.io/yukun629/busybox:220509
[root@localhost ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/test v0.1 c739e70ec08f 4 minutes ago 1.46 MB
docker.io/yukun629/busybox 220509 c739e70ec08f 4 minutes ago 1.46 MB
docker.io/library/httpd latest c30a46771695 2 weeks ago 148 MB
docker.io/library/busybox latest beae173ccac6 4 months ago 1.46 MB
[root@localhost ~]# podman push docker.io/yukun629/busybox:220509
Getting image source signatures
Copying blob 01fd6df81c8e done
Copying config c739e70ec0 done
Writing manifest to image destination
Storing signatures
可查看容器已经上传
效果图
11.配置别名
[root@localhost ~]# alias docker='podman'
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5207b75cef2c localhost/test:v0.1 /bin/sh 8 minutes ago Exited (0) 8 minutes ago gracious_darwin
[root@localhost ~]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5207b75cef2c localhost/test:v0.1 /bin/sh 9 minutes ago Exited (0) 8 minutes ago gracious_darwin
四.普通用户使用
在允许没有root特权的用户运行Podman之前,管理员必须安装或构建Podman并完成以下配置cgroup V2Linux内核功能允许用户限制普通用户容器可以使用的资源,如果使用cgroupV2启用了运行Podman的Linux发行版,则可能需要更改默认的OCI运行时。某些较旧的版本runc不适用于cgroupV2,必须切换到备用OCI运行时crun
[root@localhost ~]# yum install -y crun
[root@localhost ~]# vim /usr/share/containers/containers.conf
runtime = "crun" //解除此条注释
#runtime = "runc" //注释此条
[root@localhost ~]# podman run -d --name web -p 80:80 httpd
60c643ff6ae54dcded95f5993bca25b43637c7a8bb3be68dc0d68c23a90b5d03
[root@localhost ~]# podman inspect web | grep crun
"OCIRuntime": "crun",
"crun",
安装slirp4netns和fuse-overlayfs在普通用户环境中使用Podman时,建议使用fuse-overlayfs而不是VFS文件系统,至少需要版本0.7.6。现在新版本默认就是了
[root@localhost ~]# yum -y install slirp4netns
[root@localhost ~]# yum -y install fuse-overlayfs
[root@localhost ~]# vim /etc/containers/storage.conf
mount_program = "/usr/bin/fuse-overlayfs" //取消注释
//可以在/ etc / subuid和/ etc / subgid查看,每个用户的值必须唯一且没有任何重叠
[root@localhost ~]# useradd tom
[root@localhost ~]# cat /etc/subuid
tom:100000:65536
[root@localhost ~]# cat /etc/subgid
tom:100000:65536
[root@localhost ~]# sysctl -w "net.ipv4.ping_group_range=0 200000"
net.ipv4.ping_group_range = 0 200000
[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ping_group_range=0 200000
[root@localhost ~]# sysctl -p //立即生效
net.ipv4.ping_group_range = 0 200000
这个文件的格式是 USERNAME:UID:RANGE中/etc/passwd或输出中列出的用户名getpwent。
- 为用户分配的初始 UID。
- 为用户分配的 UID 范围的大小。
该usermod程序可用于为用户分配 UID 和 GID,而不是直接更新文件
[root@localhost ~]# grep tom /etc/subuid /etc/subgid
/etc/subuid:tom:100000:65536
/etc/subuid:tom:200000:1001
/etc/subgid:tom:100000:65536
/etc/subgid:tom:200000:1001
用户配置文件
三个主要的配置文件是container.conf、storage.conf和registries.conf。用户可以根据需要修改这些文件。
container.conf
// 用户配置文件
[root@localhost ~]# cat /usr/share/containers/containers.conf
[root@localhost ~]# cat /etc/containers/containers.conf
[root@localhost ~]# cat ~/.config/containers/containers.conf //优先级最高
如果它们以该顺序存在。每个文件都可以覆盖特定字段的前一个文件。
配置storage.conf文件
/etc/containers/storage.conf
$HOME/.config/containers/storage.conf
在普通用户中/etc/containers/storage.conf的一些字段将被忽略
[root@localhost ~]# vim /etc/containers/storage.conf
driver = "overlay" //此处改为overlay
mount_program = "/usr/bin/fuse-overlayfs" //取消注释
[root@localhost ~]# sysctl user.max_user_namespaces=15000 #如果版本为8以下,则需要做以下操作:
在普通用户中这些字段默认
graphroot="$HOME/.local/share/containers/storage"
runroot="$XDG_RUNTIME_DIR/containers"
registries.conf
配置按此顺序读入,这些文件不是默认创建的,可以从/usr/share/containers或复制文件/etc/containers并进行修改
/etc/containers/registries.conf
/etc/containers/registries.d/*
HOME/.config/containers/registries.conf
授权文件
此文件里面写了docker账号的密码,以加密方式显示
[root@localhost ~]# podman login
Username: yukun629
Password:
Login Succeeded!
[root@localhost ~]# cat /run/user/0/containers/auth.json
{
"auths": {
"registry.fedoraproject.org": {
"auth": "MTMxNDQ0NDpIMjAxNy0xOA=="
}
}
}
普通用户是无法看见root用户的镜像的相反root用户也不能看到其他用去的镜像
[root@localhost ~]# su - tom
[tom@localhost ~]$ podman pull busybox
Resolved "busybox" as an alias (/etc/containers/registries.conf.d/000-shortnames.conf)
Trying to pull docker.io/library/busybox:latest...
Getting image source signatures
Copying blob 5cc84ad355aa done
Copying config beae173cca done
Writing manifest to image destination
Storing signatures
beae173ccac6ad749f76713cf4440fe3d21d1043fe616dfbe30775815d1d0f6a
[tom@localhost ~]$ podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/busybox latest bea183cqsxc6 4 months ago 1.46 MB
[root@localhost ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/test v0.1 c739e70ec08f 42 minutes ago 1.46 MB
docker.io/yukun629/busybox 0509 c739e70ec08f 42 minutes ago 1.46 MB
docker.io/library/httpd latest dabbfbe0c57b 4 months ago 148 MB
五.卷
容器与root用户一起运行,则root容器中的用户实际上就是主机上的用户。
UID GID是在/etc/subuid和/etc/subgid等中用户映射中指定的第一个UID GID。
如果普通用户的身份从主机目录挂载到容器中,并在该目录中以根用户身份创建文件,则会看到它实际上是你的用户在主机上拥有的
1.使用卷
[root@localhost tom]# su tom
[tom@localhost ~]$ pwd
/home/tom
[tom@localhost ~]$ mkdir /home/tom/data
[tom@localhost ~]$ podman run -it --rm -v "$(pwd)"/data:/data docker.io/library/busybox /bin/sh
/ # ls -l
total 16
drwxr-xr-x 2 root root 12288 Dec 29 21:12 bin
drwxrwxr-x 2 root root 6 May 9 10:57 data
drwxr-xr-x 5 root root 360 May 9 10:58 dev
drwxr-xr-x 1 root root 66 May 9 10:58 etc
drwxr-xr-x 2 nobody nobody 6 Dec 29 21:12 home
dr-xr-xr-x 299 nobody nobody 0 May 9 10:58 proc
drwx------ 1 root root 26 May 9 10:58 root
drwxr-xr-t 3 root root 42 May 9 10:58 run
dr-xr-xr-x 13 nobody nobody 0 Apr 23 06:15 sys
drwxrwxrwt 2 root root 6 Dec 29 21:12 tmp
drwxr-xr-x 3 root root 18 Dec 29 21:12 usr
drwxr-xr-x 4 root root 30 Dec 29 21:12 var
#开启一台新终端
[tom@localhost ~]$ ls -l //在容器内的root就是tom用户
总用量 0
drwxrwxr-x. 2 tom tom 6 5月 9 18:57 data
[tom@localhost ~]$ cd data/
[tom@localhost data]$ touch 12345
[tom@localhost data]$ ls
12345
#切回另外一个终端退出容器,在创建一个新的容器
[tom@localhost ~]$ podman run -it -v "$(pwd)"/data:/data:Z
docker.io/library/busybox /bin/sh
/ # ls
bin data dev etc home proc root run sys tmp usr var
/ # cd data/
/data # ls //可以看到创建的文件
12345
2.使用普通用户映射容器端口时会报“ permission denied”的错误
[tom@localhost ~]$ 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
3.普通用户可以映射>= 1024的端口
[tom@localhost ~]$ podman run -d -p 1024:80 httpd
211a3dbf093be8e2c8a8d1de53024bc2878160594addd41da083ef6c030d1a9a
[tom@localhost ~]$ ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 128 *:1024 *:*
LISTEN 0 128 [::]:111 [::]:*
4.配置echo ‘net.ipv4.ip_unprivileged_port_start=80’ >> /etc/sysctl.conf后可以映射大于等于80的端口
[root@localhost ~]# echo 'net.ipv4.ip_unprivileged_port_start=80' >> /etc/sysctl.conf
[root@localhost ~]# sysctl -p
net.ipv4.ip_unprivileged_port_start = 80
//回到tom用户查看
[tom@localhost ~]$ podman run -d -p 80:80 httpd //此时就可以使用80端口了
4dddc0f35f73cee86fdf5586e26b49dd492624599f9e35363a4f16eec2b8740b
[tom@localhost ~]$ ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 128 *:1024 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 *:80 *:*