image-1660484693789

什么是Podman?

简单地说:别名 docker=podman

Podman是一个开源项目,可以在大多数Linux平台上使用,并驻留在GitHub上。Podman 是一个无守护程序容器引擎,用于在 Linux 系统上开发、管理和运行开放容器计划 (OCI) 容器和容器映像。Podman 提供了一个与 Docker 兼容的命令行前端,可以简单地将 Docker cli 别名为 docker=podman。Podman 还提供了一个套接字激活的 REST API 服务,以允许远程应用程序启动按需容器。这个 REST API 还支持 Docker API,允许 docker-py 和 docker-compose 的用户作为服务与 Podman 进行交互。

Podman 控制下的容器可以由 root 用户或非特权用户运行。Podman 使用 libpod 库管理整个容器生态系统,其中包括 Pod、容器、容器映像和容器卷。Podman 专门研究所有帮助您维护和修改 OCI 容器映像(如拉取和标记)的命令和函数。它允许您在生产环境中创建、运行和维护从这些映像创建的容器。

Podman服务仅在Linux平台上运行,但是podman远程REST API客户端存在于Mac和Windows平台上,并且可以通过ssh与Linux机器或VM上运行的Podman服务进行通信。Mac 客户端

Podman工作机制

Podman 原来是 CRI-O 项目的一部分,后来被分离成一个单独的项目叫 libpod。Podman 的使用体验和 Docker 类似,不同的是 Podman 没有 daemon。以前使用 Docker CLI 的时候,Docker CLI 会通过 gRPC API 去跟 Docker Engine 说「我要启动一个容器」,然后 Docker Engine 才会通过 OCI Container runtime(默认是 runc)来启动一个容器。这就意味着容器的进程不可能是 Docker CLI 的子进程,而是 Docker Engine 的子进程。

Podman 比较简单粗暴,它不使用 Daemon,而是直接通过 OCI runtime(默认也是 runc)来启动容器,所以容器的进程是 podman 的子进程。这比较像 Linux 的 fork/exec 模型,而 Docker 采用的是 C/S(客户端/服务器)模型。与 C/S 模型相比,fork/exec 模型有很多优势。

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。

image-1660484720216

图中所体现的事情是,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,仍然可以使用http://docker.io作为镜像仓库,这也是兼容性最关键的部分。

安装Podman

[root@localhost ~]# yum -y install podman        yum直接下载podman
[root@localhost ~]# rpm -qa | grep seccomp
libseccomp-2.5.2-1.el8.x86_64
[root@localhost ~]# podman version
Version:      3.3.1
API Version:  3.3.1
Go Version:   go1.16.7
Built:        Wed Nov 10 05:23:56 2021
OS/Arch:      linux/amd64

配置加速器,以及镜像仓库

[root@localhost ~]# vim /etc/containers/registries.conf   
复制下面的删除其余的镜像仓库,就留docker.io的镜像仓库
unqualified-search-registries = ["docker.io"]  
#unqualified-search-registries = ["registry.fedoraproject.org", "registry.access.redhat.com", "registry.centos.org", "docker.io"]

配置加速器
[[registry]]
prefix = "docker.io"
location = "docker.mirrors.ustc.edu.cn"   配置清华大学加速器

podman帮助文档

[root@localhost ~]# podman --help 
[root@localhost ~]#podman <subcommand> --help

Podman常用命令

如果你使用过docker的CLI命令行,podman几乎没有任何的区别,只需要把docker换成podman即可,参数顺序、含义都是一样的。详细命令参数请点击这里查看docker命令基础

镜像

podman search             #检索镜像  
docke pull                #获取镜像  
podman images             #列出镜像  
podman image Is           #列出镜像  
podman rmi                #删除镜像  
podman image rm           #删除镜像  
podman save               #导出镜像  
podman load               #导入镜像  
podmanfile                #定制镜像(三个)  
    podman build              #构建镜像  
    podman run              #运行镜像  
    podmanfile              #常用指令(四个)  
        COPY                    #复制文件  
        ADD                     #高级复制  
        CMD                     #容器启动命令  
        ENV                     #环境变量  
        EXPOSE                  #暴露端口  

命令用法
[root@localhost ~]# podman search  httpd
INDEX       NAME                                             DESCRIPTION                                      STARS       OFFICIAL    AUTOMATED
docker.io   docker.io/library/httpd                          The Apache HTTP Server Project                   4116        [OK]        
......

[root@localhost ~]# podman pull httpd
Trying to pull docker.io/library/httpd:latest...
.......
dabbfbe0c57b6e5cd4bc089818d3f664acfad496dc741c9a501e72d15e803b34

