Week9 - Selesfia/ComputerNetwork GitHub Wiki

Question: You need to create two vpc networks, i.e. myvpc1 and myvpc2. In myvpc1 (any zone), create two VMs with http server. In myvpc2, create DB (vm or cloud sql). make http servers connect to DB. Also add one load balancer. If a customer connect to LB, LB will dispatch the traffic to the backend http server.

Create VPC Networks

Create myvpc1

Execute the code at google cloud shell.

gcloud compute networks create myvpc1 --subnet-mode=custom
gcloud compute networks subnets create mysubnet1 --network=myvpc1 --region=asia-east1 --range=10.0.1.0/24

Create myvpc2

Execute the code at google cloud shell.

gcloud compute networks create myvpc2 --subnet-mode=custom
gcloud compute networks subnets create mysubnet2 --network=myvpc2 --region=asia-east1 --range=10.0.2.0/24

Create VMs for HTTP Servers

Create the First HTTP Server in myvpc1

Execute the code at google cloud shell.

gcloud compute instances create http-server-1 \
  --zone=asia-east1-a \
  --machine-type=e2-micro \
  --subnet=mysubnet1 \
  --tags=http-server \
  --image-family=ubuntu-2004-lts \
  --image-project=ubuntu-os-cloud

Create the Second HTTP Server in myvpc1

Execute the code at google cloud shell.

gcloud compute instances create http-server-2 \
  --zone=asia-east1-a \
  --machine-type=e2-micro \
  --subnet=mysubnet1 \
  --tags=http-server \
  --image-family=ubuntu-2004-lts \
  --image-project=ubuntu-os-cloud

Install an HTTP Server on Both VMs:

SSH into the db-vm and run:

sudo apt update
sudo apt install -y apache2
sudo systemctl start apache2

Before SSH into both VMs, make sure you have set the correct firewall rules allowing SSH access to your VMs. Go to "Configure Firewall Rules" Section to configure firewall rules.

Create Database VM in myvpc2

Create the database VM

Execute the code at google cloud shell.

gcloud compute instances create db-vm \
  --zone=asia-east1-a \
  --machine-type=e2-micro \
  --subnet=mysubnet2 \
  --tags=db-server \
  --image-family=ubuntu-2004-lts \
  --image-project=ubuntu-os-cloud

Install MySQL on the Database VM:

SSH into the db-vm and run:

sudo apt update
sudo apt install -y mysql-server
sudo systemctl start mysql

Before SSH into VM, make sure you have set the correct firewall rules allowing SSH access to your VM. Go to "Configure Firewall Rules" Section to configure firewall rules.

Configure MySQL

Allow Remote Connections by Editing the Config

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

Change bind-address to 0.0.0.0

Restart MySQL

sudo systemctl restart mysql

Create a Database and User

sudo mysql -u root 
CREATE DATABASE mydb;
CREATE USER 'myuser'@'%' IDENTIFIED BY 'myuser';
GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'%';
FLUSH PRIVILEGES;

Notes:

  1. sudo mysql -u root -> This will allow you to log in as the root MySQL user without needing a password, using the operating system's authentication. Once logged in, you can check and change user privileges as needed.
  2. To change root password in case you forget it
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
FLUSH PRIVILEGES;
  1. Exit mysql and try to log in with new password. mysql -u root -p

VPC Peering Connection

Create Peering Between myvpc1 and myvpc2

Execute the code at google cloud shell.

gcloud compute networks peerings create myvpc1-to-myvpc2 \
  --network=myvpc1 \
  --peer-network=myvpc2

gcloud compute networks peerings create myvpc2-to-myvpc1 \
  --network=myvpc2 \
  --peer-network=myvpc1

Configure Firewall Rules

Allow SSH and HTTP in myvpc1

Execute the code at google cloud shell.

gcloud compute firewall-rules create allow-ssh-http-vpc1 \
  --network=myvpc1 \
  --allow tcp:22,tcp:80 \
  --source-ranges=0.0.0.0/0 \
  --target-tags=http-server

Allow SSH and MySQL in myvpc2

Execute the code at google cloud shell.

gcloud compute firewall-rules create allow-ssh-mysql-vpc2 \
  --network=myvpc2 \
  --allow tcp:22,tcp:3306 \
  --source-ranges=10.0.1.0/24 \
  --target-tags=db-server

Connect HTTP Servers to the Database

Configure the HTTP Server VMs

Install MySQL Client

SSH to both VMs and run:

sudo apt install -y mysql-client

Connect to The Database

mysql -u myuser -h 10.0.2.2 -p mydb

Test the connection

Verify that the HTTP servers can connect to the database and execute SQL commands.

Create a Load Balancer

Set Up an Unmanaged Instance Group

Execute the code at google cloud shell.

gcloud compute instance-groups unmanaged create http-group \
  --zone=asia-east1-a
gcloud compute instance-groups unmanaged add-instances http-group \
  --instances=http-server-1,http-server-2 \
  --zone=asia-east1-a

Create a Backend Service

Create an HTTP Health Check
Execute the code at google cloud shell.

gcloud compute health-checks create http http-health-check \
  --port 80 \
  --request-path / \
  --check-interval 5s \
  --timeout 5s \
  --unhealthy-threshold 3 \
  --healthy-threshold 2 \
  --project=trim-mix-436602-e4

Execute the code at google cloud shell.

gcloud compute backend-services create my-backend-service \
  --protocol HTTP \
  --health-checks http-health-check \
  --port-name http \
  --global \
  --project=trim-mix-436602-e4

gcloud compute backend-services add-backend my-backend-service \
  --instance-group=http-group \
  --instance-group-zone=asia-east1-a \
  --global

Create a URL Map and HTTP Proxy

Execute the code at google cloud shell.

gcloud compute url-maps create web-map --default-service=my-backend-service
gcloud compute target-http-proxies create http-lb-proxy --url-map=web-map

Create a Forwarding Rule

Execute the code at google cloud shell.

gcloud compute forwarding-rules create http-content-rule \
  --global \
  --target-http-proxy=http-lb-proxy \
  --ports=80

Test Load Balancer

  1. Access the load balancer's external IP to see if it routes traffic to the HTTP servers.

Display the Content of Database VM

Create the Content of Your Database

SSH to your db VM and enter mysql

use mydb
create table addrbook(name varchar(50) not null, phone char(10));
insert into addrbook(name, phone) value ("Jessline", "0937711339");
select name, phone from addrbook;

Install PHP version 8.1 on Ubuntu

Install php ver 8.1 to both VMs.
You can go to this link for reference.

Connect to a MySQL database

  1. sudo vim /var/www/html/webdb.php
    And paste the following code. Remember to change to your servername, username, password, and dbname.
<?php
$servername="[10.0.2.2]";
$username="myuser";
$password="myuser";
$dbname="mydb";

$conn = new mysqli($servername, $username, $password, $dbname);

if($conn->connect_error){
    die("connection failed: " . $conn->connect_error);
}
else{
    echo "connect OK!" . "<br>";
}

$sql="select name,phone from addrbook";
$result=$conn->query($sql);

if($result->num_rows>0){
    while($row=$result->fetch_assoc()){
        echo "name: " . $row["name"] . "\tphone: " . $row["phone"] . "<br>";
    }
} else {
    echo "0 record";
}
?>
  1. Now try to open your browser using the VM external ip and add "/webdb.php".
  2. Please repeat this section to both VMs

Result

Now try to access the webdb through load balancer external ip.

PS : Remember to delete the resources that you created if you don't use it anymore.

05/11/2024

⚠️ **GitHub.com Fallback** ⚠️