服务器之家:专注于服务器技术及软件下载分享
分类导航

服务器资讯|IT/互联网|云计算|区块链|软件资讯|操作系统|手机数码|百科知识|免费资源|头条新闻|

服务器之家 - 新闻资讯 - 云计算 - Docker 大势已去,Podman 即将崛起

Docker 大势已去,Podman 即将崛起

2023-05-31 12:00未知服务器之家 云计算

文章来源:https://blog.csdn.net/qq_48289488/article/details/121905018 什么是Podman? Podman 是一个开源的容器运行时项目,可在大多数 Linux 平台上使用。Podman 提供与 Docker 非常相似的功能。正如前面提到的那样,它不需要在你的系统上运行任何


文章来源:https://blog.csdn.net/qq_48289488/article/details/121905018



什么是Podman?

Docker 大势已去,Podman 即将崛起

Podman 是一个开源的容器运行时项目,可在大多数 Linux 平台上使用。Podman 提供与 Docker 非常相似的功能。正如前面提到的那样,它不需要在你的系统上运行任何守护进程,并且它也可以在没有 root 权限的情况下运行。

Podman 可以管理和运行任何符合 OCI(Open Container Initiative)规范的容器和容器镜像。Podman 提供了一个与 Docker 兼容的命令行前端来管理 Docker 镜像。

Podman 官网地址:


Podman和Docker的主要区别是什么?

  • dockers在实现CRI的时候,它需要一个守护进程,其次需要以root运行,因此这也带来了安全隐患。
  • podman不需要守护程序,也不需要root用户运行,从逻辑架构上,比docker更加合理。
  • 在docker的运行体系中,需要多个daemon才能调用到OCI的实现RunC。
  • 在容器管理的链路中,Docker Engine的实现就是dockerd
  • daemon,它在linux中需要以root运行,dockerd调用containerd,containerd调用containerd-shim,然后才能调用runC。顾名思义shim起的作用也就是“垫片”,避免父进程退出影响容器的运训
  • podman直接调用OCI,runtime(runC),通过common作为容器进程的管理工具,但不需要dockerd这种以root身份运行的守护进程。
  • 在podman体系中,有个称之为common的守护进程,其运行路径通常是/usr/libexec/podman/conmon,它是各个容器进程的父进程,每个容器各有一个,common的父则通常是1号进程。podman中的common其实相当于docker体系中的containerd-shim。
Docker 大势已去,Podman 即将崛起

图中所体现的事情是,podman不需要守护进程,而dorker需要守护进程。在这个图的示意中,dorcker的containerd-shim与podman的common被归在Container一层。


Podman的使用与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,仍然可以使用docker.io作为镜像仓库,这也是兼容性最关键的部分。



Podman常用命令





容器

podmanrun创建并启动容器
podmanstart启动容器
podmanps查看容器
podmanstop终止容器
podmanrestart重启容器
podmanattach进入容器
podmanexec进入容器
podmanexport导出容器
podmanimport导入容器快照
podmanrm删除容器
podmanlogs查看日志

镜像

podmansearch检索镜像
podmanpull获取镜像
podmanimages列出镜像
podmanimageIs列出镜像
podmanrmi删除镜像
podmanimagerm删除镜像
podmansave导出镜像
podmanload导入镜像
podmanfile定制镜像(三个)
podmanbuild构建镜像
podmanrun运行镜像
podmanfile常用指令(四个)
COPY复制文件
ADD高级复制
CMD容器启动命令
ENV环境变量
EXPOSE暴露端口

部署 Podman

//安装podman
[root@localhost~]#yum-yinstallpodman

Podman 加速器

版本7配置加速器

//仓库配置
[root@localhost~]#vim/etc/containers/registries.conf
[registries.search]
#registries=["registry.access.redhat.com","registry.redhat.io","docker.io"]#这个是查找,从这三个地方查找

registries=["docker.io"]#如果只留一个,则只在一个源里查找
[[docker.io]]
location="j3m2itm3.mirror.aliyuncs.com"

版本8配置加速器

#unqualified-search-registries=["registry.fedoraproject.org","registry.access.redhat.com","registry.centos.org","docker.io"]#直接注释掉
unqualified-search-registries=["docker.io"]#添加一个docker.io
[[registry]]
prefix="docker.io"
location="j3m2itm3.mirror.aliyuncs.com"(不用加直接加地址)

