WireGuard setup between two VPS servers
Set up WireGuard between two VPS servers. Configure peers and AllowedIPs, keep OpenTelemetry ports private, and test the tunnel with a runnable Docker lab.

TL;DR: Connect two VPS servers with a private WireGuard tunnel. This is the setup we use between Tenwrite’s application and observability hosts. The application Collector sends OTLP to
10.10.0.2:4317; the gateway publishes that port only on its WireGuard address. Below are the peer configurations, the Docker connection and a local lab that sends a real trace through the tunnel.
In my observability post, I spent most of the time talking about collecting signals and being able to troubleshoot a user issue without jumping between three different services. I rather conveniently skipped over the bit that got the signals from one server to the other.
Well, let’s do that part now. 😄
Tenwrite’s backend lives on the application VPS. The observability stack lives on another VPS. I wanted the application side to send telemetry to the observability side, without putting the Collector’s ingestion port on the public internet.
We already had a WireGuard connection between the two machines. After a discussion with Rupam, I realised we could use it for the telemetry path too. There was no need to build another public ingestion endpoint for this connection.
The useful thing here is knowing exactly which traffic goes through that tunnel, and which address the receiving service actually listens on. A tunnel being up doesn’t automatically make every service private.
Tenwrite’s two-VPS network
The application VPS runs the API and workers, along with a local OpenTelemetry Collector. The observability VPS runs the gateway Collector and the backends I wrote about earlier: Loki, Tempo and VictoriaMetrics, with Grafana to query them.
For the connection between hosts, we use:
| Host | WireGuard address | Peer address it accepts |
|---|---|---|
| Application VPS | 10.10.0.1/24 |
10.10.0.2/32 |
| Observability VPS | 10.10.0.2/24 |
10.10.0.1/32 |
The telemetry route is:
Application VPS Observability VPS
API / workers / Blogger Agent
|
v
local Collector
|
10.10.0.1
|
+--- WireGuard / UDP ---> 10.10.0.2:4317
|
v
gateway Collector
|
Loki / Tempo / metrics
Those 10.10.0.x addresses belong to the WireGuard interfaces on the hosts. They aren’t Docker container addresses, and they aren’t public DNS names.
This is a different boundary from the one in the Caddy and Docker Compose guide. There, two containers on one Docker Engine share a bridge network. Here, the packets have to cross between two machines. Giving a Docker network the same name on both hosts doesn’t join them together.
What stays public, and what goes through WireGuard?
WireGuard itself still needs a reachable endpoint. In our setup, each host listens on UDP 51820. The encrypted packets travel between their public endpoints; the OTLP connection travels inside those packets.
So the thing we keep off the public interface is the telemetry service on TCP 4317. We still allow the WireGuard UDP traffic between peers. The site’s HTTPS route and the application’s existing public endpoints have their own jobs.
There are also two separate security questions here. HTTPS protects a connection in transit. But a long-lived ingestion token embedded in browser JavaScript is available to the person running that browser. Encrypting the HTTP connection doesn’t hide the token from its own client.
Our server-to-server Collector path keeps that work on the servers. WireGuard authenticates the configured peer keys and encrypts the host-to-host traffic. The Collector’s OTLP exporter doesn’t add another TLS layer on this particular hop.
That choice doesn’t mean we removed HTTPS from everything on the private network. Grafana’s separate lookup to the Tenwrite API still uses HTTPS and a dedicated API key. Its container maps the API hostname to the application’s WireGuard IP, so the network route is private while the hostname still works for certificate validation. Different connection, different requirements.
Configure WireGuard on two VPSs
For a fresh pair of Debian or Ubuntu hosts, install the tools on each:
sudo apt update
sudo apt install wireguard-tools iproute2 netcat-openbsd
The observability VPS runs Debian 13. WireGuard’s installation page lists the packages for other distributions.
Choose a tunnel subnet that doesn’t overlap an existing host, Docker or VPN network. I’ll keep our 10.10.0.x addresses here. For the public endpoints in the examples, I use 192.0.2.10 for the application host and 198.51.100.20 for observability. Replace those documentation addresses with your servers’ real public addresses.
First, generate a separate keypair on each host, in a fresh working directory:
mkdir -m 700 wireguard-setup
cd wireguard-setup
umask 077
wg genkey > privatekey
wg pubkey < privatekey > publickey
cat publickey
Exchange the public keys. Each private key stays on the machine that generated it. The WireGuard quick start describes these commands.
The application host’s wg0.conf looks like this:
[Interface]
PrivateKey = <APP_PRIVATE_KEY>
Address = 10.10.0.1/24
ListenPort = 51820
[Peer]
PublicKey = <OBS_PUBLIC_KEY>
AllowedIPs = 10.10.0.2/32
Endpoint = 198.51.100.20:51820
And the observability host’s file:
[Interface]
PrivateKey = <OBS_PRIVATE_KEY>
Address = 10.10.0.2/24
ListenPort = 51820
[Peer]
PublicKey = <APP_PUBLIC_KEY>
AllowedIPs = 10.10.0.1/32
Endpoint = 192.0.2.10:51820
Fill the placeholders with the appropriate keys. The download later in this post includes both templates.
There are a few settings worth understanding before bringing the interfaces up:
Addressassigns the local tunnel address.PublicKeyidentifies the other peer.Endpointsays where to send its encrypted UDP packets.AllowedIPsassociates inner IP addresses with that peer. It controls which destination addresses use the peer and which source addresses are accepted from it.
wg-quick also derives routes from AllowedIPs. Here, we only need the other host’s /32. There is no 0.0.0.0/0 to redirect all the VPS’s traffic through the tunnel. These are the routing rules described in the upstream wg manual and wg-quick manual.
I leave SaveConfig out of these example files so shutting down the interface doesn’t rewrite edits from its running configuration. Our existing infrastructure notes include that option; it is something to account for if you edit a live interface’s config file.
Bring the peers up and check the route
In your provider firewall and host firewall, allow UDP 51820 between the public addresses of the two peers. Keep your existing SSH access rules. There is no reason to open the telemetry port publicly as part of this step.
If you already use UFW, the application host’s UDP rule would be:
sudo ufw allow from 198.51.100.20 to any port 51820 proto udp
On the observability host, swap the source for 192.0.2.10. Those are still the example public addresses; use your real peer IPs. Your host policy also needs to permit the intended traffic on wg0. For Docker-published services, check Docker’s packet-filtering rules as well: published ports can bypass UFW’s usual traffic path.
On each host, install its completed config and start WireGuard:
sudo install -d -m 700 /etc/wireguard
sudo install -o root -g root -m 600 wg0.conf /etc/wireguard/wg0.conf
sudo systemctl enable --now wg-quick@wg0
These commands create the wg0 connection on the hosts; the Docker lab below creates its own interfaces inside containers instead.
From the application host, check:
ip route get 10.10.0.2
sudo wg show wg0
The route should use wg0 with source 10.10.0.1. WireGuard can be quiet until there is traffic, so don’t expect a recent handshake just because the interface exists. Send traffic through it, then look for a handshake and increasing transfer counters.
For two publicly reachable servers, we don’t need a keepalive just to make the connection exist. If a peer sits behind NAT and must stay reachable while idle, PersistentKeepalive = 25 on that peer can maintain the mapping. WireGuard explains the use case in its keepalive guidance.
Bind the Collector to the WireGuard address
This is the part that matters after the tunnel starts working.
On the observability VPS, the gateway’s Docker Compose port mapping is:
ports:
- "${OTEL_GATEWAY_BIND_IP:-127.0.0.1}:${OTEL_GATEWAY_HOST_PORT:-4317}:4317"
Its production environment sets:
OTEL_GATEWAY_BIND_IP=10.10.0.2
OTEL_GATEWAY_HOST_PORT=4317
Docker publishes the Collector’s port on the host’s WireGuard address, rather than 0.0.0.0:4317. Inside the ordinary gateway container, the OTLP receiver listens on 0.0.0.0:4317; it doesn’t own the host’s 10.10.0.2 address. Docker handles that hop from the host address to the container.
On the application VPS, the local Collector’s exporter uses:
OTEL_GATEWAY_ENDPOINT=10.10.0.2:4317
OTEL_GATEWAY_INSECURE=true
The relevant exporter configuration is:
exporters:
otlp_grpc/gateway:
endpoint: ${env:OTEL_GATEWAY_ENDPOINT}
tls:
insecure: ${env:OTEL_GATEWAY_INSECURE}
That insecure setting means do not use TLS for the OTLP connection. It doesn’t turn WireGuard’s encryption off, and it isn’t insecure_skip_verify to ignore a certificate error. The gateway intentionally accepts plain gRPC inside this encrypted host-to-host path.
The host-run API and workers reach the application Collector through loopback-published ports. The Blogger Agent reaches it through a local Docker network. The Collector then makes the outbound connection to 10.10.0.2. We don’t need to route every Docker subnet across WireGuard to make this work.
There is a startup dependency too: wg0 must own 10.10.0.2 before Docker starts the gateway’s address-bound listener. Otherwise the port binding can fail with an address-not-available error. For a systemd-managed Compose stack, put the dependency on the unit that starts that stack:
[Unit]
Requires[email protected]
After[email protected]
Once the gateway is running, try this from the application host:
nc -vz -w 3 10.10.0.2 4317
sudo wg show wg0
A successful TCP connection checks the route and listener. I also want to see an actual signal arrive at the other end. That is what the lab does.
Run the WireGuard and OpenTelemetry lab locally
Download the complete two-VPS WireGuard example, extract it and open a terminal inside wireguard-two-vps.
You need a Linux Docker Engine whose kernel supports WireGuard, the Compose plugin and a shell. The host doesn’t need a WireGuard configuration. The two peer containers get their own network namespaces and NET_ADMIN there; they don’t use host networking or privileged mode.
Run:
sh lab.sh up
sh lab.sh verify
The example builds an Alpine 3.24 peer image and starts two OpenTelemetry Collectors pinned to 0.163.2. The first run downloads the images and distribution packages. It generates fresh keypairs under .lab/, with restrictive permissions. Each peer gets its own private key and the other peer’s public key. That directory is excluded from Git and the Docker build context.
The four containers are:
| Container | What it does |
|---|---|
app-vps |
Owns the application’s wg0, address 10.10.0.1, and the test client. |
agent |
Shares the application peer’s namespace and accepts a sample trace on 127.0.0.1:4318. |
obs-vps |
Owns the observability peer’s wg0, address 10.10.0.2. |
gateway |
Shares the observability peer’s namespace, accepts OTLP/gRPC on 10.10.0.2:4317, and prints the sample span. |
Here the gateway can bind directly to 10.10.0.2 because it shares the namespace containing that interface. In the actual VPS deployment, WireGuard runs on the host and the separate Collector container is reached through the Docker port mapping shown above.
The lab’s application peer initiates the connection. The other peer learns its endpoint from authenticated traffic. The real-host templates above specify each peer’s public endpoint explicitly.
No lab container publishes a host port. Their underlay is an isolated Compose network, and the gateway accepts ingestion only at the tunnel address. The verification script checks:
- A uniquely named trace goes from the application-side HTTP receiver, through its gRPC exporter and WireGuard, into the gateway’s debug log.
- WireGuard reports a completed handshake.
- TCP 4317 is reachable through
10.10.0.2, but refused through the observability container’s ordinary underlay address. - None of the four containers has a published host port.
- Taking down the application’s tunnel breaks the connection. Bringing it back up lets a new trace arrive.
So we get to see both the working path and what happens when we remove the thing we think it depends on. I find that more convincing than a green container status. 😄
To inspect it yourself:
docker compose exec app-vps wg show wg0
docker compose exec app-vps ip route get 10.10.0.2
docker compose logs gateway
docker compose ps
The debug log contains the generated sample spans. Our actual gateway forwards signals to the observability backends instead.
When finished:
sh lab.sh down
That removes the demo containers and their Compose network. The generated keys stay in .lab/ for another run. Your host’s interfaces, routes and firewall rules stay as they were.
When the tunnel is up but the connection doesn’t work
The distinction between an interface, a handshake and a reachable service is useful while troubleshooting. They aren’t the same check.
| What you see | What I would check next |
|---|---|
wg0 exists, but no handshake after sending traffic |
Peer public keys, endpoint addresses, UDP 51820 and the provider and host firewalls. |
| Handshake works, but the route goes elsewhere | AllowedIPs, overlapping subnets and ip route get on the sending host. |
| The peer address is reachable, but TCP 4317 is refused | Whether the gateway is running and listening, and whether Docker published the expected host address and port. |
| Docker says it cannot assign the requested address | Whether WireGuard started before the gateway’s port binding. |
| OTLP reports a TLS or protocol error | Whether the exporter and receiver agree on gRPC versus HTTP and on TLS. |
| One container cannot resolve the other host’s Docker alias | Docker DNS is local to its networks. Use the peer’s tunnel address or deliberately configured private DNS across hosts. |
| Small requests work, larger transfers stall | Path MTU, blocked ICMP needed for MTU discovery, and interface MTU settings. |
WireGuard’s AllowedIPs doesn’t select application ports. A peer trusted for 10.10.0.1/32 can send traffic from that address; service listeners and firewall rules still determine what it can reach. Keep the allowed addresses narrow and expose only the services that connection needs.
And an encrypted tunnel doesn’t hide a token from the application holding it, repair a compromised peer, or make application authentication unnecessary. That is why the API lookup I mentioned earlier keeps its own credentials and HTTPS even while taking the private route.
The part I wanted to keep simple
The application Collector has a specific destination. The observability gateway has a specific host address to publish. The public interfaces carry WireGuard’s encrypted packets, while the telemetry listener stays on the tunnel address.
That separation helps when something breaks too. I can check the route, handshake, port and actual signal delivery in order, rather than changing random Collector settings because a dashboard is empty.
The observability post covers what we do with those signals after they arrive. The Caddy guide covers services talking on one Docker host. This connection is the bit between the two machines. It is another example of what I meant in the on-prem to cloud post: where things run and how they talk to each other still matter, regardless of how easy it was to provision them.