Home » Postfix » Postfix load balancing and High Availability with HaProxy

Postfix load balancing and High Availability with HaProxy

HAProxy is a absolutely price less open source high availability and load balancing solution for application layer protocols such as HTTP, SMTP, MYSQL etc…. HAProxy provides fast, reliable,
stable high availability and load balacing solution for small, medium and enterprises organizations. It is very useful for high volume traffic applications. Haproxy is available in both form open source edition and enterprises edition. It’s widely used in IT world to improve performance, stability, reliability in server and multiple application environment. Haproxy generally reduce the over load from single host by distributing across multiple nodes

In previous post we have setup apache load balancing so this post we are going to setup postfix mail server with high availability load balancing for outbound mails using HaProxy load-balancer.
How to setup postfix with multiple instances.
How to configure mx-gateway (mail gateway) on postfix
Here we are using the following scanario for Postfix load balancing and High Availability with HaProxy.
HaProxy Load-Balancer :: 192.173.123.1
Postfix Host 1 :: 192.173.123.2
Postfix Host 2 :: 192.173.123.3
Postfix Host 3 :: 192.173.123.4
haproxy
Haproxy Load-Balancer
To install haproxy load-balancer execute the following command from root user on linux command prompt.

[root@mailer keys]# yum install haproxy
Resolving Dependencies
–> Running transaction check
—> Package haproxy.x86_64 0:1.5.2-2.el6 will be installed
–> Finished Dependency Resolution
Dependencies Resolved======================================================================================================================================
Package                          Arch                        Version                            Repository                               Size
======================================================================================================================================
Installing:
haproxy                         x86_64                    1.5.2-2.el6                         base                                          790 k
Transaction Summary
======================================================================================================================================
Install 1 Package(s)
Total download size: 790 k
Installed size: 2.4 M
Is this ok [y/N]: y
Downloading Packages:
haproxy-1.5.2-2.el6.x86_64.rpm | 790 kB 00:00
Installed:
haproxy.x86_64 0:1.5.2-2.el6
Complete!

Once installation process get completed open the haproxy configuration /etc/haproxy/haproxy.cfg file and appent the following lines.

#vi /etc/haproxy/haproxy.cfg
frontend ft_smtp
bind 192.173.123.1:25
mode tcp
no option http-server-close
timeout client 1m
log global
option tcplog
maxconn 3000
default_backend bk_postfix
backend bk_postfix
mode tcp
no option http-server-close
log global
option tcplog
timeout server 1m
timeout connect 30s
balance roundrobin
server postfix-host1 192.173.123.2:10025 check
server postfix-host2 192.173.123.3:10026 check
server postfix-host3 192.173.123.4:10027 check

After that start haproxy service by using following command.

#service haproxy start

If haproxy gets start without any issue that means haproxy setup and installation finish.
Postfix Installation.
To install postfix on host 1 use the following command from root user, Same steps of postfix installation and configuration will applicable on all hosts.
[root@linuxpcfix[~]# yum install postfix.
After that open the posfix master configuration file and modify as given below.

[root@linuxpcfix[~]# vi /etc/posfix/master.cf
10025           inet           n       –          n              –             1                 postscreen
smtpd          pass           –        –           n             –             –                  smtpd
#smtp          pass          n        –           n             –             –                  smtpd
#smtp          inet           n        –           n             –             1                postscreen

ON host 2

10026           inet          n             –             n        –          1              postscreen
smtpd           pass         –              –             n        –          –               smtpd
#smtp           pass        n              –             n        –          –               smtpd
#smtp           inet         n             –              n        –          1             postscreen

On Host 3

10027           inet        n              –               n       –            1            postscreen
smtpd           pass       –               –               n      –             –             smtpd
#smtp           pass       n              –               n      –             –             smtpd
#smtp           inet        n              –               n      –            1            postscreen

Then open the main configuration and modify the inet_interfaces as given below.

inet_interfaces = all

Finally restart postfix server.

[root@linuxpcfix[~]# service postfix restart.

Now verify the everything working or not using telnet command from your own system.

[root@linuxpcfix[~]# telnet 192.173.123.1 25
Trying 192.173.123.1..
Connected to 192.173.123.1.
Escape character is ‘^]’.
220 host1.linuxpcfix.com ESMTP Postfix
[root@linuxpcfix[~]# telnet 192.173.123.1 25
Trying 192.173.123.1..
Connected to 192.173.123.1.
Escape character is ‘^]’.
220 host2.linuxpcfix.com ESMTP Postfix
[root@linuxpcfix[~]# telnet 192.173.123.1 25
Trying 192.173.123.1..
Connected to 192.173.123.1.
Escape character is ‘^]’.
220 host3.linuxpcfix.com ESMTP Postfix

If you are able to see different output (host1.linuxpcfix.com, host2.linuxpcfix.com etc…) each time while performing the telnet command from your system that’s mean haproxy load-balancer properly configured and ready to production use.

About

I am founder and webmaster of www.linuxpcfix.com and working as a Sr. Linux Administrator (Expertise on Linux/Unix & Cloud Server) and have been in the industry from last 7 years.

2 thoughts on “Postfix load balancing and High Availability with HaProxy

  1. Rahul kumar says:

    getting error [ALERT] 214/125221 (25262) : Starting frontend ft_smtp: cannot bind socket while starting the HAproxy.

  2. Rahul kumar says:

    getting error
    Trying 192.173.123.1…
    telnet: Unable to connect to remote host: Connection refused
    when restarting the HAproxy

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Time limit is exhausted. Please reload the CAPTCHA.

Categorized Tag Cloud