
A virtual private cloud (VPC) is a private network where you can deploy cloud servers, cloud GPUs, load balancers, and other Vultr resources to protect their network traffic. A VPC exists in a single Vultr location and cannot communicate with the internet or other VPCs. However, you can use VPN gateways to create a VPC peering connection between different locations, allowing them to communicate securely with each other.
VPC peering can improve security and flexibility in your network architecture. Notably, it's possible to deploy servers in a VPC with no public IP address while allowing them to communicate securely over the peering connection. This guide includes a practical example of that scenario.
Before you begin, it's helpful to visualize the complete scenario. By following the steps in this guide, you'll create the following resources and connections:
After you finish these steps, users can connect to App-Server, which in turn connects to DB-Server over the peering connection, even though the database is completely isolated from the public internet.
Please review these tutorial notes before following this guide.
Note the server names in the code examples. Because you'll make multiple SSH connections to different servers and run similar commands with different details for each, we've prefixed all commands in this guide with the server name for clarity.
You must change the public IPs with your servers' actual public IPs, which you'll find in the Vultr Console after you deploy them. The public IPs in this guide are documentation examples.
You don't need to change the VPC and Wireguard IPs; these are valid RFC1918 private addresses. If you prefer a different address scheme or these examples conflict with your existing infrastructure, choose your VPC and Wireguard IPs from the RFC1918 ranges.
Here's a summary table of the IPs used in this guide.
In this step, you'll create the VPCs in each location.
172.16.1.0 in Network Address.24 in Network Prefix.VPC-A for the network name, then click Add Network.These are the exact steps you did for VPC-A, except the network subnet and location are different.
172.16.2.0 in Network Address.24 in Network Prefix.VPC-B for the network name, then click Add Network.When finished, you'll have two VPCs, like this:
In this step, you'll deploy the servers and gateways.
Follow these steps to deploy two identical servers in Amsterdam.
2 in the Servers Qty field at the bottom of the screen.Gateway-A in the Server 1 Hostname & Label section.App-Server in the Server 2 Hostname & Label section.For demonstration, DB-Server in Bangalore has no public IP address. Because it has a different configuration than Gateway-B, you'll deploy these servers simultaneously.
Gateway-B in the Server Hostname & Label section.DB-Server has no public IP address and is only reachable through the VPC, so the steps differ slightly from the previous three deployments.
Follow this link to deploy a new server.
Select Optimized Cloud Compute - General Purpose.
Choose the Bangalore location.
Choose the Ubuntu 22.04 LTS x64 operating system.
Choose the 30 GB NVMe server size.
In the Additional Features section:
The Additional Features section looks like this after your selections:
Enter DB-Server in the Server Hostname & Label section.
Click Deploy Now.
You should now have four servers deployed. The overview page shows the servers and their public IP addresses.
Make a note of the public addresses for the two gateways and App-Server.
Because you created the VPCs before deploying the servers, Vultr could automatically configure each server's private network adapter with cloud-init.
To confirm the VPC network configuration for each server:
Verify all servers have their VPCs configured and note the addresses before proceeding.
In this section, you'll install Wireguard on both gateways.
Follow these steps for Gateway-A, then repeat the same steps on Gateway-B before proceeding to the next section.
From your local workstation, open a terminal and SSH to the server as the root user.
Update the server.
Reboot the server to ensure all updates are applied, then reconnect with SSH.
Install Wireguard.
Create Wireguard's private key.
Create Wireguard's public key.
View the keys with cat, and make a note of them. This guide uses example values for clarity.
Repeat these steps on Gateway-B and make a note of its public and private keys before proceeding to the next section.
In this section, you'll create a Wireguard interface on each machine and configure it with the remote gateway's address and public/private key information.
SSH to Gateway-A.
Create the Wireguard configuration file. The Wireguard interface takes its name from the filename, and the standard convention is to name the first Wireguard interface wg0.
Copy these settings into wg0.conf.
Change this example file:
PrivateKey to Gateway-A's private key.Address is the correct Wireguard IP address for Gateway-A. If you've followed the Wireguard and VPC addressing scheme from this guide, you won't need to change this.PublicKey to Gateway-B's public key.Endpoint to Gateway-B's public IP address, followed by :51820 to indicate the remote port.AllowedIPs matches Gateway-B's Wireguard IP and VPC-B subnet. If you've followed the Wireguard and VPC addressing scheme from this guide, you won't need to change these.These other settings rarely need to be changed:
# Firewall Rules:PreUp commands run before Wireguard brings up the tunnel. Here, it opens the firewall port 51820/udp.PostDown commands run after Wireguard brings down the tunnel. Here, it closes the firewall port.# IP forwarding:PreUp setting enables IP forwarding, which allows packets from the VPN tunnel to be passed to the VPC interface.Save and exit the file.
Start the Wireguard service.
Enable the Wireguard service to restart after the server reboots.
Gateway-A is now waiting for Gateway-B to come online, which you'll do in the next step.
Next, you'll connect to Gateway-B to configure the other end of the tunnel. These steps are similar to the ones you did for Gateway-A, but the addresses and keys differ.
SSH to Gateway-B.
Create the wg0.conf configuration file.
Copy these settings into wg0.conf.
Change this example file:
PrivateKey to Gateway-B's private key.Address is the correct Wireguard IP address for Gateway-B. If you've followed the Wireguard and VPC addressing scheme from this guide, you won't need to change this.PublicKey to Gateway-A's public key.Endpoint to Gateway-A's public IP address, followed by :51820 to indicate the remote port.AllowedIPs matches Gateway-A's VPN endpoint and VPC-A subnet. If you've followed the Wireguard and VPC addressing scheme from this guide, you won't need to change these. Like Gateway-A's example, you usually won't need to change the # Firewall Rules or # IP forwarding sections.
Start Wireguard on Gateway-B.
Enable the Wireguard service to restart after the server reboots.
This establishes the VPN tunnel. You should test the connection at this stage before proceeding.
Verify the wg0 interface was created:
You'll see something similar to:
Ping Gateway-A's Wireguard address to test the VPN link.
If successful, you'll receive a reply:
Use the wg command to get some statistics:
You'll see something like this, except with your public keys and IPs:
If the output shows the latest handshake and transfer statistics at the bottom, the link is active, and you can proceed to the next section.
In this section, you'll add routing information to App-Server and DB-Server.
Although we've designated these as App and DB servers, this guide only focuses on the network configuration and doesn't include the steps to install any application or database software. See our documentation library for instructions to install many popular applications.
Vultr uses cloud-init, which auto-configures the network interfaces when you deploy the servers. To ensure cloud-init doesn't overwrite your routing entries when the system restarts, you'll disable the networking portion of cloud-init before you add them.
To get started, SSH to App-Server.
Disable the cloud-init networking module.
Make a backup of cloud-init's network configuration.
Rename the original file to wireguard_network_config.yaml.
Open the file in an editor.
Add this routes: section before the last line at the same indentation level.
When finished, your file should look similar to this.
Save and close the file.
Apply the new network settings.
Test the route from App-Server to Gateway-B's VPC address:
If your ping is successful, proceed to the next step.
In this step, you'll configure DB-Server in Bangalore and test the entire route. Because it has no public IP address, you'll connect through Gateway-B as a jump server.
First, SSH to Gateway-B from your local workstation.
Then, SSH to DB-Server.
Disable the cloud-init networking module.
Back up the cloud-init configuration.
Rename the file to wireguard_network_config.yaml.
Open the file in an editor.
Add this routes: section before the last line at the same indentation level.
When finished, your file should look similar to this.
Save and close the file.
Apply the new network settings.
Test the route from DB-Server to App-Server's VPC address:
All the configuration is complete; it's time to test the entire route.
SSH to App-Server.
Test the connection to Gateway-A's VPC address in Amsterdam:
Notice the response time is less than 1 ms because both servers are in Amsterdam.
Test your connection to Gateway-B's VPC address in Bangalore.
Notice the latency is higher because the traffic traversed the VPN.
Test your connection to DB-Server's VPC address in Bangalore, which shows that the entire route is working.
Following this guide, you've created a VPC peering connection between Vultr locations and deployed application servers in the VPCs.
As a next step, you could expand this network by adding more servers at either location. For example, if you want to add a second server named Web-Server in Amsterdam, perform the exact steps you did for App-Server. Briefly, those steps are:
Create a new server in Amsterdam, ensuring to select Enable Virtual Private Clouds from the deploy page.
Disable the cloud-init networking module.
Back up and rename the config file.
Open the file in an editor.
Add the routes: section.
Save the file and apply the network settings.
Here are resources where you can learn more about Vultr VPCs and Wireguard:
0 Comments
Be the first to comment and share your perspective with the community.