arrow

Advanced Configuration Of Backend Servers On SLB

1. Experiment

1.1 Knowledge points

This experiment primarily shows how SLB backend servers are organized. Backend servers can provide services separately or in a server-group manner; therefore, backend servers are applicable to more network scenarios and maximize the stability of external services.

1.2 Experiment process

  • Use a single SLB instance to implement high availability across zones
  • Use SLB VServer groups to implement traffic forwarding in different paths
  • Use SLB Master/slave server groups to implement the traditional Master/slave mode

1.3 Scene architecture diagram

image desc

1.4 Cloud resources required

  • SLB
  • ECS

1.5 Prerequisites

  • Understand ECS and SLB concepts
  • Be familiar with basic Linux operations

2. Start the experiment environment

Click Start Lab in the upper right corner of the page to start the experiment.

image desc.

After the experiment environment is successfully started, the system has deployed resources required by this experiment in the background, including the ECS instance, RDS instance, Server Load Balancer instance, and OSS bucket. An account consisting of the username and password for logging on to the Web console of Alibaba Cloud is also provided.

image desc

After the experiment environment is started and related resources are properly deployed, the experiment starts a countdown. You have two hours to perform experimental operations. After the countdown ends, the experiment stops, and related resources are released. During the experiment, pay attention to the remaining time and arrange your time wisely. Next, use the username and password provided by the system to log on to the Web console of Alibaba Cloud and view related resources:

openCole

Go to the logon page of Alibaba Cloud console.

image desc

Fill in the sub-user account and click Next.

image desc

Fill in the sub-user password and click Log on.

image desc

After you successfully log on to the console, the following page is displayed.

image desc

3. High availability of a single SLB instance

In order to provide more stable and reliable load balancing services, Alibaba Cloud’s Server Load Balancer (SLB) has deployed multi-availability zones in most regions so as to implement cross-data-center disaster tolerance within the same region. When the primary zone encounters failures or is unavailable, SLB has the ability, within a very short time period (about 30s), to switch to the secondary zone so as to restore services; once the primary zone is restored, SLB will automatically switch back to the primary zone to provide services.

3.1 Install the Nginx service

Go to the ECS console;

image desc

we can see that two ECS instances have already been created and that these instances respectively correspond to Zone A and Zone B in USA West 1.

image desc

Later in this experiment, for convenience sake, we will need to change the names of these two ECS instances. At this time we will change the name of the ECS instance in Zone A to “dog_ecs” and in Zone B to “cat_ecs”

image desc

Log on to dog_ecs. Please refer to login for detailed instructions.

The default account name and password of the ECS instance:

Account name: root

Password: nkYHG890..

Once we have logged on, run the following command:

apt update

image desc

Input the following command to install the Nginx service

apt install -y nginx

The presence of the following content indicates that the installation has completed successfully.

image desc

Input the following command to open the index.nginx-debian.html file:

vim /var/www/html/index.nginx-debian.html

Then in the position indicated in the following screenshot, add the “Zone A” tag to indicate that “dog_ecs”‘s server is located in Zone A.

image desc

Save and exit. The Nginx service has already been set up; Port 80 is enabled by default; wait for access.

Input dog_ecs’s IP in a browser and you’ll be able to see that the changes we just made have already been applied; this indicates that the service has been successfully set up.

image desc

Next log on to cat_ecs and perform the same operations as we did on dog_ecs in the previous step. Note that when modifying the content of the index.nginx-debian.html file, we should add the “Zone B” tag to indicate that cat_ecs’s server is located in Zone B.

image desc

Input cat_ecs’s IP in a browser, as shown in the following screenshot.

image desc

3.2 Add backend servers

As shown in the following screenshot, select Server Load Balancer

image desc

Click Add backend Servers. The two created ECS instances are displayed.

image desc

Refer to the figure below,Click Next.

image desc

Click Add.

image desc

image desc

You can see that the addition was successful.

image desc

3.3 Create an SLB listener

Refer to the figure below,Click Listener.

image desc

Click Next.

image desc

image desc

image desc

Click Submit.

image desc

Click OK.

image desc

Follow the steps shown in the following screenshot to get the SLB instance’s IP。

image desc

Copy the IP into a browser。

image desc

<font color='red'>The user can cut off the above result picture when doing the experiment and send it to the teacher, indicating that the part of the current chapter has been completed.</font>

Refresh several times and you will be able to see that the zone shown in the page will continuously switch back and forth between Zone A and Zone B. This allows Zone B to provide services even if Zone A encounters failures or is unavailable.

