📜 ⬆️ ⬇️

It was in the evening. An autoscale load-balanced web service using the example of Bitrix in the Google Cloud Platform

Immediately it is worth making a reservation that this article was written more likely not to show the possibility of the work of this product on the Google Cloud Platform (GCP), it will work without it. Bitrix was taken for experiences simply as a popular platform. He himself is able to build pools, nodes, etc. in his “web environment”, though with his own rakes. And that is why even Debian machines were taken for tests, and not the CentOS beloved by all.

In fact, the material is applicable to many web projects. More precisely, this is a simple guide to building fault-tolerant and distributed applications based on Google Compute Engine virtual machines, Google Cloud SQL databases and Google load balancer.

Dear Bitrix experts and professionals, there are a lot of options for implementing this solution, only one is given here. You can consider virtual machines, containers, and Google App Engine as a cloud platform. Plus to all will be the ability to connect storage Google Storage, which was included in the engine for quite some time. In general, I will be happy to discuss the possibility of sharing Bitrix pilot on the GCP, perhaps it will be your experience that will be described next time as applicable specifically to Bitrix.
')

Let's get started


What is Bitrix, we will not explain for a long time. This is a professional web project management system and a huge number of corporate sites, online stores, portals and communities work on it.

Despite the volume of text with pictures, the whole deployment procedure took me about two hours, taking into account the caught grabelek.

Start by creating a database


Menu → Cloud SQL → Create Instance

Choose MySQL, “Next”, “Create a second generation database”.

Specify the database name, location, zone, machine size, disk type, storage size, backup schedule, root password, timeout for those. Service, add network. I chose a 1vCPU machine, 1.7Gb RAM and 200GB of normal HDD storage for the test, then we can change it. When you create a database, pay attention to the zone, it will be located virtual machine.

The allowed test network can be set to 0.0.0.0/0 (all) and connected directly. But we will do the right thing and do it humanly, we will not indicate anything here, safety is more important.

image

Next we need to set up a service account. Immediately make a reservation for what it is needed. If you connect to the database directly by its IP address, you can skip this step. We will need an account for a secure connection through a proxy. Otherwise, the machine will be created with a default entry that does not have access to other services.

So, open the menu, go to IAM and Administration → Service Accounts → Create Service Account . Name and ID indicate your taste, choose the role of Cloud SQL → Client Cloud SQL . OK, done.

image

Deal with the base.

Create a virtual machine


Click on the “sandwich” (Menu) → Compute Engine → VM Instances → Create Instance .

Take the image of Debian 8 or whatever. We take to start a simple machine: 1 core, 3.5 GB of RAM.

