Hands-On Guide: Google Cloud VPC Network Peering to Connect Two VMs and Check Nginx Server Access.
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:
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.
New click the CREATE VPC NETWORK:
VPC-1:
Here the name this VPC is given vpc-1, you can have your own here.
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.
For this demo, choose all firewall rules under Firewall rules, but remember that in a production environment, a more selective approach is essential.
Now click the CREATE button to complete the creation of first 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.
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.
Now click the CREATE button to complete the creation of first VPC.
And now we can check we have tow VPCs (vpc-1, vpc-2)is ready to move next step.
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.
Now click the CREATE INSTANCE as marked.
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.
At the Firewall option, selected both (HTTP,HTTPS)
At the Advanced options, expand the Networking and
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.
Now click the CREATE button to complete the first VM creation.
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.
At the Firewall option, selected both (HTTP,HTTPS)
At the Advanced options, expand the Networking and
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.
Now click the CREATE button to complete the first VM creation.
Now we have two VM ready.
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.
After clicking the Authorize button, we can get access to our 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:
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.
We have created the peering but it is still inactive.
Peering vpc-2 to vpc-1:
Now we are trying crate connection from vpc-2 to the vpc-1.
Now we can see the two network peering are active.
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.
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
vm-2 terminal:
Now we will try to access from vm-2 terminal .
curl http://34.75.68.89
And Finally we have got the nginx server response from vm-1 to vm-2.