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.
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
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
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
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
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.
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
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.
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
Change bind-address
to 0.0.0.0
sudo systemctl restart mysql
sudo mysql -u root
CREATE DATABASE mydb;
CREATE USER 'myuser'@'%' IDENTIFIED BY 'myuser';
GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'%';
FLUSH PRIVILEGES;
Notes:
- 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.
- To change root password in case you forget it
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
FLUSH PRIVILEGES;
- Exit mysql and try to log in with new password.
mysql -u root -p
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
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
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
SSH to both VMs and run:
sudo apt install -y mysql-client
mysql -u myuser -h 10.0.2.2 -p mydb
Verify that the HTTP servers can connect to the database and execute SQL commands.
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 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
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
Execute the code at google cloud shell.
gcloud compute forwarding-rules create http-content-rule \
--global \
--target-http-proxy=http-lb-proxy \
--ports=80
- Access the load balancer's external IP to see if it routes traffic to the HTTP servers.
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 ver 8.1 to both VMs.
You can go to this link for reference.
-
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";
}
?>
- Now try to open your browser using the VM external ip and add "/webdb.php".
- Please repeat this section to both VMs
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