Mar 17, 2018 - The default installation of Cpanel Server should automatically. It should configure iptables for you, If on CentOS 7, firewalld should be set for you. Note that with firewalld, you can't have a different firewall service like csf running, it will. GPU, Grafana, Graylog, GRUB, Hacking, Hacks, HAProxy, How To.
This article will help you to install HAProxy on CentOS, RHEL servers and will configure a Layer 4 Load Balancing (Transport Layer). Which will balance load and transfer requests to different-2 servers based on IP address and port numbers.
Step 1 – Install HAProxy
HAProxy package is available under the default yum repository for CentOS, Redhat systems. Use the following yum package manager command to install HAProxy on your system.
Step 2 – Configure HAProxy
Update your HAProxy configuration file /etc/haproxy/haproxy.cfg as per your requirement, You may also use below given configuration file as an example of setup and modify it.
[ Update red mark values as per your network setup ]
Change the ips in configuration file as per your network setup. In HTTP Site Configuration section if any request on ip 192.168.10.10 on port 80, this will be redirected to port 80 of 192.168.10.100 or 192.168.10.101 servers. Similarly in HTTPS Site Configuration if any request on ip 192.168.10.10 on port 443, this will be redirected to port 443 of 192.168.10.100 or 192.168.10.101 servers.
You also need to make a few changes in the configuration file as per your system configuration.
nbproc <value> # Number of processing cores in your system.mode <value> # ‘http’ for http site and ‘tcp’ for https sitebalance <value> # Type of load balancing like ‘source’, ’roundrobin’ etc.
Step 3 – Start HAProxy Service
Start HAProxy service using the following command, also configure it to auto-start on system boot.
And You have done it.
Congratulation’s! you have successfully configured HAProxy load balancer. Read next article to enable haproxy stats and setup ACL in HAProxy.
For more configuration details check below url. http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
On this page
- Basic Concept with HAProxy
HAProxy or High Availability Proxy is an open source TCP and HTTP load balancer and proxy server software. HAProxy has been written by Willy Tarreau in C, it supports SSL, compressions, keep-alive, custom log formats and header rewriting. HAProxy is a fast and lightweight proxy server and load balancer with a small memory footprint and low CPU usage. It is used by large sites like Github, StackOverflow, Reddit, Tumblr, Twitter and others. It has become the most popular software load balancer and proxy server in the past years.
In this tutorial, I will guide you trough the HAProxy installation and configuration for 3 servers, one load balancer, and two Nginx web servers. We will install HAProxy on a single server and then install Nginx web server on the other servers. HAProxy acts as a load balancer for the Nginx web servers.
Basic Concept with HAProxy
Layer 4 and Layer 7
HAProxy can run in two modes: TCP mode Layer 4 and HTTP Mode Layer 7. In Layer 4 TCP mode, HAProxy forwards the RAW TCP packets from the client to the application servers. In the Layer 7 HTTP mode, HAProxy is parsing the HTTP header before forwarding them to the application servers. In this tutorial, we will use Nginx as the web server that only supports the Layer 7 HTTP mode.
Balance Algorithm
This is the algorithm that is used by HAProxy to select the server when doing the load balancing. The following modes are available:
Roundrobin
This is the most simple balance algorithm. For each new connection, it will be handled by the next backend server. If the last backend server in the list is reached, it will start again from the top of backend list.
Lastconn
The new connection will be handled by the backend server with least amount of connections. This is useful when the time and load of the requests vary a lot.
Source
This is for sticky sessions, the client IP will be hashed to determine the backend server that received the last request from this IP. So an IP A will always be handled by backend1, and IP B will always be handled by banckend2 to not interrupt sessions
There are oOther algorithm - check the official HAProxy site for details.
Prerequisites
- 3 CentOS 7
loadbalancer
192.168.1.102
nginx1
192.168.1.104
nginx2
192.168.1.105
192.168.1.102
nginx1
192.168.1.104
nginx2
192.168.1.105
- Root privileges on all 3 servers.
Step 1 - Configure the /etc/hosts files
Log in to the load balancer server and edit the /etc/hosts file.
Add nginx1 and nginx2 hostnames:
Save the file and exit the editor.
Next, edit the hosts file on the Nginx servers (nginx1 and nginx2):
Edit and add a new line for the load balancer in the hosts files:
Add the loadbalancer hostname on each nginx server:
do this on nginx1 and nginx2 server.
Step 2 - Install and Configure HAProxy
HAProxy is available in the CentOS 7 repository, log in to the loadbalancer server and update the package lists:
Now install HAProxy with this yum command:
When the installation is finished, go to the '/etc/haproxy/' directory and backup the original configuration file:
Next, add a new HAProxy configuration file 'haproxy.cfg' file with the vi editor:
Paste the configuration below:
Save the configuration file and exit.
Next, configure rsyslog for HAProxy.
We will configure the rsyslog daemon to log the HAProxy statistics. Edit the rsyslog.conf file to enable the UDP port 514 to be used by rsyslog.
Uncomment this line to enable the UDP connection:
If you want to use a specific IP, you can add a new line like the one below:
Save the file and exit.
Then create new haproxy configuration file for rsyslog:
Paste configuration below:
Save and exit.
Now restart rsyslog and then start the haproxy:
Add haproxy to start at boot time:
Step 3 - Install and Configure Nginx
In this section, we will install Nginx from epel repository on nginx1 and nginx2 server.
Log in to the servers:
Install the epel repository with the yum command below:
Now you can install Nginx:
Nginx is installed. Go to the web directory and change the index file so that we can see which of the two servers delivered the html file:
Next, add Nginx to start at boot time and then start it:
Make sure you're doing this step on nginx1 and nginx2 server.
Step 4 - Testing
Testing from browser by accessing the loadbalancer IP: 192.168.1.102
Testing with curl command:
Testing to login to the HAProxy web monitoring that is running on port 8080 with username and password 'howtoforge':
http://192.168.1.102:8080/stats
HAProxy is working successfully and acts as a load balancer for our two Nginx web servers.
Conclusion
HAProxy or High Availability proxy is an open source software that provides high availability for TCP-based services, it operates as HTTP load balancer and proxy server. The software is written in C and supports SSL, keep-alive and compression. HAProxy is the right choice for everyone who needs a load balancer and proxy server that is fast and lightweight with a small memory footprint and low CPU usage. Haproxy can run in Layer 4 TCP mode and Layer 7 HTTP mode. Nginx supports only the Layer 7 HTTP mode with HAProxy. If you want to use Layer 4 TCP mode, you can use other web servers like apache. On CentOS 7, HAProxy is available in the default repository. It's easy to install and configure.