ikke_t.podman_container_systemd
podman-container-systemd
注意:虽然它 hopefully 仍然可用,但请注意,后续开发将在新的 linux system roles podman project 中进行。 可以尝试一下,它正在积极开发中。 感谢所有在多年来为此做出贡献的人!
这个角色使用 systemd 帮助在主机上设置要运行的容器。 Podman 实现了容器事件,但并不控制或跟踪生命周期。 这项工作由外部工具来完成,比如集群中的 Kubernetes 和本地安装中的 systemd。
我编写这个角色是为了帮助管理我个人服务器上的 podman 容器的生命周期,它不是集群。因此,我想使用 systemd 来确保它们在重启后保持启用和运行。
角色的功能:
- 安装 Podman
- 拉取所需的镜像
- 在连续运行时,如果镜像发生变化,会再次拉取镜像,并重启容器(还不支持 pod)
- 为容器或 pod 创建 systemd 文件
- 为 pod 创建 Kubernetes yaml 文件
- 如果容器的卷目录不存在,则创建卷目录。(对于pod使用 DirectoryOrCreate)
- 设置容器或 pod 在死掉时自动重启
- 使容器或 pod 在系统启动时进入运行状态
- 添加或删除容器的暴露端口到防火墙
- 支持在指定用户下运行无根容器的参数
有关该角色的参考,请查看以下两个博客:
博客描述了如何使用此模块运行单个容器或将多个容器作为一个 pod。
运行无根容器的注意事项:
- 需要在运行此角色之前先创建用户。
- 用户应该在 /etc/sub[gu]id 文件中有命名空间范围的条目。 如果没有,此角色将在其中添加一些变量,以使某些内容正常工作,但最好是您检查它们。
- 一些控制参数,例如内存或其他资源限制,在用户下将无法工作。
- 您希望大幅增加
systemd_TimeoutStartSec
,因为我们无法在 systemd 单元启动之前预取镜像。 因此,systemd 需要等待 podman 在启动容器之前拉取镜像。根据您的网络连接和容器镜像大小,可能需要几分钟。
要求
需要能够运行 podman 的系统,并且能从软件包仓库中找到 podman。角色会安装 podman。
如果用户定义了 container_firewall_ports
变量,则角色还会安装 firewalld。
如果 container_pod_yaml_template_validation: true
,则会为 pod 安装 kubeval。
角色变量
角色使用在包含时必需传递的变量。由于可以单独运行一个容器或多容器作为 pod,请注意某些选项仅适用于另一种方法。
container_image_list
- 要运行的容器镜像列表。 如果定义了多个镜像,则这些容器将在 pod 中运行。 可以将其定义为字典以包含每个镜像的认证信息,如下所示:
container_image_list:
- image: docker.io/imagename
user: exampleuser
password: examplepw
- image: docker.io/imagename2
container_image_user
- 用于认证远程注册表的可选默认用户名container_image_password
- 用于认证远程注册表的可选默认密码container_name
- 在 systemd 和 podman 命令中识别容器。 systemd 服务文件将命名为 container_name--container-pod.service。可以用 service_name 覆盖此命名。container_run_args
- 任何您传递给 podman 的内容,除了在运行单个容器时的名称和镜像。 不适用于 pod。 可以是字符串或字符串列表。container_cmd_args
- 在指定镜像名称后传递给 podman-run 的任何命令和参数。 不适用于 pod。container_run_as_user
- systemd 应该作为哪个用户运行容器。 默认是 root。container_run_as_group
- systemd 应该作为哪个组运行容器。 默认是 root。container_dir_owner
- 卷目录应该具有哪个所有者。 默认是 container_run_as_user。 如果您使用 :U 作为卷选项,podman 将自动为容器内部的用户设置权限。 引用::U 后缀告诉 Podman 使用主机的正确 UID 和 GID,基于容器内的 UID 和 GID,递归地更改源卷的所有者和组。 警告:谨慎使用,因为这将修改主机文件系统。container_dir_group
- 卷目录应该具有哪个组。 默认是 container_run_as_group。container_dir_mode
- 卷目录应该具有哪些权限。 默认值为 '0755'。container_state
- 如果状态为running
,则容器已经安装并运行;如果为absent
,则停止并移除 systemd 文件container_firewall_ports
- 您已经从容器暴露并希望开启防火墙的端口列表。 当 container_state 为 absent 时,防火墙端口将被关闭。 如果您不希望安装 firewalld,请不要定义此选项。systemd_TimeoutStartSec
- systemd 等待容器启动的时间?systemd_tempdir
- 存储单个容器的 conmon-pidfile 和 cidfile 的位置。 默认在支持该格式的系统上为%T
(请参阅 man 5 systemd.unit),否则为/tmp
。service_name
- systemd 服务文件的命名方式。 默认为"{{ container_name }}-container-pod-{{ container_run_as_user }}.service"
。service_files_dir
- 存储 systemd 服务文件的地方。 默认对 root 为/usr/local/lib/systemd/system
,对无根用户为"{{ user_info.home }}/.config/systemd/user"
。service_files_owner
- 哪个用户应该拥有 systemd 服务文件。 默认是 root。service_files_group
- 哪个组应该拥有 systemd 服务文件。 默认是 root。service_files_mode
- systemd 服务文件应该具有哪些权限。 默认是 0644。container_pod_yaml
- pod yaml 文件的路径。 pod 所需。container_pod_yaml_deploy
- 是否部署 pod yaml 文件。 默认值为false
。container_pod_yaml_template
- 用于部署 pod yaml 的模板。 模板不包括每个可能的配置选项,可以用您自己的模板覆盖。 默认为templates/container-pod-yaml.j2
。container_pod_yaml_template_validation
- 是否验证已部署的 pod yaml 文件。 默认值为false
。container_pod_labels
- 定义标签用于container_pod_yaml_deploy
。container_pod_volumes
- 定义卷用于container_pod_yaml_deploy
。container_pod_containers
- 定义容器用于container_pod_yaml_deploy
。
该任务本身没有用于解析 podman 命令参数的 Python 模块。
在此之前,您只需按您从命令行使用 podman 的方式传递所有参数。
有关信息,请参见 man podman
或 podman 教程。
如果您希望您的
镜像自动更新,
则将此标签添加到 container_cmd_args 中:--label "io.containers.autoupdate=image"
如果您打算在一个 playbook 中多次使用此角色,请勿使用 ansible.builtin.import_role
来执行此角色,否则将陷入
这个反模式。
依赖项
- containers.podman(集合)
- ansible.posix(集合)
示例 Playbook
请查看 tests/main.yml 了解示例。简而言之,包括角色和变量。
Root 容器:
- name: 测试容器
vars:
container_image_list:
- sebp/lighttpd:latest
container_name: lighttpd
container_run_args: >-
--rm
-v /tmp/podman-container-systemd:/var/www/localhost/htdocs:Z,U
--label "io.containers.autoupdate=image"
-p 8080:80
#container_state: absent
container_state: running
container_firewall_ports:
- 8080/tcp
- 8443/tcp
ansible.builtin.include_role:
name: podman-container-systemd
无根容器:
- name: 确保用户
user:
name: rootless_user
comment: 我运行示例容器
- name: 测试容器
vars:
container_run_as_user: rootless_user
container_run_as_group: rootless_user
container_image_list:
- sebp/lighttpd:latest
container_name: lighttpd
container_run_args: >-
--rm
-v /tmp/podman-container-systemd:/var/www/localhost/htdocs:Z,U
-p 8080:80
#container_state: absent
container_state: running
container_firewall_ports:
- 8080/tcp
- 8443/tcp
ansible.builtin.include_role:
name: podman-container-systemd
无根 Pod:
- name: 确保用户
user:
name: rootless_user
comment: 我运行示例容器
- name: 测试 pod
vars:
container_run_as_user: rootless_user
container_run_as_group: rootless_user
container_image_list:
- sebp/lighttpd:latest
container_name: lighttpd-pod
container_pod_yaml: /home/rootless_user/lighttpd-pod.yml
container_pod_yaml_deploy: true
container_pod_yaml_template_validation: true
container_pod_labels:
app: "{{ container_name }}"
io.containers.autoupdate: 'image(1)'
container_pod_volumes:
- name: htdocs
hostPath:
path: /tmp/podman-container-systemd
type: DirectoryOrCreate
container_pod_containers:
- name: lighttpd
image: sebp/lighttpd:latest
volumeMounts:
- name: htdocs
mountPath: /var/www/localhost/htdocs:Z
ports:
- containerPort: 80
hostPort: 8080
container_state: running
container_firewall_ports:
- 8080/tcp
- 8443/tcp
ansible.builtin.include_role:
name: podman-container-systemd
许可证
GPLv3
作者信息
Ilkka Tengvall ilkka.tengvall@iki.fi
ansible-galaxy install ikke_t.podman_container_systemd