DataSunrise is sponsoring AWS re:Invent 2024 in Las Vegas, please visit us in DataSunrise's booth #2158

Load Balancing with HAProxy

Load Balancing with HAProxy

HAProxy is a free software tool that functions as a load balancer and reverse-proxy server for TCP and HTTP-based applications. The tool distributes connection requests across multiple server nodes. With very little resource usage, it enables you to handle huge volumes of HTTP and HTTPS traffic. Moreover, HAProxy performs server health checks and forwards users to functioning node in case one of the servers is down, which makes it a helpful failover solution.

Assuming forementioned benefits, we have configured HAProxy to distribute connections for MySQL RDBMS and our user interface.

Within this article, we will consider steps to perform load balancing for three DataSunrise proxies in High Availability mode at the following addresses: 192.168.1.100, 192.168.1.101, 192.168.1.102.

Prerequisites

There are several steps to perform before starting to work with HAProxy.

Before configuring load balancing, you need to install DataSunrise in high availability mode on several servers. To implement High Availability mode on DataSunrise, perform the following:

  1. DataSunrise installation wizard allows you to select the local or remote location for a configuration tab. Select Remote.
  2. At the DataSunrise Server Details tab, specify the details of the current DataSunrise instance: server name, hostname where DataSunrise instance is installed, port number of the web UI of the instance (11000, by default).
  3. At the Dictionary location tab, specify the database to store DataSunrise configuration (Dictionary). All servers configured to use this database will share common configuration settings (including common credentials to access web UIs).

Refer to DataSunrise Administration Guide for more detailed instructions on high availability mode of DataSunrise. Also ensure the same version of product is installed on all servers.

Create Instance

Next, we need to create a DataSunrise instance on the virtual host where MySQL is installed and open proxies on machines that will connect to the MySQL database.

To create an instance, perform the following on the master machine:

  1. Enter DataSunrise GUI and go to the Databases subsection of the Configurations section.
  2. Click the Database + button.
  3. Enter the hostname, port number and other required information.
  4. Once the instance is created, select newly created instance from the list and click Edit.
  5. A window with instance configurations will open, click the Proxy + button.
  6. Specify the server, hostname and port number (mysql0, 192.168.1.100, 3306 correspondingly) and click Save.
  7. Open proxies on slave machines at the same instance specifying corresponding server name and hostname for each proxy.

At the end, the instance configuration will look like this:

HAProxy

Configuring HAProxy for MySQL

Configuring HAProxy for database management systems is pretty simple. Below we will configure HAProxy to function as a connection distributor for DataSunrise proxies (192.168.1.100:3306, 192.101.1.101:3306, 192.168.1.102:3306).

After installing HAProxy open the mentioned above /etc/haproxy/haproxy.cfg file and edit the ‘listen’ configuration block as follows:

listen af_mysql_balancer         mode tcp         bind *:3306         balance leastconn         server mysql0 192.168.1.100:3306 check         server mysql1 192.168.1.101:3306 check         server mysql2 192.168.1.102:3306 check

HAProxy will perform health checks and distribute connections according to the number of connections on the server node, forwarding the user to the server with the least number of connections.

balance <algorithm> is for selecting balancing algorithms.

roundrobinRound Robin is the default algorithm, it selects servers in turns.
leastconnWith this algorithm, HAProxy directs each new user to the server with the least number of connections.
sourceThe algorithm to direct users to servers based on a hash of the user’s IP address.

For the complete list of balancing algorithms refer to HAProxy Configuration Manual.

In case the server currently used by a user is down, the connection will be closed. The user will have to reconnect and HAProxy will open a new database connection to the server that is up and has the least amount of connections.


HAProxy

All the other database management systems can be configured the same way, only ports need to be changed.

Configuring HAProxy as a load balancer for DataSunrise backend service (GUI)

In this section, we will consider steps to perform load balancing for three DataSunrise proxies in High Availability mode at the following addresses: 192.168.1.100, 192.168.1.101, 192.168.1.102.

Install HAProxy on a separate server and open /etc/haproxy/haproxy.cfg with any text editor. Specify the listener configuration in the following way:

