一、创建容器
假设我们已经创建好了一个容器,如下图所示,名字为centos7
二、创建服务
为了让容器能够开机自动运行,需要以服务的形式存在。
在/etc/systemd/system/目录,为centos7容器创建一个systemd单元配置文件:
sudo vim /etc/systemd/system/centos7-container.service
文件内容如下:
[Unit]
Description=centos7 container
After=firewalld.service ufw.service
[Service]
Restart=always
ExecStart=/usr/bin/podman start -a centos7
ExecStop=/usr/bin/podman stop centos7
[Install]
WantedBy=multi-user.target
需要注意的是ExecStart以及ExecStop命令中的容器名字一定要与要实际的容器名字完全一致
如果容器中有端口需要让外部访问,比如容器中的SSHD服务,需要按照配置与管理Ubuntu 21.10一文中所述,修改podman网络模式,这样即使开启了防火墙也不会被防火墙屏蔽,同时要求容器服务在防火墙服务之后启动,否则不能访问容器中的端口。
如果是CentOS系统,防火墙服务默认是firewalld,ubuntu系统默认是ufw,笔者是在ubuntu中为了方便使用cockpit管理,同时安装了firewalld服务,所以需要在firewalld以及ufw服务之后。
还有一种更加方便的方式,直接使用podman来生成,我们使用podman generate --help查看用法,可以看到可以生成两种文件:一种是生成Kubernetes的YAML文件,另一种就是生成systemd的单元文件。
witton@witton:~$ podman generate --help
Generate structured data based on containers, pods or volumes.
Description:
Generate structured data (e.g., Kubernetes YAML or systemd units) based on containers, pods or volumes.
Usage:
podman generate [command]
Available Commands:
kube Generate Kubernetes YAML from containers, pods or volumes.
systemd Generate systemd units.
生成systemd的单元文件,正是我们想要的。再输入podman generate systemd --help查看用法:
witton@witton:~$ podman generate systemd --help
Generate systemd units.
Description:
Generate systemd units for a pod or container.
The generated units can later be controlled via systemctl(1).
Usage:
podman generate systemd [options] {
CONTAINER|POD}
Examples:
podman generate systemd CTR
podman generate systemd --new --time 10 CTR
podman generate systemd --files --name POD
Options:
--container-prefix string Systemd unit name prefix for containers (default "container")
-f, --files Generate .service files instead of printing to stdout
--format string Print the created units in specified format (json)
-n, --name Use container/pod names instead of IDs
--new Create a new container instead of starting an existing one
--no-header Skip header generation
--pod-prefix string Systemd unit name prefix for pods (default "pod")
--restart-policy string Systemd restart-policy (default "on-failure")
--separator string Systemd unit name separator between name/id and prefix (default "-")
-t, --time uint Stop timeout override (default 10)
使用此命令来生成centos7的systemd单元文件:
sudo podman generate systemd -n centos7
这里只是输出到屏幕,输出内容如下:
# container-centos7.service
# autogenerated by Podman 3.2.1
# Thu Nov 25 06:09:23 UTC 2021
[Unit]
Description=Podman container-centos7.service
Documentation=man:podman-generate-systemd(1)
Wants=network.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 centos7
ExecStop=/usr/bin/podman stop -t 10 centos7
ExecStopPost=/usr/bin/podman stop -t 10 centos7
PIDFile=/run/containers/storage/overlay-containers/af476904e658613624d0aefb21e1bad8ae2123008100c7affdab98acec0f1233/userdata/conmon.pid
Type=forking
[Install]
WantedBy=multi-user.target default.target
podman命令生成的文件更为全面,建议大家使用此种方式来生成。
三、设置开机自动启动
sudo systemctl enable centos7-container.service
四、测试服务
sudo systemctl start centos7-container.service
sudo systemctl status centos7-container.service
sudo systemctl stop centos7-container.service
五、开机测试
从图中可以看到开机正常地自动启动容器服务,测试连接容器中的SSH服务
完全正常。
六、测试容器运行中情况下,重启主机
在前文设置ubuntu和CentOS的IP地址为静态地址中笔者有提到:
如果系统中有podman容器处于开启状态,则在重启ubuntu后会出现找不到网卡,找不到网卡也就没有IP地址,这里需要再次重启ubuntu才能正常。
通过查看日志发现是由于在重启前未能正常卸载/run/netns,在启动中systemd-networkd网络服务未能成功启动,所以找不到网络,无法建立网络连接。
现在将容器设置为服务了,测试一下这种情况还会不会有问题。
经过测试发现完全正常了,不会再出现此问题。