0
点赞
收藏
分享

微信扫一扫

C++服务器框架开发4——日志系统logger/.cpp与.cc

老牛走世界 2023-06-07 阅读 70

podman 与 docker 对比

1.docker 存在的问题
进程问题(每起一个容器需要占用一个进程)
安全问题(docker进程是root起)

而使用 podman 不存在 docker 的以上问题
podman 是一个无守护进程的容器引擎,podman控制的容器可以使用root或非root用户运行

2.docker 与 podman 的区别
1. docker 需要在系统上运行一个守护进程,而podman不需要
2. 启动方式不一样……
3. docker 支持 --restart 策略,podman 不支持,但是可以通过systemd服务完成容器的自启动
4. docker 需要 root 启动,podman 不需要

安装 podman

rhel系统上安装podman,建议 rhel8.2 版本以上使用podman,使用 podman 1.9.3 版本,

$ rpm -qa | grep podman
podman-1.6.4-36.el7_9.x86_64
$ yum -y install podman

配置镜像加速

配置文件: /etc/containers/registries.conf

$ cat /etc/containers/registries.conf
unqualified-search-registries = ["docker.io"]
[[registry]]
prefix = "docker.io"
location = "xxxxxx.mirror.aliyuncs.com"

$ podman pull centos
$ mv /containers/registries.conf.d/shortnames.conf /containers/registries.conf.d/shortnames.conf.bak

镜像/容器管理   

podman与docker命令一样使用
podman pull httpd                                                      # 拉取镜像
podman images                                                          # 查看镜像
podman search mysql                                                    # 搜索容器
podman rmi d1676199e605                                                # 删除镜像
podman rmi docker.io/library/centos:latest                             # 删除镜像
podman save > centos-latest.tar docker.io/library/centos:latest        # 导出镜像
podman load -i centos-latest.tar                                       # 导入镜像
podman restart/stop/start centos-1                                         # 重启/停止/开始容器
podman inspect centos-1                                                # 查看容器详细信息
podman run centos                                                      # 运行容器
podman exec -it centos-1 /bin/sh                                       # 进入容器
podman ps                                                              # 查看运行的容器
podman ps -a                                                           # 查看使用容器
podman rm 34978941e2d2                                                 # 删除容器

podman网络管理

podman 仅支持bridge网络模式   

查看 podman 的网卡
$ ip a show cni-podman0
50: cni-podman0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 52:6e:99:5d:59:40 brd ff:ff:ff:ff:ff:ff
    inet 10.88.0.1/16 brd 10.88.255.255 scope global cni-podman0
       valid_lft forever preferred_lft forever
$ nmcli connection show 
NAME         UUID                                  TYPE      DEVICE      
cni-podman0  c4be7ee0-8d33-4f17-995d-179a0cf7661d  bridge    cni-podman0 

查看podman网络

$ podman network ls
NAME     VERSION   PLUGINS
podman   0.4.0     bridge,portmap,firewall

$ route -n
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.88.0.0       0.0.0.0         255.255.0.0     U     0      0        0 cni-podman0

$ podman inspect centos-1 | grep -i 'ip'
"IPAddress": "10.88.0.3",

podman 创建/删除 网络

$ podman network create                                                # 创建网络
$ podman network rm cni-podman1                                        # 删除网络
$ podman network ls                                                    # 查看网络
$ podman run -dt --name centos-2 --network=cni-podman1 centos          # 运行容器时指定网络

podman 持久化存储 *

创建volume资源

$ podman volume create volume01                         # 创建volume
$ podman volume ls                                      # 查看volume
$ find / -name 'volume01'                               # 查找本地系统存放volume目录
$ podman volume rm volume01                             # 删除volume

示例1:将 一个 volume 挂载到 httpd 容器中

$ podman volume create web    
# 将volume 挂载到httpd的默认网页目录中                                        
$ podman run -dt --name web1 -v web:/usr/local/apache2/htdocs -p 81:80 httpd            
# 在宿主机中修改默认网页页面
$ echo 'hello kitty' >> /var/lib/containers/storage/volumes/web/_data/index.html    
$ curl localhost:81
<html><body><h1>It works!</h1></body></html>

