• Categories
    Category
  • Categories
    Category
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial Comments FAQ Related Articles

How to Configure HAproxy Load Balancer with Keepalived in CentOS

813

To configure HAproxy with Keepalived in CentOS

HAproxy is a high available load balancer that performs load balancing for HTTP and other TCP-based services. It can be done by distributing load across real servers. The HAproxy is configured with keepalived to set-up load balance with two or more nodes. This article explains the configuration of HAproxy with Keepalived in CentOS.


Configuration of HAproxy with Keepalived

Two load balancer and backend webservers are required to configure HAproxy. In addition, use two load balancers for same virtual IP.

Testing Environment

Load Balancer-1:

Hostname - ha1.example.com
IP Address &ndash 192.168.5.157

Load Balancer-2:

Hostname - ha2.example.com
IP Address &ndash 192.168.5.158

Web Server-1:

Hostname - ws1.example.com
IP Address &ndash 192.168.5.159

Web Server-2:

Hostname - ws2.example.com
IP Address &ndash 192.168.5.160

Install Apache web-server packages on both the web-servers. If you don' t have DNS server, just add the entry in /etc/hosts file for Host-name resolution.


Installation of HAproxy

Utilize the following command to install the HAproxy package.

[root@ha2 ~]# yum install haproxy -y
Loaded plugins: fastestmirror, langpacks
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base                                                                                                                                                                  | 3.6 kB  00:00:00     
extras                                                                                                                                                                | 3.4 kB  00:00:00     
updates                                                                                                                                                               | 3.4 kB  00:00:00     
(1/2): extras/7/x86_64/primary_db                                                                                                                                     | 166 kB  00:00:00     
(2/2): updates/7/x86_64/primary_db                                                                                                                                    | 9.1 MB  00:02:01     
Determining fastest mirrors
 * base: mirror.fibergrid.in
 * extras: mirror.fibergrid.in
.
.
.
Transaction test succeeded
Running transaction
  Installing : haproxy-1.5.14-3.el7.x86_64                                                                                                                                               1/1
  Verifying  : haproxy-1.5.14-3.el7.x86_64                                                                                                                                               1/1

Installed:
  haproxy.x86_64 0:1.5.14-3.el7                                                                                                                                                              

Complete!

Configure the HAproxy by replace the line ' frontend main *:5000' with ' frontend main *:80' and comment the line ' use_backend static if url_static' . Replace the web server' s IP address as shown below,

[root@ha2 ~]# vim /etc/haproxy/haproxy.cfg

Entry:

server ws1 192.168.5.159:80 check
server ws2 192.168.5.160:80 check

Then start and enable the service.

[root@ha2 ~]# systemctl start haproxy
[root@ha2 ~]# systemctl enable haproxy
Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service.

Once the service is enabled, add the firewall by creating new file under /etc/firewalld/services.

[root@ha2 ~]# vim /etc/firewalld/services/haproxy.xml
HAProxy
HAProxy load-balancer

Assign the SELinux context with the following command.

[root@ha2 ~]# cd /etc/firewalld/services
[root@ha2 services]# restorecon haproxy.xml
[root@ha2 services]# chmod 640 haproxy.xml

Open the HAproxy port on the firewall.

[root@ha2 services]# firewall-cmd --permanent --add-service=haproxy
success
[root@ha2 services]# firewall-cmd --reload
Success

Now its time to configure Keepalived, use the following command to install the required packages.

[root@ha2 ~]# yum install keepalived -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.fibergrid.in
 * extras: mirror.fibergrid.in
 * updates: mirror.fibergrid.in
Resolving Dependencies
-->  Running transaction check
--->  Package keepalived.x86_64 0:1.2.13-7.el7 will be installed
-->  Processing Dependency: libnetsnmpmibs.so.31()(64bit) for package: keepalived-1.2.13-7.el7.x86_64
.
.
.
Installed:
  keepalived.x86_64 0:1.2.13-7.el7                                                                                                                                                           

Dependency Installed:
  net-snmp-agent-libs.x86_64 1:5.7.2-24.el7_2.1                                                                                                                                              

Complete!

Edit the configuration file by creating a backup for the default configuration. Create new configuration and add the below entry as shown below,

[root@ha2 ~]# cd /etc/keepalived/
[root@ha2 keepalived]# ls
keepalived.conf
[root@ha2 keepalived]# mv keepalived.conf keepalived.conf.bak
[root@ha2 keepalived]# ls
keepalived.conf.bak
[root@ha2 keepalived]# vim keepalived.conf

vrrp_script chk_haproxy {
  script " killall -0 haproxy"  # check the haproxy process
  interval 2 # every 2 seconds
  weight 2 # add 2 points if OK
}

