官术网_书友最值得收藏!

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:
主站蜘蛛池模板: 商丘市| 保山市| 定安县| 崇明县| 随州市| 咸阳市| 房山区| 定襄县| 常山县| 汪清县| 锡林郭勒盟| 渑池县| 博客| 嘉峪关市| 桑日县| 万山特区| 大理市| 浦城县| 石景山区| 甘孜县| 景德镇市| 景谷| 方城县| 称多县| 江阴市| 拉孜县| 肃宁县| 邮箱| 丹东市| 叙永县| 阳曲县| 樟树市| 平武县| 寿阳县| 河间市| 繁昌县| 永丰县| 峨眉山市| 扎囊县| 甘洛县| 琼海市|