[root@localhost ~]# podman images
REPOSITORY                 TAG         IMAGE ID      CREATED        SIZE
docker.io/library/httpd    latest      dabbfbe0c57b  7 months ago   148 MB

[root@localhost ~]# podman rmi httpd
Untagged: docker.io/library/httpd:latest
Deleted: dabbfbe0c57b6e5cd4bc089818d3f664acfad496dc741c9a501e72d15e803b34


[root@localhost tmp]# podman save  > /tmp/nginx.tar  docker.io/library/nginx


[root@localhost tmp]# podman load < nginx.tar 
........
Copying blob bdc7a32279cc [--------------------------------------] 0.0b / 0.0b
Copying config b692a91e4e done  
Writing manifest to image destination
Storing signatures
Loaded image(s): docker.io/library/nginx:latest

容器

podman run         #创建并启动容器  
podman start       #启动容器  
podman ps          #查看容器  
podman stop        #终止容器  
podman restart     #重启容器  
podman attach      #进入容器  
podman exec        #进入容器  
podman export      #导出容器  
podman import      #导入容器快照  
podman rm          #删除容器  
podman logs        #查看日志  
podman  diff       # 检查容器或映像文件系统上的更改
 
命令用法
[root@localhost ~]# podman run --name busybos -it busybox /bin/sh
/ # 

[root@localhost ~]# podman start busybos 
busybos

[root@localhost ~]# podman ps
CONTAINER ID  IMAGE                             COMMAND     CREATED         STATUS            PORTS       NAMES
1afdb4d24f37  docker.io/library/busybox:latest  /bin/sh     50 seconds ago  Up 9 seconds ago              busybos

[root@localhost ~]# podman stop busybos 
busybos

[root@localhost ~]# podman restart busybos 
1afdb4d24f374d4940a6c0fba12aa083a34be01ad3082ec8b61059c878658687

[root@localhost ~]# podman attach busybos 
/ # 

[root@localhost ~]# podman exec -it  busybos /bin/sh 
/ # 

[root@localhost ~]# podman export > /root/busybos.tar  busybos 
[root@localhost ~]# ls
busybos.tar

[root@localhost ~]# podman rm -f busybos 
0a54897f3e3fc3ff33415cf5b36470cf249448fefbb7a6ccc941ced06343b065

[root@localhost ~]# podman logs busybos 
.....
/ # mkdir /data
/ # rm -rf /data
/ # exit
......
  
 [root@localhost ~]# podman diff docker.io/library/httpd:latest 
C /usr
C /usr/local
C /usr/local/bin
A /usr/local/bin/httpd-foreground

镜像签名

准备环境

安装python3

yum install python3 -y

配置镜像签名

先创建一个GPG密钥对。根据提示提供用户名、邮箱和密码,并接受缺省选项即可。

[root@localhost ~]# gpg --full-gen-key
gpg (GnuPG) 2.2.20; Copyright (C) 2020 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

gpg: directory '/root/.gnupg' created
gpg: keybox '/root/.gnupg/pubring.kbx' created
Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
  (14) Existing key from card
Your selection? 
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 
Requested keysize is 2048 bits
Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0) 
Key does not expire at all
Is this correct? (y/N) y

GnuPG needs to construct a user ID to identify your key.

Real name: durui
Email address: 2279236475@qq.com
Comment: test
You selected this USER-ID:
    "durui (test) <2279236475@qq.com>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key DC6112446D673CD2 marked as ultimately trusted
gpg: directory '/root/.gnupg/openpgp-revocs.d' created
gpg: revocation certificate stored as '/root/.gnupg/openpgp-revocs.d/24E6A2367BAC522D3D855414DC6112446D673CD2.rev'
public and secret key created and signed.

pub   rsa2048 2022-08-15 [SC]
      24E6A2367BAC522D3D855414DC6112446D673CD2
uid                      durui (test) <2279236475@qq.com>
sub   rsa2048 2022-08-15 [E]


查看创建的GPG秘钥

[root@localhost ~]# gpg --list-keys 2279236475@qq.com
gpg: checking the trustdb
gpg: marginals needed: 3  completes needed: 1  trust model: pgp
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   rsa2048 2022-08-15 [SC]
      24E6A2367BAC522D3D855414DC6112446D673CD2
uid           [ultimate] durui (test) <2279236475@qq.com>
sub   rsa2048 2022-08-15 [E]

运行一个容器注册表。

[root@localhost ~]# podman run -d -p 5000:5000 docker.io/registry
Trying to pull docker.io/library/registry:latest...
Getting image source signatures
Copying blob e2ead8259a04 done  
Copying blob 3790aef225b9 done  
Copying blob 79e9f2f55bf5 done  
Copying blob 5b27040df4a2 done  
Copying blob 0d96da54f60b done  
Copying config b8604a3fe8 done  
Writing manifest to image destination
Storing signatures
719385b956f74d4f515120836ba535de1337780591203ba83c264cff4f46825c

