ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Podman

2021-03-10 20:34:22  阅读:1344  来源: 互联网

标签:httpd yqh podman Podman io docker root


Podman


什么是Podman?

Podman 是一个开源的容器运行时项目,可在大多数 Linux 平台上使用。Podman 提供与 Docker 非常相似的功能。正如前面提到的那样,它不需要在你的系统上运行任何守护进程,并且它也可以在没有 root 权限的情况下运行。
Podman 可以管理和运行任何符合 OCI(Open Container Initiative)规范的容器和容器镜像。Podman 提供了一个与 Docker 兼容的命令行前端来管理 Docker 镜像。

  1. Podman 官网地址:https://podman.io/
  2. Podman 项目地址:https://github.com/containers/libpod

Podman和docker的区别

1.docker需要在系统上运行一个守护进程,而podman不需要。

2.docker需要使用root用户来创建容器,而podman不需要。

3.因为docker有守护进程,所以docker启动的容器支持重启策略,但是podman不支持。

4.启动容器的方式不同:docker cli命令通过API跟Docker引擎交互告诉它我想创建一个container,然后docker引擎才会调用OCI containerd runc来启动一个container。这代表container的进程是Docker引擎的子进程,而不是Docker CLI的子进程。Podman是直接给OCI containnerd runc进行交互来创建container的,所以container进程直接是podman的子进程。


podman的使用

//安装podman
[root@yqh ~]# yum -y install podman

[root@yqh ~]# podman info
host:
  arch: amd64
  buildahVersion: 1.18.0
  cgroupManager: systemd
  cgroupVersion: v1
  conmon:
    package: conmon-2.0.22-3.module_el8.3.0+699+d61d9c41.x86_64
    path: /usr/bin/conmon
······

配置加速器

这里使用的是阿里云加速器,获取方法见 docker基础用法

//备份配置文件
[root@yqh ~]# cd /etc/containers/
[root@yqh containers]# ls
certs.d  oci  policy.json  registries.conf  registries.d  storage.conf
[root@yqh containers]# mv registries.conf registries.conf.tmpl

//新建一个空的registries.conf文件,并进行配置
[root@yqh containers]# vim registries.conf
unqualified-search-registries = ["docker.io"]

[[registry]]
prefix = ""
location= "******.mirror.aliyuncs.com"			#这里填写自己的加速器

podman基础命令应用

//使用pull命令拉网上的镜像,不加版本默认为最新
[root@yqh ~]# podman pull busybox
Completed short name "busybox" with unqualified-search registries (origin: /etc/containers/registries.conf)
Trying to pull docker.io/library/busybox:latest...
Getting image source signatures
Copying blob 8b3d7e226fab done  
Copying config a9d583973f done  
Writing manifest to image destination
Storing signatures
a9d583973f65a19b3bbd7a4312b4e2c27712c44c0ed8b94e9a38cc73e7565b75

//需要指定版本要使用:加指定版本
[root@yqh ~]# podman pull httpd:2.4-alpine
Completed short name "httpd" with unqualified-search registries (origin: /etc/containers/registries.conf)
Trying to pull docker.io/library/httpd:2.4-alpine...
Getting image source signatures
Copying blob d632c8441234 done  
Copying blob 0fcb24848396 done  
Copying blob f84cab65f19f done  
Copying blob 3e84dc11ea39 done  
Copying blob 87259b7246e5 done  
Copying config 40841bcea4 done  
Writing manifest to image destination
Storing signatures
40841bcea476b7411a163009cb256251aa3830efab8f07a689f52258f145ca48

//使用images查看本地的镜像
[root@yqh ~]# podman images
REPOSITORY                 TAG         IMAGE ID      CREATED       SIZE
docker.io/library/busybox  latest      a9d583973f65  11 hours ago  1.45 MB
docker.io/library/httpd    2.4-alpine  40841bcea476  13 days ago   57.8 MB