示例2: 直接将一个目录挂载到容器中

$ mkdir /web
$ echo 'yellow dog' > /web/index.html
# 挂载主机目录到容器
$ podman run -dt --name web2 -v /web:/usr/local/apache2/htdocs -p 82:80 httpd       
$  curl localhost:82
yellow dog

设置容器的自启动

podman 本身不支持容器自启动,但可以通过让systemd来接管,实现 podman 的自启动,需要版本1.9.3以上

两种podman容器自启动方法
1)podman generate systemd --files --name containername

这种方式,停服务即 podman stop 起服务即 podman start

--name           表示使用容器的名字来代替容器的ID
--files          表示生成systemd的服务文件
containername    表示使用哪个容器生成systemd的服务文件

设置自启动

$ podman generate systemd --files --name centos0
$ cat container-centos0.service 
[Unit]
Description=Podman container-centos0.service
Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=/run/containers/storage

[Service]
Environment=PODMAN_SYSTEMD_UNIT=%n
Restart=on-failure
TimeoutStopSec=70
ExecStart=/usr/bin/podman start centos0
ExecStop=/usr/bin/podman stop -t 10 centos0
ExecStopPost=/usr/bin/podman stop -t 10 centos0
PIDFile=/run/containers/storage/overlay-containers/c0a0ccc71c309ea2594fde69563f53d56b78fd11f2ca425041b6b4ce9be2b356/userdata/conmon.pid
Type=forking

[Install]
WantedBy=multi-user.target default.target

$ mv container-centos0.service /etc/systemd/system/

$ systemctl daemon-reload

$ restorecon --RvF /etc/systemd/system/container-centos0.service

$ systemctl enable container-centos0.service --now

$ systemctl is-enabled container-centos0.service
enabled

2)podman generate systemd --files --new --name containername
这种方式,停服务即将容器删掉、起服务即启动一个新的容器 *
设置自启动

$ podman generate systemd --files --new --name web0
$ cat container-web0.service
[Unit]
Description=Podman container-web0.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
ExecStartPre=/bin/rm -f %t/%n.ctr-id
ExecStart=/usr/bin/podman run --cidfile=%t/%n.ctr-id --sdnotify=conmon --cgroups=no-conmon --rm --replace -dt --name web0 -v web:/usr/local/apache2/htdocs -p 80:80 httpd
ExecStop=/usr/bin/podman stop --ignore --cidfile=%t/%n.ctr-id
ExecStopPost=/usr/bin/podman rm -f --ignore --cidfile=%t/%n.ctr-id
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target default.target

$ cp container-web0.service /etc/systemd/system/

$ systemctl daemon-reload

$ systemctl status container-web0.service

$ systemctl enable container-web0.service

$ systemctl is-enabled container-web0.service
enabled

非根用户运行 podman  **  

podman使用普通用户管理容器时,普通用户必须是ssh登录或者终端登录才行。
1)非根用户运行容器

$ echo 123 | passwd --stdin user1
$ ssh user1@localhost
$ podman run -dt --name web0-user2 -v /home/user1/web0:/usr/local/apache2/htdocs  httpd

2)非根用户使用systemd接管podman容器 *
a.创建~/.config/systemd/user目录来存放普通用户的systemd文件 *

$ mkdir -p ~/.config/systemd/user

b.生成systemd服务文件

$ podman generate systemd --files --new --name web0-user1

c.将服务文件移动到普通用户的systemd的目录文件

$ mv container-web0-user1.service ~/.config/systemd/user1
$ restorecon -RvF ~/.config/systemd/user/container-web0-user1.service   *

d.赋予普通用户的 systemd 管理权限 * 

$ loginctl enable-linger  

e.将podman容器设为开机自启

$ systemctl --user daemon-reload
$ systemctl --user enable container-web0-user1.service  --now
$ reboot
$ podman ps
CONTAINER ID  NAMES
2e7d376cad3d  web0-user1
举报

相关推荐

0 条评论