注册表对映像签名一无所知,它只是为容器映像提供远程存储。这意味着,如果我们要对图像进行签名,则必须注意如何分发签名。

让我们为签名实验选择一个标准映像:alpine

[root@localhost ~]# podman pull docker://docker.io/alpine:latest
Trying to pull docker.io/library/alpine:latest...
Getting image source signatures
Copying blob 59bf1c3509f3 done  
Copying config c059bfaa84 done  
Writing manifest to image destination
Storing signatures
c059bfaa849c4d8e4aecaeb3a10c2d9b3d85f5165c66ad3a4d937758128c4d18

[root@localhost ~]# podman images alpine
REPOSITORY                TAG         IMAGE ID      CREATED       SIZE
docker.io/library/alpine  latest      c059bfaa849c  8 months ago  5.87 MB

现在,我们可以重新标记映像以将其指向本地注册表:

[root@localhost ~]# podman tag alpine localhost:5000/alpine
[root@localhost ~]# podman images alpine
REPOSITORY                TAG         IMAGE ID      CREATED       SIZE
docker.io/library/alpine  latest      c059bfaa849c  8 months ago  5.87 MB
localhost:5000/alpine     latest      c059bfaa849c  8 months ago  5.87 MB

Podman现在能够推送图像并在一个命令中签名。但是要让它工作,我们必须修改系统范围的注册表配置,网址为:/etc/containers/registries.d/default.yaml

default-docker:
  sigstore: http://localhost:8000 # Added by us
  sigstore-staging: file:///var/lib/containers/sigstore

签名alpine镜像并推送到本地Docker Registry,然后删除本地的alpine镜像

[root@localhost ~]# GNUPGHOME=$HOME/.gnupg
[root@localhost ~]# podman push --tls-verify=false --sign-by 2279236475@qq.com localhost:5000/alpine:latest 
Getting image source signatures
Copying blob 8d3ac3489996 done  
Copying config c059bfaa84 done  
Writing manifest to image destination
Signing manifest
Storing signatures

查看系统签名存储

[root@localhost ~]# ls /var/lib/containers/sigstore
'alpine@sha256=964248be4bb8e3052c8b411271126f70c5c5015df31e014bfc41fad50edf78d8'

我们编辑的版本中的默认签名存储引用了侦听 的 Web 服务器。对于我们的实验,我们只需在本地暂存签名存储中启动一个新服务器:/etc/containers/registries.d/default.yaml``http://localhost:8000

[root@localhost ~]# bash -c 'cd /var/lib/containers/sigstore && python3 -m http.server'
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

删除本地图像以进行验证测试:

[root@localhost ~]# podman rmi docker.io/alpine localhost:5000/alpine:latest 
Untagged: docker.io/library/alpine:latest
Untagged: localhost:5000/alpine:latest
Deleted: c059bfaa849c4d8e4aecaeb3a10c2d9b3d85f5165c66ad3a4d937758128c4d18

写一个策略来强制签名必须有效。这可以通过在 中添加新规则来完成。从下面的示例中,将条目复制到 ./etc/containers/policy.json``"docker"``"transports"``policy.json

{
  "default": [{ "type": "insecureAcceptAnything" }],
  "transports": {
    "docker": {
      "localhost:5000": [
        {
          "type": "signedBy",
          "keyType": "GPGKeys",
          "keyPath": "/tmp/key.gpg"
        }
      ]
    }
  }
}

尚不存在,因此我们必须将 GPG 密钥放在那里:keyPath

[root@localhost ~]# gpg --output /tmp/key.gpg --armor --export 2279236475@qq.com

现在拉动图像:

[root@localhost ~]# podman pull --tls-verify=false localhost:5000/alpine
Trying to pull localhost:5000/alpine:latest...
Getting image source signatures
Checking if image destination supports signatures
Copying blob 3c4e9198e8c1 done  
Copying config c059bfaa84 done  
Writing manifest to image destination
Storing signatures
c059bfaa849c4d8e4aecaeb3a10c2d9b3d85f5165c66ad3a4d937758128c4d18

然后,我们可以在Web服务器的日志中看到签名已被访问

10.88.0.1 - - [15/Aug/2022:12:46:11 +0000] "GET /v2/alpine/blobs/sha256:3c4e9198e8c15669838fa75b9fde03039cc4a256d6868d214d966bd8f27b093d HTTP/1.1" 200 2896369 "" "containers/5.15.2 (github.com/containers/image)"

