The implementation approach would be to first create the backend service (the database), then the instance-related setup, and finally the load balancing setup:
- Let's first create a Cloud SQL instance. On the Google Console, navigate to the SQL menu item under Storage.
- Click on Create instance and select MySQL.
- Choose the recommended MySQL second generation and fill out the details:
The root password is set to a simple password for demonstration purposes.
- 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 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
);
- 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 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]
- 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.
- 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:
- Now, we have two instances running in our instance group, my-php-group. We'll bring them under a load balancer to serve traffic.
- 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:
- 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 and Frontend configuration, we'll leave the default settings.
- Once the settings are completed, an example review screen is shown as follows:
- 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:
- 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: