1. 首页 > Linux教程 > 正文

Linux教程FG432-Podman Systemd集成

内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。

本文档

风哥提示:

介学习交流加群风哥QQ113257174绍Podman与Systemd的集成方法。

Part01-Systemd服务单元

1.1 生成Systemd服务

# 创建容器
[root@podman ~]# podman run -d –name fgedu-web \
-p 80:80 \
-v /opt/fgedu/html:/usr/share/nginx/html \
nginx:latest
abc123def456789012345678901234567890123456789012345678901234

# 生成Systemd服务文件
[root@podman ~]# podman generate systemd –name fgedu-web –files –new
/root/container-fgedu-web.service

# 查看生成的服务文件
[root@podman ~]# cat /root/container-fgedu-web.service
[Unit]
Description=Podman container-fgedu-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 \
–cidfile=%t/%n.ctr-id \
–cgroups=no-conmon \
–rm \
-d \
–replace \
–name fgedu-web \
-p 80:80 \
-v /opt/fgedu/html:/usr/share/nginx/html \
docker.io/library/nginx:latest
ExecStop=/usr/bin/podman stop \
–ignore \
–cidfile=%t/%n.ctr-id \
-t 10
ExecStopPost=/usr/bin/podman rm \
from PG视频:www.itpux.com-f \
–ignore \
–cidfile=%t/%n.ctr-id
Type=forking
PIDFile=%t/%n.pid

[Install]
WantedBy=default.target

# 复制服务文件到Systemd目录
[root@podman ~]# cp /root/container-fgedu-web.service /etc/systemd/system/

# 重载Systemd配置
[root@podman ~]# systemctl daemon-reload

# 启用并启动服务
[root@podman ~]# systemctl enable –now container-fgedu-web
Created symlink /etc/systemd/system/multi-user.target.wants/container-fgedu-web.service → /etc/systemd/system/container-fgedu-web.service.

# 查看服务状态
[root@podman ~]# systemctl status container-fgedu-web
● container-fgedu-web.service – Podman container-fgedu-web.service
Loaded: loaded (/etc/systemd/system/container-fgedu-web.service; enabled; preset: disabled)
Active: active (running) since Sat 2026-04-04 08:00:00 CST; 10s ago
Docs: man:podman-generate-systemd(1)
Process: 12345 ExecStart=/usr/bin/podman run –cidfile=/run/container-fgedu-web.ctr-id –cgroups=no-conmon –rm -d –replace –name fgedu-web -p 80:80 -v /opt/fgedu/html:/usr/share/nginx/html docker.io/library/nginx:latest (code=exited, status=0/SUCCESS)
Main PID: 12400 (conmon)
Tasks: 3 (limit: 23456)
Memory: 25.5M
CPU: 100ms
CGroup: /system.slice/container-fgedu-web.service
└─12400 /usr/bin/conmon –api-version 1 -c abc123def456789012345678901234567890123456789012345678901234 -u abc123def456789012345678901234567890123456789012345678901234 -r /usr/bin/crun -b /var/lib/containers/storage/overlay-containers/abc123def456789012345678901234567890123456789012345678901234/userdata -p /run/container-fgedu-web.pid –exit-dir /run/libpod/exits –full-attach -l k8s-file:/var/lib/containers/storage/overlay-containers/abc123def4567890123456789012345678更多学习教程公众号风哥教程itpux_com90123456789012345678901234/userdata/ctr.log –log-level warning –runtime-arg –log-format=json –runtime-arg –log-path=/var/lib/containers/storage/overlay-containers/abc123def456789012345678901234567890123456789012345678901234/userdata/ctr.log –runtime-arg –log-max-size=0 –runtime-arg –log-max-files=0 -t fgedu-web -e PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin -e TERM=xterm -e container=podman –detach –exit-command /usr/bin/podman –exit-command-arg –root –exit-command-arg /var/lib/containers/storage –exit-command-arg –runroot –exit-command-arg /run/containers/storage –exit-command-arg –log-level –exit-command-arg warning –exit-command-arg –cgroup-manager –exit-command-arg systemd –exit-command-arg –tmpdir –exit-command-arg /run/libpod –exit-command-arg –runtime –exit-command-arg crun –exit-command-arg –storage-driver –exit-command-arg overlay –exit-command-arg –storage-opt –exit-command-arg overlay.mount_program=/usr/bin/fuse-overlayfs –exit-command-arg –events-backend –exit-command-arg journald –exit-command-arg –syslog –exit-command-arg container –exit-command-arg cleanup –exit-command-arg abc123def456789012345678901234567890123456789012345678901234 fgedu-web

Apr 04 08:00:00 podman.fgedu.net.cn podman[12345]: 2026-04-04 08:00:00.123456789 +0800 CST m=+0.123456789 container init abc123def456789012345678901234567890123456789012345678901234 (image=docker.io/library/nginx:latest, name=fgedu-web)
Apr 04 08:00:00 podman.fgedu.net.cn podman[12345]: 2026-04-04 08:00:00.123456789 +0800 CST m=+0.123456789 container start abc123def456789012345678901234567890123456789012345678901234 (image=docker.io/library/nginx:latest, name=fgedu-web)