If you have extremely high availability requirements, the SLB instances’ availability mechanisms may not meet your requirements. For example, if SLB instances are unavailable due to factors such as network attacks or incorrect configuration, switching between SLB instances will not be triggered because, in these cases, no zone-level failures were encountered. In these cases, you can create multiple SLB instances to schedule access using the cloud resolution DNS or implement cross-region disaster tolerance using the global load balancing solution (Related steps are not demonstrated in this experiment)

4. SLB’s path-based traffic forwarding

4.1 Modify dog_ecs’s Nginx configuration

Go back to the ECS console.

image desc

image desc

Remotely log on to dog_ecs’s server and input the following command to clear the file content. This file is nginx’s default configuration file; we need to modify this file’s content.

echo "" > /etc/nginx/sites-enabled/default

image desc

Input “vim /etc/nginx/sites-enabled/default” to open this configuration file and copy the following configuration content into it. Then save and exit.

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;
        server_name _;
        location /dog {
            root /var/www;
            index index.html;
        }
        location / {
                try_files $uri $uri/ =404;
        }
}

image desc

Input the following command to create a directory named “dog”

mkdir -p /var/www/dog

image desc

Input “vim /var/www/dog/index.html” to create a file named “index.html” under the newly-created directory and copy the following content into the file

<h1>hello,labex</h1>
<h1>this is dog Group</h1>

image desc

Execute the following command to restart the Nginx service. We need to restart the service to apply the changes we just made to the Nginx configuration file.

service nginx restart

image desc

4.2 Modify cat_ecs’s Nginx configuration

Remotely log on to cat_ecs’s server and input the following command to clear the file content. This file is nginx’s default configuration file; we need to modify this file’s content.

echo "" > /etc/nginx/sites-enabled/default

image desc

Input “vim /etc/nginx/sites-enabled/default” to open this configuration file and copy the following configuration content into it. Then save and exit.

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;
        server_name _;
        location /cat {
            root /var/www;
            index index.html;
        }
        location / {
                try_files $uri $uri/ =404;
        }
}

image desc

Input the following command to create a directory named “cat”

mkdir -p /var/www/cat

image desc

Input “vim /var/www/cat/index.html” to create a file named “index.html” under the newly-created directory and copy the following content into the file

<h1>hello,labex</h1>
<h1>this is cat Group</h1>

image desc

Execute the following command to restart the Nginx service. We need to restart the service to apply the changes we just made to the Nginx configuration file.

service nginx restart

image desc

4.3 Create VServer groups

Referring to the figure below, create a group called dog and click Add.

image desc

image desc

Add an instance of dog_ecs to the group and click Next.

image desc

Fill in port 80 and click Add.

image desc

image desc

In the same way, create another group called cat and add the cat_ecs instance to the group.

image desc

4.4 Add forwarding rules

Click Set Forwarding Rule.

image desc

Refer to the figure below,

image desc

The rule creation is complete.

image desc

We arbitrarily name this the domain, for example, “ www.aaa.com “. When the “/dog” URL is accessed, the “dog” VServer group in the background will be used to provide services; when the “/cat” URL is accessed, the “cat” VServer group in the background will be used to provide services

4.5 Access tests

The “ www.aaa.com “ domain we named in the previous step is actually not registered; in this case how can a browser resolve the domain we have entered to our specified SLB instance?

In general a browser takes the three following steps to resolve a domain name:

  1. Checks the browser cache to determine whether or not a domain name has been resolved to an IP address. If it has, the browser will stop the resolution and directly send out a request
  2. If an IP address is not found in the browser cache, the browser will check the local cache file “hosts” on the operating system.
  3. If an IP address is not found in either the browser cache or on the operating system, the browser will send a request to the domain name server

Now we need to add the resolution of this domain name in the operating system cache file as mentioned in the second scenario above.

On Windows, this cache file is under “C:\Windows\System32\drivers\etc\hosts”; On Mac or Linux, this cache file is under “/etc/hosts” (An administrator account is required to modify this file).

To open this file, Mac and Linux users need to log on as root users and input “vim /etc/hosts” (users’ local Mac or Linux system instead of files on ECS).

Simply add the following content to the end of the cache file. Note that you should replace YOUR-SLB-IP with your own SLB address

YOUR-SLB-IP www.aaa.com

Next input the following link in a browser:

www.aaa.com/dog

image desc