作为对应的例子,如果我们在以下位置指定了错误的键:/tmp/key.gpg

gpg --output /tmp/key.gpg --armor --export mail@saschagrunert.de
File '/tmp/key.gpg' exists. Overwrite? (y/N) y

然后拉动就不可能了:

sudo podman pull --tls-verify=false localhost:5000/alpine
Trying to pull localhost:5000/alpine...
Error: error pulling image "localhost:5000/alpine": unable to pull localhost:5000/alpine: unable to pull image: Source image rejected: Invalid GPG signature: …

因此,一般来说,在使用 Podman 和 GPG 对容器映像进行签名时,需要考虑四个主要事项:

  1. 我们需要在签名计算机上有一个有效的私有GPG密钥,并在每个系统上需要相应的公钥来拉取映像
  2. Web服务器必须在可以访问签名存储的地方运行
  3. 必须在任何文件中配置 Web 服务器/etc/containers/registries.d/*.yaml
  4. 每个映像拉取系统都必须配置为包含强制策略配置,通过以下方式policy.conf

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

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

管理员操作

cgroup V2 支持

cgroup V2 Linux 内核功能允许用户限制无根容器可以使用的资源量。如果您正在运行 Podman 的 Linux 发行版启用了 cgroup V2,则可能需要更改默认的 OCI 运行时。某些较旧版本的 不适用于 cgroup V2,您可能需要切换到备用 OCI 运行时 。`runc``crun

[root@localhost ~]# yum -y install crun     //centos8系统自带

[root@localhost ~]# vim /usr/share/containers/containers.conf
433 runtime = "crun"       取消注释,把下面的注释上
434 #runtime = "runc"   

 
[root@localhost ~]# podman run --name web -d docker.io/library/httpd
a3ad078c96a97b483494e3642a13cb523ff6222cbcf78173e4e9c9338ed3cb09
[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
 77 mount_program = "/usr/bin/fuse-overlayfs"   取消注释
 

/ etc / subuid和/ etc / subgid配置

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

[root@localhost ~]# yum -y install shadow-utils

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

[root@localhost ~]# useradd durui
[root@localhost ~]# cat /etc/subuid
durui:100000:65536
[root@localhost ~]# cat /etc/subgid
durui:100000:65536

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

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

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

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

[root@localhost ~]#useradd du
[root@localhost ~]# usermod --add-subuids 100000-165535 --add-subgids 100000-165535 du
[root@localhost ~]# grep du /etc/subuid /etc/subgid 
/etc/subuid:du:100000:65536
/etc/subgid:du:100000:65536

用户配置文件

三个主要的配置文件是container.confstorage.confregistries.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 ~]#  vim /etc/containers/storage.conf
7 # Default Storage Driver, Must be set for proper operation.
8 driver = "overlay"    此处改为overlay
.....
 77 mount_program = "/usr/bin/fuse-overlayfs"   取消注释
 
 [root@localhost ~]# sysctl user.max_user_namespaces=15000
user.max_user_namespaces = 15000

授权文件

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

[root@localhost ~]# podman login
Username: 
Password: 
Login Succeeded

[root@localhost ~]# cat /run/user/0/containers/auth.json
{
        "auths": {
                "registry.fedoraproject.org": {
                        "auth": "ZHVneHE6Z3hxMTMxNDkyMA=="
                }
        }

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

root用户
[root@localhost ~]# podman images     
REPOSITORY                    TAG         IMAGE ID      CREATED       SIZE
docker.io/library/busybox     latest      beae173ccac6  7 months ago  1.46 MB
docker.io/library/nginx       latest      605c77e624dd  7 months ago  146 MB
docker.io/library/httpd       latest      dabbfbe0c57b  7 months ago  148 MB

普通用户
[durui@localhost ~]$ podman images
REPOSITORY  TAG         IMAGE ID    CREATED     SIZE

root用户也无法看到普通用户的镜像

[durui@localhost ~]$ podman images
REPOSITORY               TAG         IMAGE ID      CREATED       SIZE
docker.io/library/mysql  latest      3218b38490ce  7 months ago  521 MB


[root@localhost ~]# podman images
REPOSITORY                    TAG         IMAGE ID      CREATED       SIZE
docker.io/library/busybox     latest      beae173ccac6  7 months ago  1.46 MB
docker.io/library/nginx       latest      605c77e624dd  7 months ago  146 MB

参考docker用法

如果你使用过docker的CLI命令行,podman几乎没有任何的区别,只需要把docker换成podman即可,参数顺序、含义都是一样的。
docker基础用法
docker镜像管理
docker网络配置
docker存储卷