13.7. systemd を使用した Pod の自動起動
Pod 全体を systemd サービスとして管理することで、Pod 内のすべてのコンテナーが同時に起動および停止するようにできます。これにより、マルチコンテナーアプリケーションの管理が単純化されます。
systemctl コマンドは、Pod でだけ使用するようにしてください。コンテナーは Pod サービスと内部の infra-container で管理されているため、systemctl を使用して個別にコンテナーを開始または停止しないでください。
podman v4.6 以降では、Quadlets を使用できるようになりました。Quadlets を使用すると、通常の systemd ユニットファイルと同様の形式でコンテナーを実行する方法を記述でき、systemd でコンテナーを実行する際の複雑さを意識せずに済みます。
前提条件
-
container-toolsメタパッケージがインストールされている。
手順
たとえば、
systemd-podなどの空の Pod を作成します。$ podman pod create --name systemd-pod 11d4646ba41b1fffa51c108cbdf97cfab3213f7bd9b3e1ca52fe81b90fed5577必要に応じて、すべての Pod をリスト表示します。
$ podman pod ps POD ID NAME STATUS CREATED # OF CONTAINERS INFRA ID 11d4646ba41b systemd-pod Created 40 seconds ago 1 8a428b257111 11d4646ba41b1fffa51c108cbdf97cfab3213f7bd9b3e1ca52fe81b90fed5577空の Pod に 2 つのコンテナーを作成します。たとえば、
container0とcontainer1をsystemd-podに作成するには、以下を実行します。$ *podman create --pod systemd-pod --name container0 registry.access.redhat.com/ubi*10 top $ *podman create --pod systemd-pod --name container1 registry.access.redhat.com/ubi*10 top必要に応じて、関連付けられている全 Pod およびコンテナーをリスト表示します。
$ podman ps -a --pod CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES POD ID PODNAME 24666f47d9b2 registry.access.redhat.com/ubi10:latest top 3 minutes ago Created container0 3130f724e229 systemd-pod 56eb1bf0cdfe k8s.gcr.io/pause:3.2 4 minutes ago Created 3130f724e229-infra 3130f724e229 systemd-pod 62118d170e43 registry.access.redhat.com/ubi10:latest top 3 seconds ago Created container1 3130f724e229 systemd-pod新しい Pod の
systemdユニットファイルを生成します。$ podman generate systemd --files --name systemd-pod /home/user1/pod-systemd-pod.service /home/user1/container-container0.service /home/user1/container-container1.service3 つの
systemdユニットファイルが生成されることに注意してください。1 つはsystemd-podPod 用、2 つはコンテナーcontainer0およびcontainer1用です。pod-systemd-pod.serviceユニットファイルを表示します。$ cat pod-systemd-pod.service # pod-systemd-pod.service # autogenerated by Podman 3.3.1 # Wed Sep 8 20:49:17 CEST 2021 [Unit] Description=Podman pod-systemd-pod.service Documentation=man:podman-generate-systemd(1) Wants=network-online.target After=network-online.target RequiresMountsFor= Requires=container-container0.service container-container1.service Before=container-container0.service container-container1.service [Service] Environment=PODMAN_SYSTEMD_UNIT=%n Restart=on-failure TimeoutStopSec=70 ExecStart=/usr/bin/podman start bcb128965b8e-infra ExecStop=/usr/bin/podman stop -t 10 bcb128965b8e-infra ExecStopPost=/usr/bin/podman stop -t 10 bcb128965b8e-infra PIDFile=/run/user/1000/containers/overlay-containers/1dfdcf20e35043939ea3f80f002c65c00d560e47223685dbc3230e26fe001b29/userdata/conmon.pid Type=forking [Install] WantedBy=multi-user.target default.target-
[Unit]セクションのRequires行は、container-container0.serviceとcontainer-container1.serviceユニットファイルの依存関係を定義します。両方のユニットファイルがアクティベートされます。 -
[Service]セクションのExecStart行およびExecStop行はそれぞれ infra-container を開始して停止します。
-
container-container0.serviceユニットファイルを表示します。$ cat container-container0.service # container-container0.service # autogenerated by Podman 3.3.1 # Wed Sep 8 20:49:17 CEST 2021 [Unit] Description=Podman container-container0.service Documentation=man:podman-generate-systemd(1) Wants=network-online.target After=network-online.target RequiresMountsFor=/run/user/1000/containers BindsTo=pod-systemd-pod.service After=pod-systemd-pod.service [Service] Environment=PODMAN_SYSTEMD_UNIT=%n Restart=on-failure TimeoutStopSec=70 ExecStart=/usr/bin/podman start container0 ExecStop=/usr/bin/podman stop -t 10 container0 ExecStopPost=/usr/bin/podman stop -t 10 container0 PIDFile=/run/user/1000/containers/overlay-containers/4bccd7c8616ae5909b05317df4066fa90a64a067375af5996fdef9152f6d51f5/userdata/conmon.pid Type=forking [Install] WantedBy=multi-user.target default.target-
[Unit]セクションのBindsTol 行は、pod-systemd-pod.serviceユニットファイルの依存関係を定義します。 -
[Service]セクションのExecStart行およびExecStop行では、それぞれcontainer0を起動および停止します。
-
container-container1.serviceユニットファイルを表示します。$ cat container-container1.service生成されたファイルをすべて
$HOME/.config/systemd/userにコピーして、root 以外のユーザーとしてインストールします。$ cp pod-systemd-pod.service container-container0.service container-container1.service $HOME/.config/systemd/userユーザーのログイン時に、サービスを有効にして開始します。
$ systemctl enable --user pod-systemd-pod.service Created symlink /home/user1/.config/systemd/user/multi-user.target.wants/pod-systemd-pod.service/home/user1/.config/systemd/user/pod-systemd-pod.service. Created symlink /home/user1/.config/systemd/user/default.target.wants/pod-systemd-pod.service /home/user1/.config/systemd/user/pod-systemd-pod.service. サービスは、ユーザーのログアウト時に停止される点に注意してください。
検証
サービスが有効になっているかどうかを確認します。
$ systemctl is-enabled pod-systemd-pod.service enabled
詳細は、システム上の podman-create(1)、podman-generate-systemd(1)、および systemctl(1) man ページを参照してください。