gpt4 book ai didi

Podman开机自启容器实现过程及与Docker对比

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Podman开机自启容器实现过程及与Docker对比由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

1.podman介绍

podman之前是CRI-O项目的一部分,后被分离成独立的项目libpod,libpod是一个创建容器pod的工具和库,podman是个无守护程序容器引擎,以root用户或无根模式运行,简而言之podman提供了一个docker-CLI的命令行,管理着容器 。

2.与docker相比的优势

docker劣势一:

docker大家都知道,其守护程序在多个核心上占用差不多高达100%cpu资源,采用C/S模型 。

podman优势一:

podman不需要守护进程,不需要root权限组,而且利用着用户命名空间(namespace)模拟容器中的root运行,采用fork/exec模型.

fork/exec模型相比C/S模型优势:

  • 系统管理员知道某个容器由谁启动
  • 利用cgroup对podman做限制,对应着创建的容器也会受到限制
  • systemd单元文件的生成,可以管理着任务的启动与关闭
  • socket激活,将socker从systemd发送给podman容器使用

3.兼容性

docker的功能大部分podman都是兼容的,也可以使用别名(alias)来写成docker的命令 。

4.后台服务单元文件的优先级

/usr/lib/systemd/user:优先级最低,会被优先级高的同名 unit 覆盖 ~/.local/share/systemd/user 。

/etc/systemd/user:全局共享的用户级 unit[s] 。

~/.config/systemd/user:优先级最高 。

5.podman基本操作

安装

?
1
2
3
#默认centos源
[root@slave02 ~] # yum -y  module install container-tools   #容器工具基于模块
[root@slave02 ~] # yum  -y install podman-docker            #安装docker兼容包(可选)

版本

?
1
2
[root@slave02 ~] # podman -v
podman version 3.3 . 0 - dev

仓库

官方仓库:registry.access.redhat.com 。

第三方仓库:docker.io 。

私有仓库:registry.lab.example.com 。

命令帮助

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@slave02 ~] # podman help|head -15
Manage pods, containers and images
Usage:
   podman [options] [command]
Available Commands:
   attach      Attach to a running container
   auto - update Auto update containers according to their auto - update policy
   build       Build an image using instructions from Containerfiles
   commit      Create new image based on the changed container  #基于修改的容器创建新的容器
   container   Manage containers
   cp          Copy files / folders between a container and the local filesystem
   create      Create but do not start a container
   diff        Display the changes to the object 's file system
   events      Show podman events
....

镜像加速器

修改配置文件:/etc/containers/registries.conf 即可 。

注意:不能带有httpds//:url格式 。

?
1
2
3
4
5
6
[root@slave02 ~] # cp /etc/containers/registries.conf  /backup/registries.conf.back  #备份一下         
[root@slave02 ~] # vim  /etc/containers/registries.conf
unqualified - search - registries = [ "docker.io" ]           #非限定搜索登记处
[[registry]]
prefix = "docker.io"
location = "x"             #x是阿里加速镜像地址

拉取镜像

?
1
[root@slave02 ~]# podman pull nginx

6.运行一个web容器

后台启动一个web容器,并访问容器内容

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#准备html页面内容
[root@ 192 ~] # cat /opt/webhtml/index.html
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambition
#运行一个守护web容器进程,将/opt/webhtml目录内容映射到容器的/usr/share/nginx/html存放网页的位置
[root@ 192 ~] # podman run -d --name web -p 8888:80 -v /opt/webhtml:/usr/share/nginx/html nginx
3528e6d5148bcf980f0df5708a82419d3485a33d1d16d722db3e880cc103cd2c
[root@podman ~] # curl 192.168.136.129:8888
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambition
#容器的ip
[root@podman ~] # podman inspect web|grep IPAddress
"IPAddress" : "10.88.0.6" ,
"IPAddress" : "10.88.0.6" ,
#宿主机的ip
[root@podman ~] # ip r
192.168 . 136.0 / 24 dev ens33 proto kernel scope link src 192.168 . 136.129 metric 100
#由于进行了端口绑定,所以直接 curl 192.168.136.129:8888即可访问

进入后台web容器,查看服务状态 。

?
1
2
3
[root@podman ~] # podman exec -it  web bash
root@ 3528e6d5148b : / # service nginx status
[ ok ] nginx is running.                             #运行中

修改容器业务内容 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
#修改宿主机/opt/webhtml/index.html即可
[root@podman ~] # cat /opt/webhtml/index.html
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambitionRHCAS
RHCE RHCA
#进行访问
[root@podman ~] # curl 192.168.136.129:8888
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambitionRHCAS
RHCE RHCA
#进入容器查看内容是否修改
[root@podman ~] # podman exec -it web bash
root@ 3528e6d5148b : / # cat /usr/share/nginx/html/index.html
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambitionRHCAS
RHCE RHCA