We change the service account of the machine to the one we created in the previous step (to communicate with our Cloud SQL. We put the checkboxes “allow HTTP” and “enable HTTPS” depending on the planned protocol.

image

Next, we need to slightly automate our machine, or rather run a SQL proxy on it. To do this, expand the settings “Configure management settings, disk, network and SSH keys” and in the section Automation → Start Script we write:

sudo wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 sudo mv cloud_sql_proxy.linux.amd64 cloud_sql_proxy sudo chmod +x cloud_sql_proxy sudo ./cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:3306 & 

<INSTANCE_CONNECTION_NAME> - we take from the properties of our database, this is the “Name of the connection with the instance” . Proxy Connection Article

Why is that? In fact, only the last line is needed for the launch, all the rest is enough to be done once when setting up the machine. This is an example so as not to break into an entire article in pieces.

Next, an important point. Go to the "Disks" tab and uncheck "Delete boot disk when deleting an instance." Yes, we will delete our car after setting it up.

image

With the creation of the car finished, click "create" and wait.

When the creation is complete, we connect via SSH to our virtual machine and configure it. About the configuration of the machine itself under Bitrix, I will not write much, the materials are full and on Habré and on the Internet. There is even a ready-made script “Web environment”, but I did not use it for religious reasons. It was easier for me to start everything with my hands than to understand someone else’s sandwich. I will describe only what I did to run. And without any optimization and tuning, otherwise I could be overwhelmed with comments from the optimization gurus of PHP, Zend, NGINX and others, without offense.

To begin, update:

 sudo apt-get update sudo apt-get upgrade 

MySQL client:

 sudo apt-get install mysql-client 

Apache:

 sudo apt-get install apache2 

PHP:

 sudo apt-get install php5 libapache2-mod-php5 php5-mysql 

Restart Apache:

 sudo systemctl restart apache2 

Next, we will need a Cloud SQL proxy for a “straightforward” database connection. We wrote the startup script when we created the machine, it works every time it starts. Check if it works:

 mysql -u root -p --host 127.0.0.1 

Enter the root password from the database, enjoy the line:

 mysql> 

If the first time does not work, then check the startup script of the machine and reboot it.

Maximize Bitrix


Normal wget download directly from the site:

 Wget https://www.1c-bitrix.ru/download/start_encode_php5.tar.gz 

The link is provided for example on the starting version. Next, unpack the files in the web server directory, I left the default.

With a machine like they finished, go to the list of virtual machines on Google Cloud and open our machine via HTTP (or HTTPS, if configured):

image

During the installation, we specify the MySQL server 127.0.0.1, the root login and password from our Cloud SQL database, and then go along the path we need in the wizard until the installation is completed. When everything is ready, we can check the performance of the configuration. What I got:

image

I, of course, drew attention to the performance of MySQL and at first was surprised. But, as it turned out later, this is quite adequate data, the base in reality gives out normal performance, it's just that it is still cloudy.

Everything works, move on to the next stage.

Create a scalable group of machines and put it behind the load balancer


I will do it in one region, in Europe. But to understand the service, it is still possible to spread it across regions (for example, if we have partners in Asia). The principle of operation is similar, only you will need to create two groups in two regions and a replica of the base there.

First you need to create a disk image. We delete our virtual machine. Yes, yes, we delete it. Here it is important to remember whether we removed the checkbox “Delete boot disk when deleting an instance” when creating it. If you have forgotten, then we clone (when the machine is opened, the button is on top) and remove it in the parameters. After removing the machine, we will have its disk available for creating an image.

Go to the section Compute Engine → Images , click "create image". When creating nothing tricky, we indicate our vacant disk and everything is ready.

image

Next, create a template for our future machines with Bitrix.

Go to Compute Engine → Instance Templates . We create a template by analogy with our virtual machine, but already from our image.

Specify:


 sudo ./cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:3306 & 

Again, this script was included when creating the machine, now only the last line is enough. The template is configured. Remember that we take <INSTANCE_CONNECTION_NAME> from the properties of our PaaS database.

Create a group of instances:

Open Compute Engine → Instance Groups → Create Instance Group .

Specify:


Created.

Load balancer


Click on the "sandwich" at the top left, Network → Load Balancing → Create Load Balancer .

HTTP / HTTPS load balancing -> Start configuration .

Specify:


Done, create. Work starts almost immediately, you can not wait.

After creating the balancer, open the tab “interface VMs” and see its external address. According to it, our Bitrix will work in the Google Cloud Platform. Go to the address, enjoy. We can write this address in DNS.

image

Finally, a couple of performance and scaling tests


First, what we have:


I configured the launch of additional instances of machines first with 100 simultaneous connections to show how it works. For combat decisions can use your own options. Testing was conducted during the trial period of GCP with a limit on the number of cores for all machines of only eight, therefore the eighth core was taken by the machine with which the test was directly launched.

First, we got the following test schedule:

image

It became clear that the machines do not have time to run. Therefore, their minimum number was increased to two, the number of connections per machine was reduced to 50, and the maximum load was reduced to 80%. The result was:

image

In the end, everything took off, automatic scaling worked as it should.

Of course, a lot of things can be added to our solution. For example, server tuning under Bitrix itself, copying and replication of the base, failover, etc. This topic is already for other materials. This article is written more likely not to show the possibility of Bitrix and other web and mobile projects on Google Cloud. This is a simple guide to building fault-tolerant and distributed applications based on virtual machines using Google load balancer.

To organize a distributed application, we just need to replicate the database to another region, create another group of virtual machines in another region, and turn on the machines in our global balancer. Google itself will choose the best route and direct users to the application in the nearest location.

A little later, another article will be published on how to run Bitrix on Google App Engine. I think it will be more interesting.

Finally, let's touch the cost of services:


Prices are naturally approximate, for a specific combat solution may require a powerful database, the distribution by region and so on. Everything is designed for location in a European data center without excesses.

In addition to the modest price for impressive resources, it is also pleasing that the Google Cloud Platform has finally become available in Russia for cashless payment to organizations.

Source: https://habr.com/ru/post/330664/


All Articles