Hands-On Guide: Google Cloud VPC Network Peering to Connect Two VMs and Check Nginx Server Access.

Mostafizur Rahman
7 min readAug 23, 2023

--

Today, we we dive into VPC network peering within Google Cloud Platform (GCP) through a hands-on demonstration and comprehensive guide.

Overview:

We’ll create separate VPCs in different regions, set up VMs in each VPC, and deploy an Nginx server within Google Cloud Platform (GCP). And then, we will establish a connection between two VMs using VPC network peering.

Here is an overview of todays hands on:

VPC Peering Overview
VPC Peering Overview

Prerequisites:

In the hands on demo, we need a Google Cloud Platform(GCP) account or playground and small knowledge about VPC, VM instance, Subnet, Network interface.

Step-1: Create Two VPCs:

From the GCP dashboard, go to the VPC network and select the VPC networks.

VPC networks option
VPC networks option

New click the CREATE VPC NETWORK:

Create VPC network
Create VPC network

VPC-1:

Here the name this VPC is given vpc-1, you can have your own here.

VPC name

At the New subnet option, provide a subnet name, Region and IPv4 range. We have give the name here as subnet-1, Region us-east1 and network range 10.10.0.0/24.

VPC subnet
VPC subnet

For this demo, choose all firewall rules under Firewall rules, but remember that in a production environment, a more selective approach is essential.

VPC Firewall rules
VPC Firewall rules

Now click the CREATE button to complete the creation of first VPC.

Create VPC
Create VPC

VPC-2:

Now we will create our second VPC in the similar way. Here the name for this VPC is given vpc-2, you can have your own here.

VPC name
VPC name

At the New subnet option, we have given the subnet name as subnet-2, Region us-south1 and IPv4 range 192.168.0.0/24.

At the Firewall rules option, select all rules as done before.

VPC Firewall rules
VPC Firewall rules

Now click the CREATE button to complete the creation of first VPC.

Create VPC
Create VPC

And now we can check we have tow VPCs (vpc-1, vpc-2)is ready to move next step.

VPC Network List
VPC Network List

Step-2: Create Two VMs:

Now this is time to crate two VMs under the two VPCs we have already created.

From the GCP dashboard, go to the Compute Engine and select the VM instances.

VM instance option
VM Instance Option

Now click the CREATE INSTANCE as marked.

Create VM Instance

VM-1:

Now we have given a name for the first VM as vm-1, selected the Region us-east1 (as we have vpc-1 at the same Region) with available Zone and selected a Machine configuration E2.

Instance Name, Configuration
Instance Name, Configuration

At the Firewall option, selected both (HTTP,HTTPS)

Instance Firewall rules
Instance Firewall rules

At the Advanced options, expand the Networking and

Advanced options
Advanced options

Now Provide the network interface. As we are creating this VM under the VPC-1 so we have selected vpc-1 having the subnet-1 with an IP range 10.10.0.0/24.

VM Network Interface
VM Network Interface

Now click the CREATE button to complete the first VM creation.

Create Instance
Create Instance

VM-2:

For the second VM we have the similar steps as below. We have given VM name as vm-2, selected the Region us-south1 (as we have vpc-2 at the same Region) with available Zone and selected a Machine configuration E2.

Instance Name, Configuration
Instance Name, Configuration

At the Firewall option, selected both (HTTP,HTTPS)

Instance Firewall rules
Instance Firewall rules

At the Advanced options, expand the Networking and

Advanced options
Advanced options

Now Provide the network interface. As we are creating this VM under the VPC-2 so we have selected vpc-2 having the subnet-2 with an IP range 192.168.0.0/24.

VM Network Interface
VM Network Interface

Now click the CREATE button to complete the first VM creation.

Create Instance
Create Instance

Now we have two VM ready.

VM instance List
VM instance List

Step-3: Connect VM through SSH :

Now we can connect our VM through SSH. To do it, go to the VM instances and click SSH on vm-1.

We can see an Authorize pop-up.

SSH Authorization
SSH Authorization

After clicking the Authorize button, we can get access to our vm-1 terminal.

vm-1 Terminal
vm-1 Terminal

Now if we try to connect from vm-1 to vm-2, we have to ping from this terminal like:

ping 192.168.0.2 -c 2

And we will get response like this:

Connection from VM-1 to VM-2
Connection from VM-1 to VM-2

We can see, no connection is established between these two VMs. Now we will try to connect these two VMs in the next step.

Step-4: Peering Two VPC:

In this step we will try to connect to two VPC that enables the connection of two VMs too by VPC Peering.

From the GCP dashboard, go to the VPC networking and select the VPC network peering.

Then click CREATE CINNECTION button.

Then click CONTINUE to the next step.

Peering vpc-1 to vpc-2:

Now we are trying crate connection from vpc-1 to the vpc-2.

Create Network Peering (vpc1 to vpc-2)
Create Network Peering (vpc1 to vpc-2)

We have created the peering but it is still inactive.

Network Peering List
Network Peering List

Peering vpc-2 to vpc-1:

Now we are trying crate connection from vpc-2 to the vpc-1.

Create Network Peering (vpc2 to vpc-1)
Create Network Peering (vpc2 to vpc-1)

Now we can see the two network peering are active.

Active Network Peering List
Active Network Peering List

Step-5: Check Connection :

Now if we try to connect from vm-1 to the vm-2 as we done before, it will be connected now.

VM connection established
VM connection established

Step-6: Create Nginx at One VM And Access From Others :

Here we will install nginx at vm-1 and will try to access from vm-2.

vm-1 terminal:

From vm-1 terminal, use the following command.

sudo apt update -y

To install nginx:

sudo apt install nginx

To check nginx service status:

systemctl status nginx
Nginix Service Status
Nginx Service Status

vm-2 terminal:

Now we will try to access from vm-2 terminal .

curl http://34.75.68.89
Nginx Server Response
Nginx Server Response

And Finally we have got the nginx server response from vm-1 to vm-2.

--

--

No responses yet