vrrp_instance VI_1 {
  interface eth0 # interface to monitor
  state MASTER # MASTER on ha1, BACKUP on ha2
  virtual_router_id 51
  priority 101 # 101 on ha1, 100 on ha2
  virtual_ipaddress {
    192.168.5.200 # virtual ip address
  }
  track_script {
    chk_haproxy
  }
}

Once the above process is completed, then start and enable the service.

[root@ha2 ~]# systemctl stop haproxy
[root@ha2 ~]# systemctl status haproxy

Finally, it is set for high availability load balancer. Just enter IP address in the web browser to see the content of the configured web-server. You can also cross check it by calling IP address of Load Balancer for the same result.

HAproxy-load-balancer-Keepalived

It there is any failure in any one load balancer, it will not affect the client to view the web-server’ s.

Tags:
caden
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

Q

What isHAproxy Load Balancer?

A

HAproxy is a high available load balancer that performs load balancing for HTTP and other TCP-based services. It can be done by distributing load across real servers. The HAproxy is configured with keepalived to set-up load balance with two or more nodes.

Q

What does layer 7 mean?

A

Layer 7 is related to the seventh layer of the OSI model: application level.
For example, HTTP, FTP, SMTP, DNS protocols are application level.

Q

What is layer 4 load-balancing?

A

A layer 4 load-balancer takes routing decision based on IPs and TCP or UDP ports
The layer 4 connection is established between the client and the server.
It is really fast but can’t perform any action on the protocol above layer 4.

The fastest layer4 load-balancers uses an ASIC to take routing decision

Q

What are the possible architectures with layer4 load balancers?

A

Mainly three architectures are doable, depending on your needs:

NAT or routed
Direct Server Return or Gateway mode
IP Tunnel mode

Q

What are the possible architectures with layer7 load balancers?

A

Actually, only one: Proxy mode, but two main flavors of it are available:
Proxy mode
Transparent proxy mode

Related Tutorials in How to Configure HAproxy Load Balancer with Keepalived in CentOS

Related Tutorials in How to Configure HAproxy Load Balancer with Keepalived in CentOS

How to install Apache from Source Code on CentOS 7
How to install Apache from Source Code on CentOS 7
Oct 21, 2017
How to Completely Remove and Install Apache package on CentOS 7.6
How to Completely Remove and Install Apache package on CentOS 7.6
May 23, 2019
How to install apache jmeter on ubuntu 18.04
How to install apache jmeter on ubuntu 18.04
May 19, 2018
How to Install Pligg - Content Management System
How to Install Pligg - Content Management System
Jul 26, 2016
How to Configure HAproxy Load Balancer with Keepalived in CentOS
How to Configure HAproxy Load Balancer with Keepalived in CentOS
Nov 21, 2016
How to enable the Apache server-status on centos 7
How to enable the Apache server-status on centos 7
Jan 28, 2019
How to Create Ansible Playbook to Install Apache Server
How to Create Ansible Playbook to Install Apache Server
May 12, 2021
How to Configure Reverse Proxy with Apache in CentOS
How to Configure Reverse Proxy with Apache in CentOS
Jan 25, 2017

Related Forums in How to Configure HAproxy Load Balancer with Keepalived in CentOS

Related Forums in How to Configure HAproxy Load Balancer with Keepalived in CentOS

CentOS
connor class=
How To Completely Remove Apache package On CentOS 7.6
May 14, 2019
Apache
isaac class=
How to disable apache welcome page on Ubuntu
Dec 15, 2018
Apache
rebeccajazz class=
Apache2 : mod_proxy in opensuse
Jan 3, 2018
Apache
elijah class=
What is the difference between httpd and apache
Feb 18, 2017
Linux Monitoring tool
luke class=
how to monitor haproxy using nagios
Jun 15, 2018
Apache
logan class=
How to install Apache GUI
Feb 24, 2017
Apache
rolando class=
How to find apache user in opensuse
Sep 23, 2017
Apache
caden class=
how to use php variables in apache
May 12, 2017

Related News in How to Configure HAproxy Load Balancer with Keepalived in CentOS

Related News in How to Configure HAproxy Load Balancer with Keepalived in CentOS

Attackers take advantage of Apache Struts vulnerabilities
Attackers take advantage of Apache Struts vulnerabilities
Mar 17, 2017
An Apache Web Server Bug That Grants Root Access on  Shared Web Hosts
An Apache Web Server Bug That Grants Root Access on Shared Web Hosts
Apr 10, 2019
Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help legeek ?
Installation of the call center module

hello

I wish to install a call center in virtual with issabel, I downloaded the latest version of it , but I don' t arrive to install the call center module in issabel. please help me

thanks!

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.