//使用search命令查找网上的镜像httpd
[root@yqh ~]# podman search httpd
INDEX      NAME                                             DESCRIPTION                                      STARS   OFFICIAL  AUTOMATED
docker.io  docker.io/library/httpd                          The Apache HTTP Server Project                   3399    [OK]      
docker.io  docker.io/centos/httpd-24-centos7                Platform for running Apache httpd 2.4 or bui...  36                
docker.io  docker.io/manageiq/httpd                         Container with httpd, built on CentOS for Ma...  0                 [OK]
docker.io  docker.io/clearlinux/httpd                       httpd HyperText Transfer Protocol (HTTP) ser...  1                 
docker.io  docker.io/centos/httpd                                                                            33                [OK]

//使用create在一个镜像中创建容器
[root@yqh ~]# podman create httpd:2.4-alpine 
7edbc6af5cdcea46a25ea42353cb689845653727bf40eefb12eaa498ef4e5f2b

//使用ps -a查看所有的容器
[root@yqh ~]# podman ps -a
CONTAINER ID  IMAGE                               COMMAND           CREATED         STATUS   PORTS   NAMES
7edbc6af5cdc  docker.io/library/httpd:2.4-alpine  httpd-foreground  48 seconds ago  Created          beautiful_edison

//使用start使容器启动
[root@yqh ~]# podman start 7edbc6af5cdc
7edbc6af5cdc

//使用restart重启容器
[root@yqh ~]# podman restart 7edbc6af5cdc
7edbc6af5cdcea46a25ea42353cb689845653727bf40eefb12eaa498ef4e5f2b

//使用stop停止容器
[root@yqh ~]# podman stop 7edbc6af5cdc
7edbc6af5cdcea46a25ea42353cb689845653727bf40eefb12eaa498ef4e5f2b
[root@yqh ~]# podman ps -a
CONTAINER ID  IMAGE                               COMMAND           CREATED        STATUS                    PORTS   NAMES
7edbc6af5cdc  docker.io/library/httpd:2.4-alpine  httpd-foreground  6 minutes ago  Exited (0) 4 seconds ago          beautiful_edison

//使用rm删除容器(删除时容器应处于停止状态,若容器还在运行则删除失败,可以使用rm -f强制删除)
[root@yqh ~]# podman rm 7edbc6af5cdc
7edbc6af5cdcea46a25ea42353cb689845653727bf40eefb12eaa498ef4e5f2b
[root@yqh ~]# podman ps -a
CONTAINER ID  IMAGE   COMMAND  CREATED  STATUS  PORTS   NAMES

//使用rmi删除镜像(删除镜像时应没有容器在运行,如果镜像里有容器在运行,仅只会把镜像的名称删除,镜像依旧存在且镜像里的容器还会继续运行)
[root@yqh ~]# podman rmi docker.io/library/httpd:2.4-alpine 
Untagged: docker.io/library/httpd:2.4-alpine
Deleted: 40841bcea476b7411a163009cb256251aa3830efab8f07a689f52258f145ca48
[root@yqh ~]# podman images
REPOSITORY                 TAG     IMAGE ID      CREATED       SIZE
docker.io/library/busybox  latest  a9d583973f65  11 hours ago  1.45 MB

//使用run可以自动进行创建和运行容器(加上-d使其在后台运行)
[root@yqh ~]# podman run httpd
Completed short name "httpd" with unqualified-search registries (origin: /etc/containers/registries.conf)
Trying to pull docker.io/library/httpd:latest...
Getting image source signatures
Copying blob 243acf75a504 done  
Copying blob 45b42c59be33 done  
Copying blob 8fc1ad93a9b1 done  
Copying blob 83ac8490fcc3 done  
Copying blob bdb2d204d86d done  
Copying config 464fdc577e done  
Writing manifest to image destination
Storing signatures
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.88.0.6. 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.6. Set the 'ServerName' directive globally to suppress this message
[Wed Mar 10 08:47:56.661193 2021] [mpm_event:notice] [pid 1:tid 140400069350528] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Wed Mar 10 08:47:56.661319 2021] [core:notice] [pid 1:tid 140400069350528] AH00094: Command line: 'httpd -D FOREGROUND'
(动不了,另外开一个终端)
[root@yqh ~]# podman ps -a
CONTAINER ID  IMAGE                           COMMAND           CREATED         STATUS             PORTS   NAMES
1307c283b810  docker.io/library/httpd:latest  httpd-foreground  23 seconds ago  Up 23 seconds ago          bold_bouman

