生成的 podman systemd 文件无法启用
Posted
技术标签:
【中文标题】生成的 podman systemd 文件无法启用【英文标题】:Generated podman systemd file can not be enabled 【发布时间】:2021-11-17 17:46:28 【问题描述】:我正在尝试编写一个 ansible playbook,它将 nginx 部署为 podman 容器,生成与所述容器相关的 systemd 单元并启用并启动该 systemd 单元。容器本身创建良好并且已启动并运行,但系统无法识别创建的单元文件。容器创建后ansible playbook相关部分如下
---
- name: Create systemd script for created container
become: true
shell: podman generate systemd -n nginx --files
register: container_systemd_file
- name: Get service name
set_fact:
service_name: " container_systemd_file.stdout | basename "
- name: Move created systemd script to correct location
become: true
shell: mv container_systemd_file.stdout /etc/systemd/system/
- name: Force systemd to reread configs
become: true
systemd: daemon_reload=yes
- name: Enable nginx container service
become: true
systemd:
name: " service_name "
enabled: true
state: started
最后一步失败并显示消息"msg": "Could not find the requested service container-nginx.service: host"
。
单元文件可以在宿主机上找到
$ ls -al /etc/systemd/system/container-nginx.service
-rw-r--r--. 1 root root 701 Sep 24 15:19 /etc/systemd/system/container-nginx.service
有内容
# container-nginx.service
# autogenerated by Podman 3.2.3
# Fri Sep 24 15:19:41 EEST 2021
[Unit]
Description=Podman container-nginx.service
Documentation=man:podman-generate-systemd(1)
Wants=network.target
After=network-online.target
RequiresMountsFor=/var/run/containers/storage
[Service]
Environment=PODMAN_SYSTEMD_UNIT=%n
Restart=on-failure
TimeoutStopSec=70
ExecStart=/usr/bin/podman start nginx
ExecStop=/usr/bin/podman stop -t 10 nginx
ExecStopPost=/usr/bin/podman stop -t 10 nginx
PIDFile=/var/run/containers/storage/overlay-containers/c5ad5d961913a8d48ea09557484eaf17749086581eb5a1750e65fb4cf8965272/userdata/conmon.pid
Type=forking
[Install]
WantedBy=multi-user.target default.target
同样,如果我运行 systemctl daemon-reload 并尝试在主机上手动启用该服务,它会失败并显示消息 Failed to enable unit: Unit file container-nginx.service does not exist.
如何让系统识别新创建的单元文件并启用服务?我正在使用 Podman 3.2.3 和 RHEL 8.4。
【问题讨论】:
【参考方案1】:我无法弄清楚为什么最初的方法不起作用,而是首先将 systemd 单元作为文件生成到用户的主目录然后移动它,而是直接将文件写入 /etc/systemd/system/
下工作。
---
- name: Create systemd script for created container
become: true
shell: podman generate systemd --new --name nginx > /etc/systemd/system/nginx.service
- name: Force systemd to reread configs
become: true
systemd: daemon_reload=yes
- name: Enable nginx container service
become: true
systemd:
name: nginx.service
enabled: true
state: started
【讨论】:
以上是关于生成的 podman systemd 文件无法启用的主要内容,如果未能解决你的问题,请参考以下文章
Docker 容器在启用 systemd 脚本后重新启动后无法启动