podman的应用
文章目录
1. podman的工作原理
-
Podman 可以管理和运行任何符合 OCI(Open Container Initiative)规范的容器和容器镜像。Podman 提供了一个与 Docker 兼容的命令行前端来管理 Docker 镜像。
-
Podman是RedHat开发的一个用户友好的容器调度器,是RedHat 8和CentOS 8中默认的容器引擎
-
它是一款集合了命令集的工具,设计初衷是为了处理容器化进程的不同任务,可以作为一个模块化框架工作
2. podman与docker的区别
- dockers在实现CRI的时候,它需要一个守护进程,其次需要以root运行
- podman不需要守护程序,也不需要root用户运行
- 在docker的运行体系中,需要多个daemon才能调用到OCI的实现RunC
- 在容器管理的链路中,Docker Engine的实现就是dockerd
- daemon,它在linux中需要以root运行,dockerd调用containerd,containerd调用containerd-shim,然后才能调用runC
- podman直接调用OCI,runtime(runC),通过common作为容器进程的管理工具,但不需要dockerd这种以root身份运行的守护进程
- 在podman体系中,有个称之为common的守护进程,其运行路径通常是/usr/libexec/podman/conmon,它是各个容器进程的父进程,每个容器各有一个,common的父则通常是1号进程。podman中的common其实相当于docker体系中的containerd-shim
[root@SYL2 ~]# podman top -l 单进程多线程
USER PID PPID %CPU ELAPSED TTY TIME COMMAND
root 1 0 0.000 7m3.763026951s ? 0s httpd -DFOREGROUND
www-data 7 1 0.000 7m3.763188302s ? 0s httpd -DFOREGROUND
www-data 8 1 0.000 7m3.763251984s ? 0s httpd -DFOREGROUND
www-data 9 1 0.000 7m3.763304541s ? 0s httpd -DFOREGROUND
[root@SYL2 ~]#
- podman不需要守护进程,而dorker需要守护进程
2.1 Podman的使用与docker的区别
- podman与docker的命令基本兼容,都包括容器运行时(run/start/kill/ps/inspect),本地镜像(images/rmi/build)、镜像仓库(login/pull/push)等。因此podman的命令行工具与docker类似,比如构建镜像、启停容器等。甚至可以通过
alias docker=podman
可以进行替换。因此,即便使用了podman,仍然可以使用http://docker.io作为镜像仓库,这也是兼容性最关键的部分。
3. podman的应用
- podman的常用命令
容器
podman run 创建并启动容器
podman start 启动容器
podman ps 查看容器
podman stop 终止容器
podman restart 重启容器
podman attach 进入容器
podman exec 进入容器
podman export 导出容器
podman import 导入容器快照
podman rm 删除容器
podman logs 查看日志
podman info 显示整个系统的信息
镜像
podman search 检索镜像
podman pull 获取镜像
podman images 列出镜像
podman image ls 列出镜像
podman rmi 删除镜像
podman image rm 删除镜像
podman save 导出镜像
podman load 导入镜像
podmanfile 定制镜像(三个)
podman build 构建镜像
podman run 运行镜像
podmanfile 常用指令(四个)
COPY 复制文件
ADD 高级复制
CMD 容器启动命令
ENV 环境变量
EXPOSE 暴露端口
3.1 安装podman
[root@SYL2 ~]# yum list all|grep podman
[root@SYL2 ~]# yum -y install podman
3.2 配置加速器
[root@SYL2 ~]# cd /etc/containers/
[root@SYL2 containers]# ls
certs.d policy.json registries.conf.d storage.conf
oci registries.conf registries.d
[root@SYL2 containers]# vim registries.conf
[root@SYL2 containers]# cat registries.conf
#unqualified-search-registries = ["registry.fedoraproject.org", "registry.access.redhat.com", "registry.centos.org", "docker.io"]
unqualified-search-registries = ["docker.io"]
[[registry]]
prefix = "docker.io"
location = "szudx43q.mirror.aliyuncs.com"
3.3 使用podman
1.拉取镜像
[root@SYL2 ~]# 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@SYL2 ~]#
3.查看镜像
[root@SYL2 ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/busybox latest beae173ccac6 4 months ago 1.46 MB
docker.io/library/httpd latest dabbfbe0c57b 4 months ago 148 MB
3.删除镜像
[root@SYL2 ~]# podman rmi busybox
Untagged: docker.io/library/busybox:latest
Deleted: beae173ccac6ad749f76713cf4440fe3d21d1043fe616dfbe30775815d1d0f6a
4.运行容器
[root@SYL2 ~]# podman run -d --rm --name web -p 80:80 httpd
22462ebf22e2946294a3792a92b94cafd89e5fd2692b74e9e29ae3b993abe156
[root@SYL2 ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
22462ebf22e2 docker.io/library/httpd:latest httpd-foreground 9 seconds ago Up 9 seconds ago 0.0.0.0:80->80/tcp web
[root@SYL2 ~]#
5.查看ip
[root@SYL2 ~]# podman inspect -l|grep -i ipaddr
"IPAddress": "10.88.0.2",
"IPAddress": "10.88.0.2",
6.访问
[root@SYL2 ~]# curl 10.88.0.2
<html><body><h1>It works!</h1></body></html>
[root@SYL2 ~]# podman inspect -l|grep -i ipaddr
"IPAddress": "10.88.0.3",
"IPAddress": "10.88.0.3",
[root@SYL2 ~]# curl 10.88.0.3
<html><body><h1>It works!</h1></body></html>
[root@SYL2 ~]# curl 10.88.0.3
<html><body><h1>It works!</h1></body></html>
[root@SYL2 ~]# curl 10.88.0.3
<html><body><h1>It works!</h1></body></html>
[root@SYL2 ~]# curl 10.88.0.3
<html><body><h1>It works!</h1></body></html>
[root@SYL2 ~]# curl 10.88.0.3/aa/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
</body></html>
[root@SYL2 ~]#
7.查看日志
[root@SYL2 ~]# podman logs -l
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.88.0.3. 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.3. Set the 'ServerName' directive globally to suppress this message
[Mon May 09 09:52:49.441134 2022] [mpm_event:notice] [pid 1:tid 140599089048896] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Mon May 09 09:52:49.441340 2022] [core:notice] [pid 1:tid 140599089048896] AH00094: Command line: 'httpd -D FOREGROUND'
10.88.0.1 - - [09/May/2022:09:54:31 +0000] "GET / HTTP/1.1" 200 45
10.88.0.1 - - [09/May/2022:09:54:34 +0000] "GET / HTTP/1.1" 200 45
10.88.0.1 - - [09/May/2022:09:54:35 +0000] "GET / HTTP/1.1" 200 45
10.88.0.1 - - [09/May/2022:09:54:36 +0000] "GET / HTTP/1.1" 200 45
10.88.0.1 - - [09/May/2022:09:54:49 +0000] "GET /aa/ HTTP/1.1" 404 196
[root@SYL2 ~]#
8.查看一个运行容器中的进程资源使用情况
[root@SYL2 ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
22462ebf22e2 docker.io/library/httpd:latest httpd-foreground 3 minutes ago Up 3 minutes ago 0.0.0.0:80->80/tcp web
[root@SYL2 ~]# podman top web
USER PID PPID %CPU ELAPSED TTY TIME COMMAND
root 1 0 0.000 3m35.476781373s ? 0s httpd -DFOREGROUND
www-data 7 1 0.000 3m35.47694501s ? 0s httpd -DFOREGROUND
www-data 8 1 0.000 3m35.477005755s ? 0s httpd -DFOREGROUND
www-data 9 1 0.000 3m35.477062934s ? 0s httpd -DFOREGROUND
www-data 91 1 0.000 2m6.477118614s ? 0s httpd -DFOREGROUND
[root@SYL2 ~]#
[root@SYL2 ~]# podman top -l 单进程多线程,一个进程,3个线程
USER PID PPID %CPU ELAPSED TTY TIME COMMAND
root 1 0 0.000 7m3.763026951s ? 0s httpd -DFOREGROUND
www-data 7 1 0.000 7m3.763188302s ? 0s httpd -DFOREGROUND
www-data 8 1 0.000 7m3.763251984s ? 0s httpd -DFOREGROUND
www-data 9 1 0.000 7m3.763304541s ? 0s httpd -DFOREGROUND
[root@SYL2 ~]#
9.停止一个运行中的容器
[root@SYL2 ~]# podman stop -l
22462ebf22e2946294a3792a92b94cafd89e5fd2692b74e9e29ae3b993abe156
[root@SYL2 ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@SYL2 ~]#
10.制作一个镜像
[root@SYL2 ~]# mkdir busybox
[root@SYL2 ~]# cd busybox/
[root@SYL2 busybox]# vim Podmanfile
[root@SYL2 busybox]# podman build -f Podmanfile -t busybox:v0.1 . //或者直接用dockerfile不用-f
STEP 1/2: FROM busybox
STEP 2/2: ENV a 10
COMMIT busybox:v0.1
--> 7202249ab27
Successfully tagged localhost/busybox:v0.1
7202249ab271843f06e31a24938b7d8aa2f6548282ec59e963691a6b2f201122
[root@SYL2 busybox]# cat Podmanfile
FROM busybox
11.ENV a 10
[root@SYL2 busybox]#
12.测试
[root@SYL2 ~]# podman run -it --rm --name myweb busybox:v0.1 /bin/sh
/ # echo $a
10
/ #
13.修改名字
[root@SYL2 ~]# podman tag localhost/busybox:v0.1 docker.io/xiaoyinguhong/busybox:0509
[root@SYL2 ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/busybox v0.1 7202249ab271 9 minutes ago 1.46 MB
docker.io/xiaoyinguhong/busybox 0509 7202249ab271 9 minutes ago 1.46 MB
docker.io/library/busybox latest beae173ccac6 4 months ago 1.46 MB
docker.io/library/httpd latest dabbfbe0c57b 4 months ago 148 MB
[root@SYL2 ~]#
14.登录账号
docker.io/library/httpd latest dabbfbe0c57b 4 months ago 148 MB
[root@SYL2 ~]# podman login docker.io
Username: xiaoyinguhong
Password:
Login Succeeded!
15.上传
[root@SYL2 ~]# podman push docker.io/xiaoyinguhong/busybox:0509
Getting image source signatures
Copying blob 01fd6df81c8e done
Copying config 7202249ab2 done
Writing manifest to image destination
Storing signatures
[root@SYL2 ~]#
16.命令别名
[root@SYL2 ~]# alias docker='podman'
[root@SYL2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ac43eccd9f4e docker.io/library/httpd:latest httpd-foreground 3 hours ago Up 3 hours ago 0.0.0.0:80->80/tcp web
[root@SYL2 ~]#
3.4 用户操作
- 在允许没有root特权的用户运行Podman之前,管理员必须安装或构建Podman并完成以下配置cgroup(控制组) V2Linux内核功能允许用户限制普通用户容器可以使用的资源
- 如果使用cgroupV2启用了运行Podman的Linux发行版,则可能需要更改默认的OCI运行时。某些较旧的版本runc不适用于cgroupV2,必须切换到备用OCI运行时crun。
[root@SYL2 ~]# rpm -qa|grep crun
[root@SYL2 ~]# dnf list all|grep crun
Failed to set locale, defaulting to C.UTF-8
crun.x86_64 1.4.3-1.module_el8.7.0+1106+45480ee0 appstream
[root@SYL2 ~]# dnf -y install crun
- 修改配置文件,运行时,改成crun
[root@SYL2 ~]# cd /usr/share/containers/
[root@SYL2 containers]# ls
containers.conf mounts.conf seccomp.json selinux
[root@SYL2 containers]# vim containers.conf
[root@SYL2 containers]# cat containers.conf |grep crun
runtime = "crun"
- 更改之前启动的是runc,之后是crun
[root@SYL2 ~]# podman run -d httpd
cdc992e356c8f2693d2da26a494e5e84face70289b21e46730917a6dd017e472
[root@SYL2 ~]# podman inspect -l|grep -i runtime
"OCIRuntime": "crun",
"Runtime": "oci",
"CpuRealtimeRuntime": 0,
[root@SYL2 ~]# podman inspect web|grep -i runtime
"OCIRuntime": "runc",
"Runtime": "oci",
"CpuRealtimeRuntime": 0,
[root@SYL2 ~]#
- 安装 slirp4netns 和 fuse-overlayfs 在普通用户环境中使用Podman时,建议使用fuse-overlayfs而不是VFS文件系统(虚拟文件系统)
[root@SYL2 ~]# dnf list all|grep slir
Failed to set locale, defaulting to C.UTF-8
libslirp.x86_64 4.4.0-1.module_el8.6.0+926+8bef8ae7 @appstream
slirp4netns.x86_64 1.1.8-2.module_el8.7.0+1106+45480ee0 @appstream
libslirp-devel.x86_64 4.4.0-1.module_el8.6.0+926+8bef8ae7 appstream
[root@SYL2 ~]# dnf list all|grep fuse
[root@SYL2 ~]# dnf -y install slirp4netns fuse-overlayfs
- 配置/etc/containers/storage.conf文件修改fuse-overlayfs
[root@SYL2 ~]# cd /etc/containers/
[root@SYL2 containers]# ls
certs.d policy.json registries.conf.d storage.conf
oci registries.conf registries.d
[root@SYL2 containers]# vim storage.conf
[root@SYL2 containers]# cat storage.conf |grep mount_program
mount_program = "/usr/bin/fuse-overlayfs"
# the "user.containers.override_stat" xattr and the "mount_program" option must
[root@SYL2 containers]#
- / etc / subuid和/ etc / subgid配置Podman要求运行它的用户在/ etc / subuid和/ etc / subgid文件中列出一系列UID,shadow-utils或newuid包提供这些文件
[root@SYL2 ~]# dnf list all|grep shadow
Failed to set locale, defaulting to C.UTF-8
shadow-utils.x86_64 2:4.6-16.el8 @anaconda
shadow-utils-subid.x86_64 2:4.6-16.el8 @baseos
shadow-utils-subid.i686 2:4.6-16.el8 baseos
[root@SYL2 ~]# rpm -qa|grep shadow-utils
shadow-utils-4.6-16.el8.x86_64
shadow-utils-subid-4.6-16.el8.x86_64
[root@SYL2 ~]#
可以在/ etc / subuid和/ etc / subgid查看,每个用户的值必须唯一且没有任何重叠。
[root@SYL2 ~]# cat /etc/subuid
[root@SYL2 ~]# cat /etc/subgid
[root@SYL2 ~]# useradd tom
[root@SYL2 ~]# cat /etc/subuid
tom:100000:65536
[root@SYL2 ~]# cat /etc/subgid
tom:100000:65536
[root@SYL2 ~]# useradd timi
[root@SYL2 ~]# cat /etc/subuid
tom:100000:65536
timi:165536:65536
[root@SYL2 ~]# cat /etc/subgid
tom:100000:65536
timi:165536:65536
[root@SYL2 ~]#
启动非特权ping,写到文件中下次重启生效
[root@SYL2 ~]# vim /etc/sysctl.conf
[root@SYL2 ~]# sysctl -p
net.ipv4.ping_group_range = 0 200000
[root@SYL2 ~]#
usermod程序可用于为用户分配 UID 和 GID,而不是直接更新文件
[root@SYL2 ~]# useradd titi
[root@SYL2 ~]# usermod --add-subuids 200000-201000 --add-subgids 200000-201000 titi
[root@SYL2 ~]# cat /etc/subuid
tom:100000:65536
timi:165536:65536
titi:231072:65536
titi:200000:1001
[root@SYL2 ~]# cat /etc/subgid
tom:100000:65536
timi:165536:65536
titi:231072:65536
titi:200000:1001
[root@SYL2 ~]#
- 用户配置文件,三个主要的配置文件是container.conf (修改crun)、storage.conf (fuse-overlayfs)和registries.conf (加速器)用户可以根据需要修改这些文件。
// 用户配置文件
[root@localhost ~]# cat /usr/share/containers/containers.conf
[root@localhost ~]# cat /etc/containers/containers.conf
[root@localhost ~]# cat ~/.config/containers/containers.conf //优先级最高
- 在普通用户中**/etc/containers/storage.conf**的一些字段将被忽略
[root@SYL2 ~]# cd /etc/containers/
[root@SYL2 containers]# ls
certs.d policy.json registries.conf.d storage.conf
oci registries.conf registries.d
[root@SYL2 containers]# vim storage.conf
[storage]
# Default Storage Driver, Must be set for proper operation.
driver = "overlay"
# Temporary storage location
runroot = "/run/containers/storage"
[root@SYL2 ~]# vim /etc/sysctl.conf
[root@SYL2 ~]# sysctl -p
net.ipv4.ping_group_range = 0 200000
user.max_user_namespaces = 15000
[root@SYL2 ~]#
- 普通用户是无法看见root用户的镜像的
[root@SYL2 ~]# podman image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/busybox v0.1 7202249ab271 3 hours ago 1.46 MB
docker.io/xiaoyinguhong/busybox 0509 7202249ab271 3 hours ago 1.46 MB
docker.io/library/busybox latest beae173ccac6 4 months ago 1.46 MB
docker.io/library/httpd latest dabbfbe0c57b 4 months ago 148 MB
[root@SYL2 ~]# su - tom
[tom@SYL2 ~]$ podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
[tom@SYL2 ~]$ podman run -it --rm busybox /bin/sh
/ #
3.5 卷的操作
-
容器与root用户一起运行,则root容器中的用户实际上就是主机上的用户。
-
UID GID是在/etc/subuid和/etc/subgid等中用户映射中指定的第一个UID GID。
-
如果普通用户的身份从主机目录挂载到容器中,并在该目录中以根用户身份创建文件,则会看到它实际上是你的用户在主机上拥有的。
-
[tom@SYL2 ~]$ id uid=1000(tom) gid=1000(tom) groups=1000(tom) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 [tom@SYL2 ~]$ podman ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [tom@SYL2 ~]$ podman run -it --rm busybox /bin/sh / # id uid=0(root) gid=0(root) groups=10(wheel) / # exit
3.5.1 使用卷
容器里
[tom@SYL2 ~]$ podman run -it --rm -v $(pwd)/data:/data:Z busybox /bin/sh
/ # cd data/
/data # ls
abc
/data # cat abc
123
/data # touch 456
/data #
用户中
[root@SYL2 ~]# su - tom
Last login: Mon May 9 21:40:15 CST 2022 on pts/0
[tom@SYL2 ~]$ ll
total 0
drwxrwxr-x. 2 tom tom 6 May 9 21:49 data
[tom@SYL2 ~]$ echo '123' > data/abc
[tom@SYL2 ~]$ ls
data
[tom@SYL2 ~]$ cd data/
[tom@SYL2 data]$ ls
456 abc
[tom@SYL2 data]$
- 在容器中查看
[tom@SYL2 ~]$ podman run -it --rm -v $(pwd)/data:/data:Z busybox /bin/sh
/ # cd data/
/data # ls
abc
/data # cat abc
123
/data # touch 456
/data # ls -l
total 4
-rw-r--r-- 1 root root 0 May 9 13:56 456
-rw-rw-r-- 1 root root 4 May 9 13:55 abc
/data #
发现在容器里面的文件的属主和属组都属于root,如何才能让其属于tom用户
[tom@SYL2 ~]$ podman run -it --rm -v $(pwd)/data:/data:Z --userns=keep-id busybox /bin/sh
~ $ cd data/
/data $ ls -l
total 4
-rw-r--r-- 1 tom tom 0 May 9 13:56 456
-rw-rw-r-- 1 tom tom 4 May 9 13:55 abc
/data $ id tom
uid=1000(tom) gid=1000(tom) groups=1000(tom)
/data $
- 使用普通用户映射容器端口时会报“ permission denied”的错误
[tom@SYL2 ~]$ podman run -d --name web -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
[tom@SYL2 ~]$
- 大于1024端口
[tom@SYL2 ~]$ podman run -d --name web1 -p 8080:80 httpd
01cf309d6ef3e613e098178815beec3f1c1c2a7cb2327435cac227789f603bbf
[tom@SYL2 ~]$ podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
01cf309d6ef3 docker.io/library/httpd:latest httpd-foreground 8 seconds ago Up 8 seconds ago 0.0.0.0:8080->80/tcp web1
[tom@SYL2 ~]$
- 在普通用户中不能修改/etc/sysctl.conf 文件,修改后可以暴露1024以内的端口号
管理员
[root@SYL2 ~]# vim /etc/sysctl.conf
[root@SYL2 ~]# sysctl -p
net.ipv4.ping_group_range = 0 200000
user.max_user_namespaces = 15000
net.ipv4.ip_unprivileged_port_start = 80
[root@SYL2 ~]#
普通用户
[tom@SYL2 ~]$ podman run -d --name web -p 80:80 httpd
a74b34ce80d32c78acf6ad04f9ac913f90664d1c925a89add587e4f15723c367
[tom@SYL2 ~]$ podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a74b34ce80d3 docker.io/library/httpd:latest httpd-foreground 5 seconds ago Up 5 seconds ago 0.0.0.0:80->80/tcp web
[tom@SYL2 ~]$
ipv4.ping_group_range = 0 200000
user.max_user_namespaces = 15000
net.ipv4.ip_unprivileged_port_start = 80
[root@SYL2 ~]#
普通用户
[tom@SYL2 ~]$ podman run -d --name web -p 80:80 httpd
a74b34ce80d32c78acf6ad04f9ac913f90664d1c925a89add587e4f15723c367
[tom@SYL2 ~]$ podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a74b34ce80d3 docker.io/library/httpd:latest httpd-foreground 5 seconds ago Up 5 seconds ago 0.0.0.0:80->80/tcp web
[tom@SYL2 ~]$