k8s-ocp-yaml
Search…
2021-05-17-podman添加开机自启
podman因为不像docker那样有daemon进程,启动容器不再支持 restart 参数,对于系统重启后,需要自启的容器需要手动做一些配置,如通过 systemd 来管理。
podman 提供了命令生成 systemd 配置文件。前提是容器已经创建。
1
# 先看下容器名称,最后一个参数
2
[[email protected] ~]# podman ps
3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4
396f8e6b7bf3 docker.io/library/registry:2.6 /etc/docker/regis... 4 months ago Up 11 minutes ago 0.0.0.0:5000->5000/tcp mirror-registry
5
6
# systemd 后面跟容器名称
7
8
[[email protected] ~]# podman generate systemd mirror-registry
9
# container-396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38.service
10
# autogenerated by Podman 1.6.4
11
# Mon May 17 16:43:07 CST 2021
12
13
[Unit]
14
Description=Podman container-396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38.service
15
Documentation=man:podman-generate-systemd(1)
16
17
[Service]
18
Restart=on-failure
19
ExecStart=/usr/bin/podman start 396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38
20
ExecStop=/usr/bin/podman stop -t 10 396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38
21
KillMode=none
22
Type=forking
23
PIDFile=/var/run/containers/storage/overlay-containers/396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38/userdata/conmon.pid
24
25
[Install]
26
WantedBy=multi-user.target
Copied!
生成配置文件,并移到目录 /usr/lib/systemd/system/ 并设置开机自启
1
podman generate systemd mirror-registry > mirror-registry.service
2
mv mirror-registry.service /usr/lib/systemd/system/
3
4
systemctl enable mirror-registry
Copied!
测试一下,要确保此容器停止不影响业务。
第一次systemctl stop 没有成功,可能和当前还没有 PIDFile 有关,手动 stop 一下。 然后再测试 start, stop ,系统重启
1
[[email protected] ~]# systemctl stop mirror-registry
2
[[email protected] ~]# podman stop -t 10 396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38
3
4
[[email protected] ~]# podman ps
5
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6
7
[[email protected] ~]# systemctl start mirror-registry
8
9
[[email protected] ~]# podman ps
10
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
11
396f8e6b7bf3 docker.io/library/registry:2.6 /etc/docker/regis... 4 months ago Up 2 seconds ago 0.0.0.0:5000->5000/tcp mirror-registry
12
13
[[email protected] ~]# systemctl stop mirror-registry
14
[[email protected] ~]# podman ps
15
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
16
17
# 可以通过 systemctl 控制启动与停止
Copied!
重启系统测试下能不能自启动
1
[[email protected] ~]# reboot
2
3
[[email protected] ~]# podman ps
4
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5
396f8e6b7bf3 docker.io/library/registry:2.6 /etc/docker/regis... 4 months ago Up 10 minutes ago 0.0.0.0:5000->5000/tcp mirror-registry
6
7
8
[[email protected] ~]# systemctl status mirror-registry.service
9
● mirror-registry.service - Podman container-396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38.service
10
Loaded: loaded (/usr/lib/systemd/system/mirror-registry.service; enabled; vendor preset: disabled)
11
Active: active (running) since Mon 2021-05-17 17:03:47 CST; 10min ago
12
Docs: man:podman-generate-systemd(1)
13
Process: 3337 ExecStart=/usr/bin/podman start 396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38 (code=exited, status=0/SUCCESS)
14
Main PID: 5847 (conmon)
15
CGroup: /system.slice/mirror-registry.service
16
5847 /usr/bin/conmon --api-version 1 -s -c 396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38 -u 396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4...
17
18
May 16 20:58:37 bastion systemd[1]: Starting Podman container-396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38.service...
19
May 17 17:03:46 bastion podman[3337]: 2021-05-17 17:03:46.645432077 +0800 CST m=+5.691401362 system refresh
20
May 17 17:03:47 bastion podman[3337]: 2021-05-17 17:03:47.60543448 +0800 CST m=+6.651403749 container init 396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156...r-registry)
21
May 17 17:03:47 bastion podman[3337]: 2021-05-17 17:03:47.674173834 +0800 CST m=+6.720143108 container start 396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd44561...r-registry)
22
May 17 17:03:47 bastion podman[3337]: 396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38
23
May 17 17:03:47 bastion systemd[1]: Started Podman container-396f8e6b7bf3bb2f0b92f0f0073da54c68b17905fd4456156e9cbcae035bba38.service.
24
Hint: Some lines were ellipsized, use -l to show in full.
Copied!
Copy link