使用 Podman

使用 Podman 非常的简单,Podman 的指令跟 Docker 大多数都是相同的。下面我们来看几个常用的例子:

运行一个容器

[root@localhost~]#podmanrun-d--namehttpddocker.io/library/httpd
Tryingtopulldocker.io/library/httpd...
Gettingimagesourcesignatures
Copyingblobe5ae68f74026done
Copyingblobd3576f2b6317done
Copyingblobbc36ee1127ecdone
Copyingblobf1aa5f54b226done
Copyingblobaa379c0cedc2done
Copyingconfigea28e1b82fdone
Writingmanifesttoimagedestination
Storingsignatures
0492e405b9ecb05e6e6be1fec0ac1a8b6ba3ff949df259b45146037b5f355035

//查看镜像
[root@localhost~]#podmanimages
REPOSITORYTAGIMAGEIDCREATEDSIZE
docker.io/library/httpdlatestea28e1b82f3111daysago148MB

列出运行的容器

[root@localhost~]#podmanps
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
0492e405b9ecdocker.io/library/httpd:latesthttpd-foregroundAboutaminuteagoUpAboutaminuteagohttpd

注意:如果在ps命令中添加-a,Podman 将显示所有容器。

检查正在运行的容器

您可以“检查”正在运行的容器的元数据和有关其自身的详细信息。我们甚至可以使用 inspect 子命令查看分配给容器的 IP 地址。由于容器以无根模式运行,因此未分配 IP 地址,并且该值将在检查的输出中列为“无”。

[root@localhost~]#podmaninspect-l|grepIPAddress\":
"SecondaryIPAddresses":null,
"IPAddress":"10.88.0.5",

[root@localhost~]#curl10.88.0.5
<html><body><h1>Itworks!</h1></body></html>

注意:-l 是最新容器的便利参数。您还可以使用容器的 ID 代替 -l。

查看一个运行中容器的日志

选项
--latest#最近的

[root@localhost~]#podmanlogs--latest
AH00558:httpd:Couldnotreliablydeterminetheserver'sfullyqualifieddomainname,using10.88.0.5.Setthe'ServerName'directivegloballytosuppressthismessage
AH00558:httpd:Couldnotreliablydeterminetheserver'
sfullyqualifieddomainname,using10.88.0.5.Setthe'ServerName'directivegloballytosuppressthismessage
[MonDec1315:17:53.6908442021][mpm_event:notice][pid1:tid140665160166720]AH00489:Apache/2.4.51(Unix)configured--resumingnormaloperations
[MonDec1315:17:53.6909462021][core:notice][pid1:tid140665160166720]AH00094:Commandline:'httpd-DFOREGROUND'
10.88.0.1--[13/Dec/2021:15:19:48+0000]"GET/HTTP/1.1"20045
10.88.0.1--[13/Dec/2021:15:20:47+0000]"GET/HTTP/1.1"20045

查看一个运行容器中的进程资源使用情况,可以使用top观察容器中的 nginx pid

语法:
podmantop<container_id>

[root@localhost~]#podmantophttpd
USERPIDPPID%CPUELAPSEDTTYTIMECOMMAND
root100.00015m38.599711321s?0shttpd-DFOREGROUND
www-data710.00015m38.599783256s?0shttpd-DFOREGROUND
www-data810.00015m38.599845342s?0shttpd-DFOREGROUND
www-data910.00015m38.599880444s?0shttpd-DFOREGROUND

停止一个运行中的容器

[root@localhost~]#podmanstop--latest
2f3edf712621d3a41e03fa8c7f6a5cdba56fbbad43a7a59ede26cc88f31006c4
[root@localhost~]#podmanps
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES

删除一个容器

[root@localhost~]#podmanrm--latest
2f3edf712621d3a41e03fa8c7f6a5cdba56fbbad43a7a59ede26cc88f31006c4
[root@localhost~]#podmanps-a
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES

以上这些特性基本上都和 Docker 一样,Podman 除了兼容这些特性外,还支持了一些新的特性。

上传镜像

例如,如果我们想在 docker.io 上分享我们新建的 Nginx 容器镜像,这很容易。首先登录码头:

[root@localhostnginx]#tree
.
├──Dockerfile
└──files
└──nginx-1.20.1.tar.gz

[root@localhostnginx]#catDockerfile
FROMdocker.io/library/centos