//使用logs查看容器的日志
[root@yqh ~]# podman logs 1307c283b810
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.88.0.6. 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.6. Set the 'ServerName' directive globally to suppress this message
[Wed Mar 10 08:47:56.661193 2021] [mpm_event:notice] [pid 1:tid 140400069350528] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Wed Mar 10 08:47:56.661319 2021] [core:notice] [pid 1:tid 140400069350528] AH00094: Command line: 'httpd -D FOREGROUND'

//使用inspect查看容器的各种信息,比如IP
[root@yqh ~]# podman inspect 1307c283b810
        "NetworkSettings": {
            "EndpointID": "",
            "Gateway": "10.88.0.1",
            "IPAddress": "10.88.0.6",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "MacAddress": "2e:8c:c7:9b:ac:cb",
            "Bridge": "",
            "SandboxID": "",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/netns/cni-ff472609-0748-aba8-b730-e68eeb4d9f3b"
        },

//使用attach进入到容器的内部,但不能操作且退出时容器也会停止,不推荐使用。
[root@yqh ~]# podman attach 1307c283b810

(另开一个终端访问它)
[root@yqh ~]# curl 10.88.0.6
<html><body><h1>It works!</h1></body></html>

(内部出现访问信息)
10.88.0.1 - - [10/Mar/2021:08:52:12 +0000] "GET / HTTP/1.1" 200 45

(Ctrl+C终止容器运行)
^C[Wed Mar 10 08:53:20.064118 2021] [mpm_event:notice] [pid 1:tid 140400069350528] AH00491: caught SIGTERM, shutting down

//使用exec -it指定交互模式进入容器,比如/bin/bash或/bin/sh,由此可以实现操作且退出时容器不会停止
(需要先启动容器)
[root@yqh ~]# podman start 1307c283b810
1307c283b810
[root@yqh ~]# podman exec -it 1307c283b810 /bin/sh
# pwd
/usr/local/apache2

普通用户使用的配置

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

详情见 Podman官方文档

创建普通用户

[root@yqh ~]# useradd yqh

cgroup V2支持

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

用于通过在系统级或在任一改变用于在containers.conf文件“默认OCI运行时”的值的所有命令用户级别runtime = "runc"runtime = "crun"

//安装crun
[root@yqh ~]# yum -y install crun

//取消注释并修改成crun
[root@yqh ~]# vim /usr/share/containers/containers.conf
runtime = "crun"
          
//启动一个容器查看
[root@yqh ~]# podman run -d --rm --name web2 nginx
[root@yqh ~]# podman inspect web2|grep crun
        "OCIRuntime": "crun",
            "crun",

安装slirp4netns

提供用户模式网络,并且必须安装上才能使Podman在普通用户环境中运行

[root@yqh ~]# yum -y install slirp4netns

安装fuse-overlayfs

在普通用户环境下,建议使用fuse-overlayfs文件系统而不是VFS文件系统

[root@yqh ~]# yum -y install fuse-overlayfs

//确保配置文件如下
[root@yqh ~]# vim /etc/containers/storage.conf
[storage]
driver = "overlay"
······
mount_program = "/usr/bin/fuse-overlayfs"		#取消注释

启用用户名称空间(RHEL7)

文件中指定了系统上允许的用户名称空间的数量/proc/sys/user/max_user_namespaces。在大多数Linux平台上,默认情况下是预设的,因此无需进行任何调整。但是,在RHEL7上,具有root权限的用户可能需要使用以下命令将其设置为合理的值: sysctl user.max_user_namespaces=15000

配置/etc/subuid和/etc/subgid

安装shadownewuid

[root@yqh ~]# yum -y install shadow

[root@yqh ~]# yum -y install newuid