暂停与删除容器

?
1
2
3
4
5
6
7
8
9
10
11
12
#暂停
[root@podman ~] # podman stop web
web
[root@podman ~] # podman ps -a
CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS                     PORTS                 NAMES
3528e6d5148b  docker.io / library / nginx:latest  nginx - g daemon o...  25 minutes ago  Exited ( 0 ) 16 seconds ago  0.0 . 0.0 : 8888 - > 80 / tcp  web
#删除
[root@podman ~] # podman rm web
3528e6d5148bcf980f0df5708a82419d3485a33d1d16d722db3e880cc103cd2c
#或强制删除运行中的容器
[root@podman ~] # podman rm  -f web
3528e6d5148bcf980f0df5708a82419d3485a33d1d16d722db3e880cc103cd2c

7.web容器设置开机自启

后台运行一个web容器

?
1
2
[root@podman ~]# podman run --name web -d -p 8080:80 -v /opt/webhtml:/usr/shar/nginx/html nginx
910db3ab6bd1ef18e5fd0afe1844912f0b89334b7b8ab758353a948a1b55282a

基于web容器,在优先级一般的/etc/systemd/system内 。

创建.service单元文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@ 192 ~] # cd /etc/systemd/system/
[root@podman user] # podman generate systemd --
- - container - prefix  (Systemd unit name prefix for containers)
- - files             {生成.service文件,而不是打印到标准输出}
- - format            ( Print the created units in specified format (json)) #以指定的格式打印单元文件
- - name              (Use container / pod names instead of IDs)  #创建新容器,而不是使用现有的容器
- - new               (Create a new container instead of starting an existing one) #(跳过标头生成)
- - no - header         (Skip header generation)
- - pod - prefix        (Systemd unit name prefix for pods)
- - restart - policy    (Systemd restart - policy)
- - separator         (Systemd unit name separator between name / id and prefix)
- - time              (Stop timeout override)
[root@ 192 system] # podman generate systemd --name web --files --new
/ etc / systemd / system / container - web.service

查看生成的单元文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@ 192 system] # cat container-web.service
# container-web.service
# autogenerated by Podman 3.3.0-dev                                 #podman 3.3.0-dev自动生成
# Tue Aug 17 13:03:13 CST 2021                                      #8月17日星期二13:03:13 CST 2021                                                           
[Unit]       #单元
Description = Podman container - web.service              #描述
Documentation = man:podman - generate - systemd( 1 )          #帮助以及生成的系统
Wants = network - online.target                           #网络
After = network - online.target
RequiresMountsFor = % t / containers                         #前面不重要直接跳过
[Service]
Environment = PODMAN_SYSTEMD_UNIT = % n                  
Restart = on - failure                  #故障时重新启动
TimeoutStopSec = 70                   #超时时间   
ExecStart = / usr / bin / podman run - - sdnotify = conmon - - cgroups = no - conmon - - rm - - replace - - name web - d - p 8080 : 80 - v / opt / webhtml: / usr / shar / nginx / html nginx   #执行开始为/usr/bin/podman  运行刚才创建的容器
Type = notify
NotifyAccess = all
[Install]
WantedBy = multi - user.target default.target

删除刚才的容器

?
1
2
3
4
[root@podman ~] # podman rm web
910db3ab6bd1ef18e5fd0afe1844912f0b89334b7b8ab758353a948a1b55282a
[root@podman ~] # podman ps -a
CONTAINER ID  IMAGE       COMMAND     CREATED     STATUS      PORTS       NAMES

设置开机自启

?
1
2
3
4
5
6
7
[root@ 192 ~] # systemctl daemon-reload
[root@ 192 ~] # systemctl enable --now container-web.service
Created symlink / etc / systemd / system / multi - user.target.wants / container - web.service → / etc / systemd / system / container - web.service.
Created symlink / etc / systemd / system / default.target.wants / container - web.service → / etc / systemd / system / container - web.service.
[root@ 192 user] # podman ps -a
CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS             PORTS                   NAMES
b0c7709cb00e  docker.io / library / nginx:latest  nginx - g daemon o...  15 seconds ago  Up 16 seconds ago  0.0 . 0.0 : 8080 - > 80 / tcp    web

无根root模式设置容器和上面这种方式大同小异 。

使用systemctl命令带上 --user 即可 。

?
1
2
#需要运行loginctl enable-linger命令,使用户服务在服务器启动时自动启动即可
[containers@serverb ~]$ loginctl enable - linger

以上就是Podman开机自启容器实现过程的详细内容,更多关于Podman开机自启容器的资料请关注我其它相关文章! 。

原文链接:https://blog.csdn.net/qq_47945825/article/details/119754888 。

最后此篇关于Podman开机自启容器实现过程及与Docker对比的文章就讲到这里了,如果你想了解更多关于Podman开机自启容器实现过程及与Docker对比的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com