Then input the following link in a browser:

www.aaa.com/cat

image desc

<font color='red'>The user can cut off the above result picture when doing the experiment and send it to the teacher, indicating that the part of the current chapter has been completed.</font>

Although the domain names are identical, we can distribute access traffic to the backend server based on the path after we have added path forwarding rules

5. Use of SLB Master/slave server groups

You can use Master/slave server groups to meet your traditional Master/slave needs, that is, a master instance and a slave instance on a backend server. When the master server works normally, the traffic will go through the master server. When the master server goes down, the traffic will go through the slave server to avoid service interruptions.

Both the Master/slave server groups and the VServer group are maintained in the listener dimension, that is, different listeners under the instance can forward the traffic to different server groups. A VServer group can include multiple ECS instances, while a Master/slave server group only allows two ECS instances where one is the master server and the other is the slave server.

5.1 Modify dog_ecs’s Nginx configuration

Remotely log on to dog_ecs and input the following command to create a directory named “labex”

mkdir -p /var/www/labex

image desc

Input “vim /var/www/labex/index.html” to create a file named “index.html” under the directory we just created. Copy the following content into the file. Then save the change and exit.

<h1>hello,labex</h1>
<h1>this is master Group</h1>

image desc

Input “vim /etc/nginx/sites-enabled/default” to open Nginx’s configuration file and copy the following content into it. Then save and exit.

server {
        listen 81 default_server;
        listen [::]:81 default_server;
        root /var/www/labex;
        index index.html index.htm index.nginx-debian.html;
        server_name _;
        location / {
                try_files $uri $uri/ =404;
        }
}

image desc

This newly-added configuration listens to Port 81 (to avoid conflicts with Port 80). When a request is made to access Port 81, the request will be sent to the index.html file we just created.

Execute the following command to restart the Nginx service. We need to restart the service to apply the changes we just made to the Nginx configuration file.

service nginx restart

image desc

5.2 Modify cat_ecs’s Nginx configuration

Remotely log on to cat_ecs and input the following command to create a directory named “labex”

mkdir -p /var/www/labex

image desc

Input “vim /var/www/labex/index.html” to create a file named “index.html” under the directory we just created. Copy the following content into the file. Then save the change and exit.

<h1>hello,labex</h1>
<h1>this is slave Group</h1>

image desc

Input “vim /etc/nginx/sites-enabled/default” to open Nginx’s configuration file and copy the following content into it. Then save and exit.

server {
        listen 81 default_server;
        listen [::]:81 default_server;
        root /var/www/labex;
        index index.html index.htm index.nginx-debian.html;
        server_name _;
        location / {
                try_files $uri $uri/ =404;
        }
}

image desc

Execute the following command to restart the Nginx service.

service nginx restart

image desc

5.3 Add a Master/slave server group

As shown in the following figure.

image desc

image desc

Add both ECS instances and click Next.

image desc

Click Add.

image desc

image desc

image desc

5.4 Create a new listener

Add a new Listener.

image desc

image desc

image desc

image desc

image desc

image desc

5.5 Test access

Input the following link in a browser. Note that you should replace YOUR-SLB-IP with your own SLB IP address.

http://YOUR-SLB-IP:81/

image desc

We can see that it is the master server (namely, the dog_ecs server) in the Master/slave group that is currently used to provide services. It is still the case even if you refresh the page multiple times.

Remotely log on to the server of dog_ecs again and input the following command to stop the Nginx service that was enabled in the previous step

service nginx stop

image desc

Refresh the page again in the browser and you can see that the slave server (namely, the cat_ecs server) in the Master/slave server group is now used to provide services. This is because the Nginx service on the master server has been interrupted and the slave server is then enabled.

image desc

<font color='red'>Users can cut off the above result picture when they are doing the experiment and send it to the teacher, indicating that the current experiment has been completed.</font>

Reminder:
Before you leave this lab, remember to log out your Alibaba RAM account before you click the ‘stop’ button of your lab. Otherwise you’ll encounter some issue when opening a new lab session in the same browser:

image desc image desc

6. Experiment summary

Alibaba Cloud’s SLB is a traffic distribution control service that distributes access traffic to multiple backend ECS servers (ECS instances) based on forwarding policies. Being one of the common services for modern network architectures, SLB expands application service capacity and improves the availability of applications. This experiment demonstrates some of Alibaba Cloud SLB’s different application scenarios by using three different methods to add backend servers. Users can use this experiment as the reference for the future use of SLB services.