Getting started with Google Compute Engine

Google Compute Engine
When we need to deploy applications we need servers and when we need to deploy applications in the cloud we need virtual servers.
- Rent virtual servers
- Virtual Machines- Virtual servers in GCP
- Google Compute Engine(GCE) - Provision and manage virtual machines.
Google compute engine - Features
| Compute Engine | Persistent Disk | Cloud Load Balancing |
|---|
- Setup VM instances as HTTP(Web) server.
- Distribute load with load balancer.
Creating our first VM in GCP
Create a virtual machine using the GCP Console
- In the Navigation menu (Navigation menu), click Compute Engine > VM instances.
- Click Create.
- On the Create an Instance page, for Name, type your VM name my-vm-1.
- For Region and Zone, select the region and zone .
- For Machine type, accept the default.
- For Boot disk, if the Image shown is not Debian GNU/Linux 10 (Buster), click Change and select Debian GNU/Linux 10 (Buster).
- Leave the defaults for Identity and API access unmodified.
- For Firewall, click Allow HTTP traffic.
- Leave all other defaults unmodified.
- To create and launch the VM, click Create.
Compute Engine Machine types and images
Compute engine machine family
- General purpose(E2,N2,N2D,N1): Best price-performance ratio
- Web and application servers, small-medium databases, dev environments.
- Memory optimized(M2,M1): Ultra high memory workloads
- Large in-memory databases and in-memory analytics.
- Compute Optimized(C2): Compute intensive workloads
- Gaming Applications
Compute Engine Machine Types
| Machine Name | vCPUs^1 | Memory(GB) | Max number of persistent disks(PDs)^2 | Max total PD size(TB) | Local SSD | Maximum egress bandwidth(Gpps)^3 |
|---|---|---|---|---|---|---|
| e2-standard-2 | 2 | 8 | 128 | 257 | no | 4 |
| e2-standard-4 | 4 | 16 | 128 | 257 | no | 8 |
| e2-standard-8 | 8 | 32 | 128 | 257 | no | 16 |
| e2-standard-16 | 16 | 64 | 128 | 257 | no | 16 |
| e2-standard-32 | 32 | 128 | 128 | 257 | no | 16 |
- variety of machine types are available for each machine family.
- Let's take an example: e2-standard-2:
- e2- machine type family
- standard- Type of workload
- 2- number of CPUs
Image
- Public images: Provided & maintained by google or open source communities or third party vendors.
- Custom Images: Created by you for your projects.
Some commands we can use inside our debian VM
sudo su
apt update
apt install apache2
ls /var/www/html
echo "Hello World!"
echo "Hello World!" > /var/www/html/index.html
echo $(hostname)
echo $(hostname -i)
echo "Hello World from $(hostname)"
echo "Hello World from $(hostname) $(hostname -i)"
echo "Hello world from $(hostname) $(hostname -i)" > /var/www/html/index.html
sudo service apache2 start
Installing HTTP webserver on GCE VM
sudo su
apt install apache2
Now if I click on external IP of the virual machine I can see the apache debian webpage
Now lets go to apache html dir
ls /var/www/html
# output- index.html
we can put our content inside our index.html file
echo "Hi I'm Tushar" > /var/www/html/index.html
Internal and External IPs
- However, two different corporate networks can have resources with same internal (private) IP address.
- (Remember) When you stop an VM instance, External IP address is lost.
Static IP addresses
- Go to Search bar and search External IP addresses
- Click on external ip address(Part of VPC)
- We can see our External IP address which is assigned to our VM.
- Click on reserve Static address
- Add a name for your static IP address.
- click on reserve.
- We can see- one static address with type- static, and in VM was ephameral.
- Check a button- change in VM row click on that, and attach IP address to VM.
- Make sure that you explicitly release an static IP when you're not using it.
Startup Script
Simplify VM HTTP server setup
- Startup script
- instance template
- custom image
Bootstrapping with startup script
#!/bin/bash
apt update
apt -y install apache2
echo "Hello world from $(hostname) $(hostname -I)" > /var/www/html/index.html
How to use startup script
Simplify VM creation with Instance templates
- How about creating an instance template?
- Define machine type, image, labels, startup script and other properties.
- Provides a convenient way to create similar instances.
- To make a change, copy an existing template and modify it.
- Latest non-depprecated version of the family is used.
- Go to instance template (under Compute engine)
- Click on create instance template
- Name it
- Add all configurations(region, zone, machine family, machine type etc.)
- Fire wall - allow http traffic
- Add startup script
- Create it
- No cost associated with instance template.
Reducing Launch time with a custom image
- Can be created from an instance, a persistent disk, a snapshot, another image, or a file in cloud storage.
- Can be shared across projects.
- (Recommendation) Deprecate old images($ specify replacement image)
- (Recomendation) Hardering an image- customize images to your corporate security standards.
always stop the instance and create image from it.
Troubleshooting launch of Apache web server on VM
ls /var/www/html
cat /var/www/html/index.html
sudo su
service apache2 start
Reducing costs - Compute engine Virtual Machines
| Feature | GCE |
| Sustained use discounts | Automatic discounts for using resources for long periods of time. Ex: if you use N1, N2, machine types for more than 25% of a month, you get a 20% to 50% discount on every incremental minute. |
| Committed use discounts | Reserve compute instances ahead of time, commit for 1 year or 3 years, Up to 70% discount based on machine types and base images. |
| Preemptible VMs | Cheaper, temporary instances for non critical workloads(Fixed pricing, Max 24 hrs, cheapest) |
Achieving High availability with live migration and automatic restart
- Your running instance is migrated to another host in the same zone.
- does not change any attributes or properties of the VM.
- Supported for instances with local SSDs.
- Not supported for GPUs and preemptible instances.
- On Host maintainance: What should happen during periodic infrastructure maintenance?
- Migrate(default): Migrate VM instance to other hardware/
- Terminate: Stop the VM instance
- Automatic restart- Restart VM Instance if they are terminated due to non-user-initiated reasons(maintenance event, hardware failure etc)
VMs - Best practices
- Cost, regulations, availability needs, latency and specific hardware needs.
- distribute instances in multiple zones and regions for high availability.
- Play with them to find out the right machine type.
- Use GPUs for Math and graphical intensive applications.
Compute Engine Scenarios
| Scenario | Solution |
|---|---|
| What are the pre-requisite to be able to create a VM instance? |
|
| You want dedicated hardware for your compliance, licensing, and management needs | Sole-tenant nodes |
| I've 1000s of VM and I want to automate OS patch management, OS inventory management and OS configuration management(manage software installed) | Use VM Manager |
| You want to login to your VM instance to install software | SSH into it |
| Don't want to expose a VM to internet | Don't assign an external IP address |
| You want to allow http traffic to your VM | Configure firewall rules |