使用允许每个用户创建类似于以下内容的容器的字段来更新/etc/subuid和/etc /subgid的字段。请注意,每个用户的值必须唯一且没有任何重叠。如果存在重叠,则用户有可能使用其他人的命名空间,并且他们可能破坏该命名空间。

[root@yqh ~]# cat /etc/subuid
yqh:100000:65536

该文件的格式为 USERNAME:UID:RANGE

  • 在/etc/passwd或getpwent中列出的用户名。
  • 为用户分配的初始uid。
  • 为用户分配的UID范围的大小。

这意味着在/etc/passwd文件中为用户yqh分配了UIDS 100000-165535及其标准UID。注意:网络安装当前不支持此功能。这些文件必须在本地可用于主机。无法使用LDAP或Active Directory进行配置。

如果更新/etc/subuid或/etc/subgid文件,则需要停止该用户拥有的所有正在运行的容器,并终止该用户在系统上运行的暂停过程。这可以通过使用podman system migrate命令自动完成,该命令将为用户停止所有容器并终止暂停过程。

启用非特权 ping

在非特权容器中运行的用户可能无法使用该ping容器中的实用程序。

如果需要这样做,管理员必须验证用户的UID是否在/proc/sys/net/ipv4/ping_group_range文件范围内。

要更改其值,管理员可以使用类似于的呼叫sysctl -w "net.ipv4.ping_group_range=0 2000000"

为了使更改持久存在,管理员将需要添加一个文件.conf扩展名,/etc/sysctl.d其中包含net.ipv4.ping_group_range=0 $MAX_GID,该文件$MAX_GID是运行容器的用户的最高可分配GID。

[root@yqh ~]# echo 'net.ipv4.ip_unprivileged_port_start=80' >> /etc/sysctl.conf
[root@yqh ~]# sysctl -p
net.ipv4.ip_unprivileged_port_start = 80

使用普通用户启动nginx容器测试

//切换到普通用户
[root@yqh ~]# su - yqh

//启动一个nginx容器
[yqh@yqh ~]$ podman run -d --name web -p 80:80 nginx
ae9b8ef916baf09a92801b1f72e14df44a9253b6bb122348174058dabb3b15d1

[yqh@yqh ~]$ podman ps -a
CONTAINER ID  IMAGE                           COMMAND               CREATED        STATUS            PORTS               NAMES
ae9b8ef916ba  docker.io/library/nginx:latest  nginx -g daemon o...  5 minutes ago  Up 4 minutes ago  0.0.0.0:80->80/tcp  web

[yqh@yqh ~]$ ss -antl
State     Recv-Q     Send-Q         Local Address:Port         Peer Address:Port    
LISTEN    0          128                  0.0.0.0:22                0.0.0.0:*       
LISTEN    0          128                        *:80                      *:*       
LISTEN    0          128                     [::]:22                   [::]:* 

访问本机IP(192.168.100.3)

img

授权文件

podman loginpodman logout命令使用的默认授权文件位于中${XDG_RUNTIME_DIR}/containers/auth.json

//使用root用户登录官网
[root@yqh ~]# podman login
Username: baoziong
Password: 
Login Succeeded!

[root@yqh ~]# find / -name auth.json
/run/containers/0/auth.json

不用创建仓库推镜像至官网

[root@yqh ~]# podman images
REPOSITORY                 TAG     IMAGE ID      CREATED       SIZE
docker.io/library/busybox  latest  a9d583973f65  14 hours ago  1.45 MB
docker.io/library/nginx    latest  35c43ace9216  2 weeks ago   137 MB
docker.io/library/httpd    latest  464fdc577ef4  4 weeks ago   142 MB

//给busybox打上标签
[root@yqh ~]# podman tag docker.io/library/busybox:latest docker.io/baoziong/busybox:v0.1

//直接上传镜像
[root@yqh ~]# podman push docker.io/baoziong/busybox:v0.1
Getting image source signatures
Copying blob 2983725f2649 skipped: already exists  
Copying config a9d583973f done  
Writing manifest to image destination
Storing signatures

上传成功

img

img

标签:httpd,yqh,podman,Podman,io,docker,root
来源: https://www.cnblogs.com/yuqinghao/p/14514020.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有