ENVPATH/usr/local/nginx/sbin:$PATH
ADDfiles/nginx-1.20.1.tar.gz/usr/src
RUNuseradd-r-M-s/sbin/nologinnginx&&\
yum-yinstallpcre-developensslopenssl-develgd-develgccgcc-c++make&&\
mkdir-p/var/log/nginx&&\
cd/usr/src/nginx-1.20.1&&\
./configure\
--prefix=/usr/local/nginx\
--user=nginx\
--group=nginx\
--with-debug\
--with-http_ssl_module\
--with-http_realip_module\
--with-http_image_filter_module\
--with-http_gunzip_module\
--with-http_gzip_static_module\
--with-http_stub_status_module\
--http-log-path=/var/log/nginx/access.log\
--error-log-path=/var/log/nginx/error.log&&\
make&&makeinstall

CMD["nginx","-g","daemonoff"]
[root@localhostnginx]#podmanbuild-tnginx.

//修改镜像名
[root@localhost~]#podmantagdocker.io/library/nginx:latestdocker.io/1314444/test:latest

//登录并上传镜像
[root@localhost~]#podmanlogindocker.io//需要告诉其要登录到docker仓库
[root@localhost~]#podmanlogindocker.io
Username:1314444#账户
Password:********#密码
LoginSucceeded!

[root@localhostnginx]#podmanpushdocker.io/1314444/test:latest//上传镜像
Gettingimagesourcesignatures
Copyingblob38c40d6c2c85done
Copyingblobfee76a531659done
Copyingblobc2adabaecedbdone
Copyingconfig7f3589c0b8done
Writingmanifesttoimagedestination
Copyingconfig7f3589c0b8done
Writingmanifesttoimagedestination
Storingsignatures