listen af_gui_balancer         timeout client 50000         timeout server 50000         mode http         bind *:11000 ssl crt /home/anon/proxy.pem         redirect scheme https if !{ ssl_fc }         cookie HA_BACKEND_ID insert         balance leastconn         server node0 192.168.1.100:11000 ssl verify none check cookie 0         server node1 192.168.1.101:11000 ssl verify none check cookie 1         server node2 192.168.1.102:11000 ssl verify none check cookie 2

Configuration Breakdown

User Inactivity Time

timeout client <timeout> – set the maximum user inactivity time period after which the connection will be closed (milliseconds).
timeout server <timeout> – set the maximum server inactivity time period after which the connection will be closed (milliseconds).

Instance Protocol

mode { tcp|http } – defines the protocol of the instance. In our case, the balancing will be performed at the HTTP protocol level. The client request will be analyzed in depth before connecting to any server. Any request which is not RFC-compliant will be rejected. Layer 7 filtering, processing and switching will be possible.
HAProxy can balance at TCP level as well, but specifics of web applications having a user session condition makes it hard to perform proper load balancing with no opportunity to analyze or modify certain parts of HTTP queries.  We use http mode because we need HAProxy to assign and read persistence cookie that will enable HAProxy to determine the server which the user must be directed to. Thus, the web-server will correctly operate with the user session.

Listening Address and Port

bind [<address>]:<port_range> – bind option defines one or several listening addresses and ports in a frontend. <address> can be a host name, IPv4 address, IPv6 address or ‘*’. ‘*’ means that the port will be open in all available IP-addresses.
ssl crt /home/anon/proxy.pem – The ssl option enables SSL deciphering which requires certificates. Certificates and keys will be taken from the /home/anon/proxy.pem file. System users must have read-access to the file. The pem file content should be in the following format:

-----BEGIN CERTIFICATE-----
HLDXjCDSAkY...
-----END CERTIFICATE-----
-----BEGIN RSA PRIVATE KEY-----
HLDEpgIBDSKC....
-----END RSA PRIVATE KEY-----

Forwarding

redirect scheme https if !{ ssl_fc } the following configuration forwards all plain text HTTP connections on port 11000 to HTTPS scheme. Thus, users will not be able to use the administration service without encryption.

Persistent Cookie

cookie HA_BACKEND_ID insert. The given option defines the persistent cookie. After the first request, HAProxy sends cookie to the user. According to the cookie value HAProxy will determine which server node must be used for subsequent queries of the user.
HA_BACKEND_ID is the cookie name.
The insert option defines that HAProxy will assign those cookie values after the first query. The persistent cookie value is used until the user clears browser cookies or ends the session.

The last three lines define instances.

node0 is an arbitrary name of the instance used only for identification in terms of HAProxy.
192.168.1.100:11000 – the address and port of the instance.
ssl – defines that the instance accepts https connections.
verify [none | optional | required]. By default, the setting is set to none, which means that the SSL certificate sent by server will not be verified, i.e. HAProxy will trust the certificate of DataSunrise backend service.
The check option enables the health checking mechanism. Before forwarding the client query to the node, HAProxy checks whether the node is available.
cookie <value> – the parameter sets the cookie value assigned to the server.

Save the configuration changes and relaunch HAProxy.

Conclusion

As a result, we will have the configuration enabling HAProxy to function as follows:

  • A user connects to HAProxy.
  • HAProxy performs health check of the servers.
  • HAProxy forwards the user to the server node with the least load, which actually balances the load.

HAProxy operates on Linux and Solaris. For Windows operating system try more advanced Nginx solution.

If you need any assistance while configuring HAProxy or DataSunrise in high availability mode, please feel free to contact us. Our support engineers will help you.

Next

IoT Data Security: Challenges and Solutions Explored

IoT Data Security: Challenges and Solutions Explored

Learn More

Need Our Support Team Help?

Our experts will be glad to answer your questions.

General information:
[email protected]
Customer Service and Technical Support:
support.datasunrise.com
Partnership and Alliance Inquiries:
[email protected]