Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Google Cloud Platform Cookbook

You're reading from   Google Cloud Platform Cookbook Implement, deploy, maintain, and migrate applications on Google Cloud Platform

Arrow left icon
Product type Paperback
Published in Apr 2018
Publisher Packt
ISBN-13 9781788291996
Length 280 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Legorie Rajan PS Legorie Rajan PS
Author Profile Icon Legorie Rajan PS
Legorie Rajan PS
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Compute 2. Storage and Databases FREE CHAPTER 3. Networking 4. Security 5. Machine Learning and Big Data 6. Management Tools 7. Best Practices 8. Other Books You May Enjoy

Hosting a highly scalable application on Google Compute Engine

There are a number of ways to host a highly scalable application on GCP using Compute Engine, App Engine, and Container Engine. We'll look at a simple PHP and MySQL application hosted on GCE with Cloud SQL and see how the GCP ecosystem helps us in building it in a scalable manner.

First, we'll create a Cloud SQL instance, which will be used by the application servers. The application servers should be designed to be replicated at will depending on any events, such as CPU usage, high utilization, and so on.

So, we'll create an instance template which is a definition of how GCP should create a new application server when it is needed. We feed in the start up script that prepares the instance to our requirements.

Then, we create an instance group which is a group of identical instances defined by the instance template. The instance group also monitors the health of the instances to make sure they maintain the defined number of servers. It automatically identifies unhealthy instances and recreates them as defined by the template.

Later, we create an HTTP(S) load balancer to serve traffic to the instance group we have created. With the load balancer in place, we now have two instances serving traffic to the users under a single endpoint provided by the load balancer. Finally, to handle any unexpected load, we'll use the autoscaling feature of the instance group.

Getting ready

The following are the initial setup verification steps to be taken before the recipe can be executed:

  1. Create or select a GCP project
  2. Enable billing and enable the default APIs (some APIs such as BigQuery, storage, monitoring, and a few others are enabled automatically)
  3. Enable the Google Cloud SQL API
  4. Verify that Google Cloud SDK is installed on your development machine
  5. Verify that the default project is set properly

How to do it...

The implementation approach would be to first create the backend service (the database), then the instance-related setup, and finally the load balancing setup:

  1. Let's first create a Cloud SQL instance. On the Google Console, navigate to the SQL menu item under Storage.
  2. Click on Create instance and select MySQL.
  3. Choose the recommended MySQL second generation and fill out the details:
The root password is set to a simple password for demonstration purposes.
  1. Note the IP address of the Cloud SQL instance that will be fed to the configuration file in the next step:
  1. Navigate to the /Chapter01/php-app/pdo folder. Edit the config.php file as follows:
$host       = "35.190.175.176" // IP Address of the Cloud SQL
$username   = "root";
$password   = ""; 
// Password which was given during the creation $dbname = "test"; $dsn = "mysql:host=$host;dbname=$dbname"; $options = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION );
  1. The PHP application code is now ready to be hosted and replicated into multiple machines. Commit the changes to the Source Repositories from where the start up scripts will pick the code.
  2. The startup-script.sh can be found in the Chapter01/php-app/ directory. The script installs the necessary software to run the PHP application, then it downloads the application code from Source Repositories and moves it to the /var/www/html folder and installs the components for logging. Do update the project ID and the repository name in the following script to point to your GCP repository:
#!/bin/bash
# Modified from https://github.com/GoogleCloudPlatform/
getting-started-php/blob/master/optional-compute-engine/gce/
startup-script.sh # [START all] set -e export HOME=/root # [START php] apt-get update apt-get install -y git apache2 php5 php5-mysql php5-dev php-pear
pkg-config mysql-client # Fetch the project ID from the Metadata server PROJECTID=$(curl -s "http://metadata.google.internal/computeMetadata/v1/project/
project-id" -H "Metadata-Flavor: Google") # Get the application source code git config --global credential.helper gcloud.sh git clone https://source.developers.google.com/p/<Project ID>/r/<Repository Name> /opt/src -b master #ln -s /opt/src/optional-compute-engine /opt/app cp /opt/src/Chapter01/php-app/pdo/* /var/www/html -r # [END php] systemctl restart apache2 iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT # [START project_config] # Fetch the application config file from the Metadata server and add it to the project #curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/project-config" \ # -H "Metadata-Flavor: Google" >> /opt/app/config/settings.yml # [END project_config] # [START logging] # Install Fluentd sudo curl -s "https://storage.googleapis.com/signals-agents/logging/google-fluentd-install.sh" | bash # Start Fluentd service google-fluentd restart & # [END logging] # [END all]
  1. Do make sure the firewall-rules are updated to allow traffic for ports 80 and 3306. The instances are tagged http-server, so include them in the target-tags attribute.
  2. We'll create an instance group for a group of the same PHP application servers. Create an instance template as follows:
$ gcloud compute instance-templates create my-php-tmpl \      
--machine-type=g1-small \       
--scopes logging-write,storage-ro,
https://www.googleapis.com/auth/projecthosting \ --metadata-from-file startup-script=./startup-script.sh \ --image-family=debian-8 \ --image-project=debian-cloud \ --tags http-server

The following screenshot shows the output for the preceding command:

Create the instance group as follows:

$ gcloud compute instance-groups managed create my-php-group \
--base-instance-name my-php-app \
--size 2 \
--template my-php-tmpl \
--zone us-east1-c

The following screenshot shows the output for the preceding command:

We'll create a health check that will poll the instance at specified intervals to verify that they can continue to serve traffic:

gcloud compute http-health-checks create php-health-check --request-path /public/index.php

The following screenshot shows the output for the preceding command:

  1. Now, we have two instances running in our instance group, my-php-group. We'll bring them under a load balancer to serve traffic.
  2. Head over to the Load balancing submenu and let's create a new HTTP(S) load balancer by navigating to Networking | Network Services | Load balancing:
  1. For the Backend configuration, we'll have to create a backend service which will point to the instance group and the health check that we have already created:
  1. For the Host and path rules and Frontend configuration, we'll leave the default settings.
  2. Once the settings are completed, an example review screen is shown as follows:
  1. Go ahead and create the HTTP(S) load balancer—an external IP address is created to address the load balancer. After some time, once the instances are identified as healthy, the load balancer will serve traffic to our instances under the group:
  1. In cases where traffic cannot be handled by the fixed number of instances under a load balancer, GCP provides a Compute Engine autoscaler. For scalability based on certain criteria, we can configure autoscaling at the instance group level. Instances can be scaled depending on CPU usage, HTTP load balancing usage, monitoring metrics and a combination of these factors:

How it works...

When the user hits the endpoint URL of the load balancer, it transfers the request to one of the available instances under its control. A load balancer constantly checks for the health of the instance under its supervision. The URL to test for the health is set up using the Google Compute's health check.

The PHP applications running on both the instances are configured to use the same Cloud SQL database. So, irrespective of the request hitting Instance 1 or Instance 2, the data is dealt from the common Cloud SQL database. 

Also, the Autoscaler is turned on in the Instance Group governing the two instances. If there is an increase in usage (CPU in our example), the Autoscaler will spawn a new instance to handle the increase in traffic:

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £16.99/month. Cancel anytime