fahrradflucht
fahrradflucht

Reputation: 1595

Require and start podman network interface using systemd

I have a host service managed by systemd which listens on the Podman default network interface (cni-podman0) so that containers can talk to it.

The problem I have, is that Podman only creates the network interface when the first container is started. That means when the host service which the containers depend on is started, the network interface isn't up and the service fails to listen on it.

So the dependency chain is: Podman container -needs> Host Service -needs> CNI network interface

But currently the only way I know of to bring up the interface is starting the container.

How can I make systemd tell Podman / CNI to start the default bridge network interface, so that I can depend on that in the host service unit?

Is there a command to bring up the interface explicitly, I could put in a unit file?

Upvotes: 3

Views: 2439

Answers (3)

Joel Purra
Joel Purra

Reputation: 25127

See Podman Quadlet, which is an official part of Podman.

Quadlets allow using augmented Podman-related unit files which may depend on each other. It also abstracts away listing arguments in ExecStart= in favor of unit-specific uses, such as UserNS=keep-id.

Try creating two separate unit files: a [Container] with Network=<name>.network, and a [Network] in <name>.network.

Network=
Specify a custom network for the container. This has the same format as the --network option to podman run. For example, use host to use the host network in the container, or none to not set up networking in the container.

As a special case, if the name of the network ends with .network, a Podman network called systemd-$name is used, and the generated systemd service contains a dependency on the $name-network.service. Such a network can be automatically created by using a $name.network Quadlet file.

This key can be listed multiple times.

You can use Quadlet units rootful or rootless, depending on if you place them in, for example, /etc/containers/systemd/ or ~/.config/containers/systemd/.

Upvotes: 1

fahrradflucht
fahrradflucht

Reputation: 1595

I solved it for now by adding a oneshot systemd service unit to the host service dependencies, which runs an immediately exiting alpine container using Podman. This "tricks" Podman into bringing up the bridge network interface.

Less hacky solutions are still more than welcome.

Upvotes: 0

ccpizza
ccpizza

Reputation: 31696

Unless I misunderstood the question, it's possible to use the After and Wants parameters in your systemd service file.

Open your service file, e.g. vim /etc/systemd/system/my_custom_daemon.service and make sure you have the following:

[Unit]
After=network.target
Wants=network.target

If it's not the host network that you need to satisfy as a precondition then you'd need to create a custom systemd target and reference it in your After/Wants.

Upvotes: 0

Related Questions