//请注意,我们将四层推送到我们的注册表,现在可供其他人共享。快速浏览一下:
[root@localhost~]#podmaninspect1314444/test:nginx
//输出:
[
{
"Id":"7f3589c0b8849a9e1ff52ceb0fcea2390e2731db9d1a7358c2f5fad216a48263",
"Digest":"sha256:7822b5ba4c2eaabdd0ff3812277cfafa8a25527d1e234be028ed381a43ad5498",
"RepoTags":[
"docker.io/1314444/test:nginx",
......

总而言之,Podman 使查找、运行、构建和共享容器变得容易。

配置别名

如果习惯了使用 Docker 命令,可以直接给 Podman 配置一个别名来实现无缝转移。你只需要在 .bashrc 下加入以下行内容即可:

[root@localhost~]#echo"aliasdocker=podman">>.bashrc
source.bashrc
[root@localhost~]#alias
aliascp='cp-i'
aliasdocker='podman'
.......

用户操作

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

cgroup V2Linux内核功能允许用户限制普通用户容器可以使用的资源,如果使用cgroupV2启用了运行Podman的Linux发行版,则可能需要更改默认的OCI运行时。某些较旧的版本runc不适用于cgroupV2,必须切换到备用OCI运行时crun。

[root@localhost~]#yum-yinstallcrun//centos8系统自带

[root@localhost~]#vi/usr/share/containers/containers.conf
446#DefaultOCIruntime
447#
448runtime="crun"//取消注释并将runc改为crun

[root@localhost~]#podmanrun-d--nameweb-p80:80docker.io/library/nginx
c8664d2e43c872e1e5219f82d41f63048ed3a5ed4fb6259c225a14d6c243677f

[root@localhost~]#podmaninspectweb|grepcrun
"OCIRuntime":"crun",
"crun",

安装slirp4netns和fuse-overlayfs

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

[root@localhost~]#yum-yinstallslirp4netns

[root@localhost~]#yum-yinstallfuse-overlayfs
[root@localhost~]#vi/etc/containers/storage.conf
77mount_program="/usr/bin/fuse-overlayfs"//取消注释

/ etc / subuid和/ etc / subgid配置

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

[root@localhost~]#yum-yinstallshadow-utils

可以在/ etc / subuid和/ etc / subgid查看,每个用户的值必须唯一且没有任何重叠。

[root@localhost~]#useraddzz
[root@localhost~]#cat/etc/subuid
zz:100000:65536
[root@localhost~]#cat/etc/subgid
zz:100000:65536

//启动非特权ping
[root@localhost~]#sysctl-w"net.ipv4.ping_group_range=0200000"//大于100000这个就表示tom可以操作podman
net.ipv4.ping_group_range=0200000

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

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

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

[root@localhost~]#usermod--add-subuids200000-201000--add-subgids200000-201000hh
grephh/etc/subuid/etc/subgid
/etc/subuid:hh:200000:1001
/etc/subgid:hh:200000:1001

用户配置文件

三个主要的配置文件是container.confstorage.confr egistries.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文件

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

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

[root@localhost~]#vi/etc/containers/storage.conf
[storage]


>推荐下自己做的 Spring Cloud 的实战项目:
>
><

#DefaultStorageDriver,Mustbesetforproperoperation.
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并进行修改。

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

授权文件

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

[root@localhost~]#podmanlogin
Username:1314444
Password:
LoginSucceeded!
[root@localhost~]#cat/run/user/0/containers/auth.json
{
"auths":{
"registry.fedoraproject.org":{
"auth":"MTMxNDQ0NDpIMjAxNy0xOA=="
}
}
}

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

//root用户
[root@localhost~]#podmanimages
REPOSITORYTAGIMAGEIDCREATEDSIZE
docker.io/library/httpdlatestea28e1b82f3111daysago146MB

//普通用户
[root@localhost~]#su-zz
[zz@localhost~]$podmanimages
REPOSITORYTAGIMAGEIDCREATEDSIZE

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

使用卷

[root@localhost~]#su-zz
[zz@localhost~]$pwd
/home/zz
[zz@localhost~]$mkdir/home/zz/data

[zz@localhost~]$podmanrun-it-v"$(pwd)"/data:/datadocker.io/library/busybox/bin/sh
Tryingtopulldocker.io/library/busybox:latest...
Gettingimagesourcesignatures
Copyingblob3cb635b06aa2done
Copyingconfigffe9d497c3done
Writingmanifesttoimagedestination
Storingsignatures
/#ls
bindatadevetchomeprocrootrunsystmpusrvar
/#cddata/
/data#ls
/data#touch123
/data#ls-l
total0
-rw-r--r--1rootroot0Dec1300:17123

在主机上查看

[zz@localhost~]$lldata/
总用量0
-rw-r--r--1zzzz012月1300:17123

//写入文件
[zz@localhost~]$echo"hellworld">>123
[zz@localhost~]$cat123
hellworld

容器里查看

/data#cat123
hellworld

//我们可以发现在容器里面的文件的属主和属组都属于root,那么如何才能让其属于tom用户呢?下面告诉你答案
/data#ls-l
total4
-rw-rw-r--1rootroot12Dec1300:20123

//只要在运行容器的时候加上一个--userns=keep-id即可。
[zz@localhost~]$podmanrun-it--nametest-v"$(pwd)"/data:/data--userns=keep-iddocker.io/library/busybox/bin/sh
~$cddata/
/data$ls-l
total4
-rw-r--r--1zzzz11Dec1300:21123

使用普通用户映射容器端口时会报“ permission denied”的错误

[zz@localhost~]$podmanrun-d-p80:80httpd
Error:rootlessportcannotexposeprivilegedport80,youcanadd'net.ipv4.ip_unprivileged_port_start=80'to/etc/sysctl.conf(currently1024),orchoosealargerportnumber(>=1024):listentcp0.0.0.0:80:bind:permissiondenied

普通用户可以映射>= 1024的端口

[zz@localhost~]$podmanrun-d-p1024:80httpd
58613a6bdc70d4d4f9f624583f795a62a610596d166f0873bdff8fb26aa15092
[zz@localhost~]$ss-anlt
StateRecv-QSend-QLocalAddress:PortPeerAddress:PortProcess
LISTEN01280.0.0.0:220.0.0.0:*
LISTEN0128*:1024*:*
LISTEN0128[::]:22[::]:*

配置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

[zz@localhost~]$podmanrun-d-p80:80httpd
1215455a0c300d78e7bf6afaefc9873f818c6b0f26affeee4e2bc17954e72d8e
[zz@localhost~]$ss-anlt
StateRecv-QSend-QLocalAddress:PortPeerAddress:PortProcess
LISTEN01280.0.0.0:220.0.0.0:*
LISTEN0128*:1024*:*
LISTEN0128*:80*:*
LISTEN0128[::]:22[::]:*


延伸 · 阅读

精彩推荐