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.
The following are the initial setup verification steps to be taken before the recipe can be executed:
- Create or select a GCP project
- Enable billing and enable the default APIs (some APIs such as BigQuery, storage, monitoring, and a few others are enabled automatically)
- Enable the Google Cloud SQL API
- Verify that Google Cloud SDK is installed on your development machine
- Verify that the default project is set properly
The implementation approach would be to first create the backend service (the database), then the instance-related setup, and finally the load balancing setup:
- Note the
IP address
of the Cloud SQL instance that will be fed to the configuration file in the next step:
- Navigate to the
/Chapter01/php-app/pdo
folder. Edit theconfig.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 );
- 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.
- The
startup-script.sh
can be found in theChapter01/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]
$ 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:
- 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:
- For the
Host and path rules
andFrontend configuration
, we'll leave the default settings. - Once the settings are completed, an example review screen is shown as follows:
- 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:
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: