DEV Community

IPYE
IPYE

Posted on

systemd-nspawn Containers with Routed Addresses from a Free /48

Disclosure up front: I run IPYE, a company that leases IP space. Most of this article is practical how-to; IPYE appears where a free /48, LOA, or ROA helps you try the work, with a short call to action at the end.

systemd-nspawn is the container manager already present on most systemd-based Linux hosts. It is lighter than a full container platform and well suited to running a few long-lived services, each in its own root file system. With systemd-networkd on both sides, you can give each container a public IPv6 address from your own prefix, routed by the host, without NAT or neighbour discovery proxies. This guide does that with a free IPv6 /48 from IPYE. The host receives 2001:db8:100:31::/64 from the edge router and gives it to a container named web.

Prerequisites

The /64 must be routed to the host. On the edge router, a static route such as ip -6 route add 2001:db8:100:31::/64 via 2001:db8:100:10::20, where ::20 is the host's own address, does the job. On the host, enable forwarding:

sysctl -w net.ipv6.conf.all.forwarding=1
Enter fullscreen mode Exit fullscreen mode

Make it persistent in /etc/sysctl.d/. The host also needs systemd-networkd running to manage the container's virtual Ethernet link.

Create the container

Bootstrap a Debian root file system into the machines directory:

debootstrap stable /var/lib/machines/web
systemd-nspawn -D /var/lib/machines/web passwd
Enter fullscreen mode Exit fullscreen mode

Any distribution works, as long as it uses systemd inside so networkd can configure the container side.

Container settings

# /etc/systemd/nspawn/web.nspawn
[Network]
VirtualEthernet=yes
Enter fullscreen mode Exit fullscreen mode

VirtualEthernet=yes creates a veth pair: ve-web on the host and host0 inside the container.

Host side of the link

systemd ships a default network file for container links that is aimed at IPv4 with NAT. Override it for this container with a more specific file:

# /etc/systemd/network/50-ve-web.network
[Match]
Name=ve-web

[Network]
Address=2001:db8:100:31::1/64
IPv6SendRA=yes

[IPv6Prefix]
Prefix=2001:db8:100:31::/64
Enter fullscreen mode Exit fullscreen mode

The host takes ::1 on the link and sends router advertisements for the /64, so the container learns its default route automatically. Because the file name sorts before the default 80-container-ve.network, networkd uses it for ve-web.

Container side

Inside the container, configure host0:

# /var/lib/machines/web/etc/systemd/network/50-host0.network
[Match]
Name=host0

[Network]
Address=2001:db8:100:31::80/64
IPv6AcceptRA=yes
DNS=2001:db8:100:53::53
Enter fullscreen mode Exit fullscreen mode

The static address gives the service a predictable home that you can publish in DNS. Accepting router advertisements provides the default route via the host's link-local address. Enable networkd inside the container with systemctl enable systemd-networkd from a shell in it, or by creating the symlink from the host.

Start and check

machinectl start web
machinectl list
machinectl shell web /bin/sh -c "ip -6 addr; ip -6 route"
Enter fullscreen mode Exit fullscreen mode

The container should show 2001:db8:100:31::80 on host0 and a default route via an fe80:: address. From the host, ping -6 2001:db8:100:31::80 should succeed, and once the edge routes the /64, so should a ping from the Internet.

Enable the container at boot with machinectl enable web.

Firewall on the host

The host now forwards traffic to the container, so its forward chain decides what reaches it. An nftables excerpt that allows web traffic to the container and everything outbound:

table inet filter {
  chain forward {
    type filter hook forward priority 0; policy drop;
    ct state established,related accept
    meta l4proto ipv6-icmp accept
    iifname "ve-web" accept
    oifname "ve-web" ip6 daddr 2001:db8:100:31::80 tcp dport { 80, 443 } accept
  }
}
Enter fullscreen mode Exit fullscreen mode

Add the container's services as you deploy them, and keep the default policy at drop.

More containers

Each container can get its own /64 by repeating the pattern with a new .network file, or several containers can share one /64 through a bridge. A /64 per container makes firewalling and routing obvious at the cost of more configuration; a shared bridge is simpler for many small containers. Either way, the edge routes a larger block to the host, such as a /60, and the host splits it.

Resource controls

nspawn containers run as systemd units, so the usual resource settings apply. Add a [Service] drop-in for systemd-nspawn@web.service with MemoryMax= or CPUQuota= to keep one service from starving the others.

The route to the host

All of this sits behind your edge announcing the /48. The upstream needs the LOA from the IPYE customer portal, and the ROA IPYE creates makes the announcement valid for RPKI.

Next step

Claim your free /48, route a /64 to your container host, and give the first nspawn container a public address without any NAT. Questions: ipye@turnel.com.

About IPYE

IPYE (https://ipye.com) leases IPv4, IPv6 and ASN space with instant setup. Your first IPv6 /48 is free for the first year, a real allocation you can announce on your own ASN, with a LOA from the portal and the ROA created automatically. ASN and region changes apply automatically. IPv4 /24 to /22 leases include LOA, ROA and per-/24 rDNS; WHOIS is editable on IPv6 /44 and larger. Locations: Los Angeles, New York, Ashburn, Salt Lake City, London, Amsterdam, Frankfurt. Claim your free /48: https://my.ipye.com/products/ipv6-rental. Questions: ipye@turnel.com.

Disclosure: I run IPYE

Top comments (0)