Part02-Pod Systemd服务

2.1 Pod服务管理

# 创建Pod
[root@podman ~]# podman pod create –name fgedu-pod -p 8080:80

# 在Pod中添加容器
[root@podman ~]# podman run -d –pod fgedu-pod –name fgedu-web nginx:latest
abc123def456789012345678901234567890123456789012345678901234

[root@podman ~]# podman run -d –pod fgedu-pod –name fgedu-app python:3.11-slim
def456789012345678901234567890123456789012345678901234

# 生成Pod的Systemd服务
[root@podman ~]# podman generate systemd –name fgedu-pod –files –new
/root/pod-fgedu-pod.service
/root/container-fgedu-web.service
/root/container-fgedu-app.service

# 查看Pod服务文件
[root@podman ~]# cat /root/pod-fgedu-pod.service
[Unit]
Description=Podman pod-fgedu-pod.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 pod start abc123def456789012345678901234567890123456789012345678901234
ExecStop=/usr/bin/podman pod stop -t 10 abc123def456789012345678901234567890123456789012345678901234
ExecStopPost=/usr/bin/podman pod rm abc123def456789012345678901234567890123456789012345678901234
PIDFile=%t/pod-fgedu-pod.pid
Type=forking

[Install]
WantedBy=default.target

# 复制所有服务文件
[root@podman ~]# cp /root/pod-*.service /root/container-*.service /etc/systemd/system/

# 启用Pod服务
[root@podman ~]# systemctl enable –now pod-fgedu-pod
Created symlink /etc/systemd/system/multi-user.target.wants/pod-fgedu-pod.更多视频教程www.fgedu.net.cnservice → /etc/systemd/system/pod-fgedu-pod.service.

# 查看服务状态
[root@podman ~]# systemctl status pod-fgedu-pod
● pod-fgedu-pod.service – Podman pod-fgedu-pod.service
Loaded: loaded (/etc/systemd/system/pod-fgedu-pod.service; enabled; preset: disabled)
Active: active (running) since Sat 2026-04-04 08:00:00 CST; 10s ago
Docs: man:podman-generate-systemd(1)
Process: 12345 ExecStart=/usr/bin/podman pod start abc123def456789012345678901234567890123456789012345678901234 (code=exited, status=0/SUCCESS)
Main PID: 12400 (conmon)
Tasks: 5 (limit: 23456)
Memory: 50.5M
CPU: 200ms
CGroup: /system.slice/pod-fgedu-pod.service
├─12400 /usr/bin/conmon –api-version 1 -c abc123def456789012345678901234567890123456789012345678901234 …
└─12500 /usr/bin/conmon –api-version 1 -c def456789012345678901234567890123456789012345678901234 …

Part03-Quadlet配置

3.1 使用Quadlet

# 创建Quadlet目录
[root@podman ~]# mkdir -p /etc/containers/systemd

# 创建容器配置文件
[root@podman ~]# cat > /etc/containers/systemd/fgedu-web.container << 'EOF' [Unit] Description=FGEDU Web Container [Container] Image=docker.io/library/nginx:latest Name=fgedu-web PublishPort=80:80 Volume=/opt/fgedu/html:/usr/share/nginx/html [Service] Restart=always [Install] WantedBy=multi-user.target EOF # 创建Pod配置文件 [root@podman ~]# cat > /etc/containers/systemd/fgedu-pod.pod << 'EOF' [Unit] Description=FGEDU Application Pod [Pod] Name=fgedu-pod PublishPort=8080:80 [Install] WantedBy=multi-user.target EOF # 创建Pod中的容器配置 [root@podman ~]# cat > /etc/containers/systemd/fgedu-app.container << 'EOF' [Unit] Description=FGEDU App Container Requires=fgedu-pod.service [Container] Image=docker.io/library/python:3.11-slim Name=fgedu-app Pod=fgedu-pod.pod Environment=DB_HOST=localhost [Service] Restart=always [Install] WantedBy=multi-user.target EOF # 重载Systemd [root@podman ~]# systemctl daemon-reload # 启动服务 [root@podman ~]# systemctl enable --now fgedu-web Created symlink /etc/systemd/system/multi-user.target.wants/fgedu-web.service → /etc/systemd/system/fgedu-web.service. # 查看生成的服务 [root@podman ~]# systemctl status fgedu-web ● fgedu-web.service - FGEDU Web Container Loaded: loaded (/etc/containers/systemd/fgedu-web.container; generated) Active: active (running) since Sat 2026-04-04 08:00:00 CST; 10s ago Main PID: 12400 (conmon) Tasks: 3 (limit: 23456) Memory: 25.5M CPU: 100ms CGroup: /system.slice/fgedu-web.service

风哥针对Systemd集成建议:

  • 使用Quadlet简化配置
  • 配置自动重启策略
  • 设置服务依赖关系
  • 配置资源限制
  • 定期检查服务状态

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息