如何让 Podman 容器作为系统服务运行 ?

众所周知,podman 是一个开源的无守护进程工具,它提供了构建、运行和管理容器的环境。将容器作为 systemd service 运行意味着当系统重新启动时,容器将自动启动。

在这篇文章中,我们将学习如何在基于 RHEL 的发行版 (如 RHEL 8、CentOS 8 和 Rocky Linux 8) 上使用 podman 将容器作为系统服务运行。

前提条件

  • Minimal RHEL based OS Installation.
  • Stable Internet Connection
  • Sudo User with root privileges

(1) 安装 Podman

要在 RHEL 8 上安装 podman,请运行

$ sudo dnf install @container-tools -y

要在 CentOS 8 / Rocky Linux 8 上安装 podman,请运行

$ sudo dnf install -y podman

要检查 podman 是否安装成功,请尝试使用 podman 下面的命令运行 hello-world 容器。

$ podman -v
podman version 3.3.1
$
$ podman run 'hello-world'

注意:当我们第一次运行 podman 时,它会提示我们选择想要下载容器映像的托管仓库。

podman-installation-verification-rhel8

输出确认 podman 已成功安装,让我们进入下一步。

(2) 生成 systemd 服务

假设要给 rsyslog 容器生成 systemd 服务,首先使用以下 podman 命令启动 rsyslog 容器

$ podman run -d --name <Container-Name>  <Image-Name>

注意: 如果您希望从特定的仓库下载 rsyslog 容器映像,请使用以下语法

$ podman run -d --name container-name  <registry-URL>/<image-name>

在这个演示中,我使用了 Red hat 仓库,首先登录仓库

$ podman login registry.access.redhat.com
Username: <Specify-User-Name>
Password: <Enter-Password>
Login Succeeded!
$

现在尝试使用以下 podman 命令运行容器

$ podman run -d --name rsyslog-server registry.access.redhat.com/rhel7/rsyslog
$ podman ps

podman-run-rsyslog-container-linux

现在,为 rsyslog-server 容器创建 systemd 服务,运行以下命令

$ mkdir -p .config/systemd/user
$ cd .config/systemd/user/
$ podman generate systemd --name rsyslog-server --files --new
/home/sysops/.config/systemd/user/container-rsyslog-server.service
$

有关 podman generate systemd 命令的详细信息,请参阅它的帮助页面

$ podman generate systemd --help

podman-generate-systemd-command

(3) 启动和启用容器 Systemd 服务

运行以下命令启动并启用 rsyslog-server 容器的 systemd 服务

$ cd .config/systemd/user/
$ systemctl --user daemon-reload
$ systemctl --user enable container-rsyslog-server.service
$ systemctl --user restart container-rsyslog-server.service

验证 systemd 服务状态和容器状态

$ systemctl --user status container-rsyslog-server.service
$ podman ps

当系统重新启动时,容器将通过它的 systemd 服务自动启动。我们重启一次并验证容器服务。

$ sudo reboot

系统重启后,然后登录系统并验证容器服务

$ podman ps
$ cd .config/systemd/user/
$ systemctl --user status container-rsyslog-server.service

container-status-post-reboot

很好,这确认了 rsyslog-server 容器在重启后通过它的 systemd 服务成功自动启动。

我的开源项目

酷瓜云课堂-开源网校系统

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值