Gluetun, NordVPN, and the CPU core I got back
How the download apps sit behind a VPN with a working kill switch, and why switching Gluetun from OpenVPN to WireGuard took its CPU use from a full core to almost nothing.
My download apps (qBittorrent, Radarr, Sonarr, Jackett and a book downloader) all run in Docker on smeagol, my main Docker VM. None of them talk to the internet directly. They all go through one container: Gluetun, connected to NordVPN.
How it's wired
Gluetun is the only container on the "vpn" network setup. The other apps join it with network_mode: service:vpn, which means they share Gluetun's network instead of having their own. A few things follow from that:
- The apps' web pages are published on Gluetun, not on the apps themselves. Gluetun's compose entry lists every port: 8080 for qBittorrent, 7878 for Radarr, and so on.
- The apps wait for Gluetun to be healthy before they start, so nothing ever runs before the tunnel is up.
- The kill switch is Gluetun's firewall. If the VPN drops, traffic stops instead of leaking out through my home connection. I tested this by breaking the tunnel on purpose. One exception lets the apps still reach my own network, for Plex and the NAS:
FIREWALL_OUTBOUND_SUBNETS.
One catch with NordVPN: it doesn't do port forwarding. Downloads still work, just with fewer incoming connections.
The problem: a CPU core at 100%
For months Gluetun ran NordVPN over OpenVPN. It worked, but OpenVPN does its encryption in a single thread in normal software, and under load that pinned a whole CPU core at 100% on a VM with only six of them.
The fix: WireGuard (NordLynx)
NordVPN's WireGuard service is called NordLynx, and Gluetun supports it. Two details are different from OpenVPN:
- Instead of a username and password, WireGuard needs a private key. Nord doesn't show it on its website. You create an access token in your Nord account, ask Nord's API for your NordLynx key with it, and store the key in the
.envfile next to the compose file, never in the compose file itself. - NordLynx always gives you the same internal address,
10.5.0.2/32, which Gluetun needs to be told.
The compose change was small: VPN_TYPE=wireguard, the key, the address, and the OpenVPN lines commented out rather than deleted, so going back is easy. Same server, same kill switch, same ports.
The result
After the switch Gluetun showed about 0.04% CPU. WireGuard runs inside the Linux kernel and uses modern encryption that's cheap to compute, so the busy core is free again. I checked from inside qBittorrent and Radarr that the world still sees a Nord address, not my home IP.
A bonus lesson from later the same day
When I gave smeagol more CPUs and RAM, I found its virtual CPU was set to Proxmox's old generic default type. That hides features the real Xeon underneath has, including the AES instructions that make encryption fast. Setting the CPU type to host gives the VM the real thing. It also turned out to be required for the database behind this site. More on that in the next post.