Linux centos7: load balancing service

There are many load balancing services on the CentOS7 system, including LVS, HAProxy, nginx, etc. Their characteristics are as follows:

1. LVS

LVS (Linux Virtual Server) is a high-performance, high-stability load balancing tool on Linux systems that supports multiple load balancing algorithms, such as RR (Round-Robin), WRR (Weighted Round-Robin) and LCA (Least-Connection-Active). LVS can forward the request from the client to multiple real servers at the backend according to certain rules, so as to realize the load balancing of the application. The advantages of LVS are high performance, high reliability and high flexibility.

2. HAProxy:

HAProxy is a free, high-performance, and reliable load balancing software for web applications, API applications, and more. HAProxy supports a variety of load balancing algorithms, including RR, WRR, LC, etc., and also supports functions such as session maintenance and health check. In addition, HAProxy also provides advanced features such as load balancing control based on statistics. The advantages of HAProxy are high performance, high availability, rich functions and easy configuration.

3. nginx

Although nginx is a web server, it can also act as a reverse proxy server and load balancer. nginx supports a variety of load balancing algorithms, including RR, WRR, LC, etc. It also supports functions such as session maintenance, health check, and dynamic load balancing scheduling. In addition, nginx's modular design and asynchronous event-driven features also make it a high-performance and highly available load balancer.

In general, the load-balancing services on CentOS7 have the characteristics of high performance, high reliability, flexibility, and rich functions, which can meet the needs of different application scenarios.

To use LVS to achieve load balancing in CentOS7 , the following steps are required:

  1. Install LVS:

Execute the following command in terminal to install LVS :

yum install ipvsadm -y

  1. Configure LVS:

LVS is divided into three main components:

  • load balancer
  • Virtual server VIP (Virtual IP)
  • backend real server

Before setting up LVS , make sure that web server software (such as Apache , Nginx or others) is installed on all real servers .

The following is an example of a simple load balancer configuration:

  • Create a lvs.cf configuration file that will configure the LVS cluster's virtual services and backend real servers:

#Set up LVS load balancer

ipvsadm -C

#Add backend real server

ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.101:80 -m

ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.102:80 -m

ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.103:80 -m

#Add virtual service address

ipvsadm -A -t 192.168.1.100:80 -s rr

ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.101:80 -g

ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.102:80 -g

ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.103:80 -g

In the above example, paste the contents of lvs.cf into the command line. The first line resets the default configuration. Subsequent lines define the real server and virtual service for load balancing, respectively.

  • Run the lvs.cf file to apply the above configuration:

sh lvs.cf

  1. Test the LVS cluster:

After applying the LVS cluster configuration in the previous step , to test whether it is working properly, you can use the following command:

curl http://192.168.1.100/

If everything is normal, you should be able to see the home page of the web server. At this time, you can shut down one of the real servers and run the above command again, and you can see that the number of visits has begun to be distributed to another server.

These are the simple steps to achieve load balancing with LVS on CentOS7 . The exact implementation method may vary based on your specific needs and environment.

Using Nginx to implement load balancing on CentOS7 requires the following steps:

  1. Install Nginx:

Execute the following command in a terminal to install Nginx :

yum install nginx -y

  1. Configure Nginx:

Open the  /etc/nginx/nginx.conf  file and add the following:

http {

   upstream backend {

        server 192.168.0.101;

        server 192.168.0.102;

        server 192.168.0.103;

   }

   server {

       listen 80;

       location / {

           proxy_pass http://backend;

           proxy_set_header Host $host;

           proxy_set_header X-Real-IP $remote_addr;

           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

           proxy_set_header X-Forwarded-Proto $scheme;

       }

   }

}

This will create a  load balancing server group called backend , add the three backend server  IP addresses to it and configure Nginx to use this group for load balancing.

  1. Restart Nginx:

Execute the following command to restart the Nginx service and make the configuration take effect:

systemctl restart nginx

Now we can test Nginx 's load balancing function by accessing the IP address of the load balancing server.

In the above configuration, when a client requests an IP address on the Nginx load balancing server through a browser , it will use the Round-Robin algorithm to forward the request to one of 192.168.0.101 , 192.168.0.102 or 192.168.0.103 . Note that in the actual production environment, further configuration and optimization are required according to specific conditions.

Here are the steps to install HAProxy on CentOS7 :

  1. Update system dependencies

Update the system's dependent libraries with the following command:

yum update

  1. Install HAProxy

Install HAProxy with the following command :

yum install haproxy -y

  1. Configure HAProxy

Edit the HAProxy configuration file  /etc/haproxy/haproxy.cfg and modify the configuration there to suit your specific environment and needs. In this configuration file, you need to define backend servers and load balancing rules. HAProxy supports a variety of load balancing algorithms, such as round robin, weighted round robin, least connection, etc. Just choose the appropriate algorithm according to your actual needs.

For example, in the HAProxy configuration file, you can configure it like this:

frontend myapp

    bind *:80

    mode http

    default_backend myapp_servers

backend myapp_servers

    mode http

    balance roundrobin

    server server1 192.168.1.2:80 check

    server server2 192.168.1.3:80 check

The meaning of the above configuration is to forward HTTP requests from any IP address to one of the two servers ( 192.168.1.2 and 192.168.1.3 ), both of which have IP addresses of port 80 , and HAProxy will use a round robin algorithm to load balance these requests.

  1. Restart HAProxy

Once the configuration is complete, restart the HAProxy service with the following command:

systemctl restart haproxy

At this point, HAProxy will start monitoring the status of the backend servers and forwarding client requests to them.

These are the basic steps to install and configure HAProxy for load balancing on CentOS7 , actual usage may be different, depending on your specific needs.

The above are the characteristics and operation steps of the three commonly used load balancing services, you can refer to them.

Guess you like

Origin blog.csdn.net/weixin_63294004/article/details/130450528
Recommended