Google Cloud Fundamentals Part 1
Tags: gcp, googlecloud
Published: 2022-03-29 16:49:26

**_Regions_**
There are specific geographical locations to host one's resources and it is called Region. Google has almost 20+ regions. Usages: Availability, Low latency, global footprint, adherence to government.

**_Zones/Discrete Cluster_**
Each region has more than one zones. Generally 3 zones. For example: us-west1 regions has zone us-west1-a,us-west1-b,us-west1-c.

**_Google Compute Engine_**
Enables to create and manage Virtual Machine. Gives one to load balance and auto-scale multiple virtual machine instances, attach storage and network storage, manage network connectivity and configuration. 
Create a virtual machine instance( Hands-on):
1. Go o Google Console and search for "Compute Engine". Press Create
2. Give it a name and add labels. For example, the "environment" key can have "dev"  value, "business" key with "sales" value etc. with a save.
3. Choose a region and zone. 
4. From the Machine Configuration, choose Machine Family based on your need. For example, choose 'General purpose' and keep others as default.
5. From the boot disk, select the image/operating system you want to use. 
6. Under the Firewall, we are going to select Allow HTTP Traffic to allow a person from outside with http website can get into our VM.
Then press "Create".
Now go to VM Instances and you can find your instance. You can see the external IP address which we will use to talk to the machine.

Note: You can also press 'SSH' to work with the VM instance through the terminal. Now we are going to install a http webserver on GCP VM.

`sudo su` to become the root user. To update all package managers `apt update`. Now to install apache, `apt install apache2`.
Now go to see the http server, go to the external IP and see the page

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sgjc2wfx4b81jw5333yt.png)
As the default root is here `/var/www/html`, we are going to use it 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zlgulidvnwc8of8znl6w.png)
thus use `ls /var/www/html`. Now going to edit "Hello world" in the index.html file
`echo "Hello World!" > /var/www/html/index.html`
Now go to the External IP address and you may see 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tugov60prtceovv8uwkr.png)
Now to print the host name( Virtual machine name), we are going write `echo $(hostname)` and to get the Internal IP address, `echo $(hostname -i)`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xls5ni3rc410cis57zq2.png)
To pass all these information to our index page, we can use `echo "Hello world from $(hostname) $(hostname -i)" > /var/www/html/index.html`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/orce3gifi8861olzbjvt.png)

And we can see the output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxro4q7eykttv3gjrm5c.png)

**_Basics of VM IP address_**
Internal IP address does not change and unique for a VM
External IP address changes when someone stop the VM. External users use the VM using External IP address. 
Static IP address is Permanent External IP address that can be attached to a VM. To create a static IP address, go to VPC Network and then External IP address and press Reserve Static Address to make your selected External IP address as a static one. Or manually search for External Ip address from the search bar and then from the dropdown, select "Static" 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ln9vc6truww8cdkhet7e.png)
You can then give it a name

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xivy53d0yy13i17vqzs.png)
As it is a premium thing, you need not to use static one if you are not using it. Just go to external IP address under VPC Network and press "Change" and Attach IP address to None.


**_Simplify VM HTTP Server setup_**
1. Using startup script:
Go to VM Instances and  press create. Give it a name and go to "Management, security, disk,..." and go to Startup script. 
add this code 

```
#!/bin/bash
apt update 
apt -y install apache2
echo "Hello world from $(hostname) $(hostname -I)" > /var/www/html/index.html
```
and press Create
2. Instance Template:
Rather than creating instances manually every time, we can create instance templates.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9nqt9n58ikypcp8oxybr.png)
Now, under the Compute Engine, go to Instance Templates. Create a new. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5nczmnu8wzevjj8okhb.png)
add the default disk and others and again add Startup Script like the previous example with the same code. 

Note: We need to pay if we create a VM using the Instance Template. 
3. Use a custom image

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pp33tscetvjbdscjmhih.png)
Under the Compute engine, go to storage and choose "Disks"(Hard disk associated with your virtual machine). Assume that we have a VM already created and thus a Disk already there.  
Now go to "Actions" and go to "Create Image" 
Note: You should not create an image from a disk that is currently attached to a running instance. You should stop the instance and then create an image from that disk. 
Give the image a name and press "Create".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i71174fsy0v30p3eun02.png)
Now you will find a disk and you can create an instance from your custom disk.
You can now create instance templates using your custom disk. While creating instance templates, you need to choose your disk inn the "Boot disk" section. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vv7z2kwxrisf5xlymcfj.png)
**_Use discounts_**

1. Sustainable
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndo014qoqzpbj7xewdxj.png)

2. Committed use discount

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rv5v8xialw8gy3i10yqo.png)

3. Preemptible VM

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qtfjv97fb4pmu7rr0g52.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1m0v0m79cti5o91huczu.png)

**_Sole Tenant Nodes_**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4iv0s3xkjp9iuabk5kun.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h15al3kzz9czkfxbgqyv.png)
Press 'Create a Node group'. Under the node template properties, create a node template and remember to select Affinity Labels. For example: key="group" and value="a". You can choose number of nodes as 1. Thus you can create one.
Note: Sole tenants are very expensive and we will refrain from making this. 
While creating a VM instance, go to Management,disk ,........
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gyh256t9e5iwtyku2q20.png)
Add the affinity label we created
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvvz3hqj3fxfxy48sc80.png)
So, what basically happens is that if we have a key and value created with this, the VM is getting created under that node.


**_Google Cloud Pricing Calculator_**
[Calculator](https://cloud.google.com/products/calculator/)