Install HAProxy

(0 comments)

How to install and configure HAproxy 1.5 on Ubuntu 12.04
HAProxy - The Reliable, High Performance TCP/HTTP Load Balancer

haproxy to -mysql

HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP andHTTP-based applications. It is particularly suited for very high traffic web sites and powers quite a number of the world's most visited ones. Over the years it has become the de-facto standard opensource load balancer, is now shipped with most mainstream Linux distributions, and is often deployed by default in cloud platforms. Since it does not advertise itself, we only know it's used when the admins report it :-)

Its mode of operation makes its integration into existing architectures very easy and riskless, while still offering the possibility not to expose fragile web servers to the net, such as below

Servers:

haproxy:
IP - 10.185.0.16
Percona cluster Mysql server:
mysql1:
IP - 10.185.0.18
mysql2:
IP - 10.185.0.19
#for future
web servers:
lamp1:
IP - 10.185.0.151
lamp2:
IP - 10.185.0.152

server haproxy:

We need to add repo to install haproxy 1.5

$apt-get install python-software-properties
$add-apt-repository ppa:vbernat/haproxy-1.5
$sudo apt-get update
$sudo apt-get install haproxy

By default haproxy cannot start, we need edit file:
/etc/default/proxy to set 1:
ENABLE=1
$sudo service haproxy start
$haproxy -v
should :
HA-Proxy version 1.5.6 2014/10/18
Copyright 2000-2014 Willy Tarreau <[email protected]>

edit new file /etc/haproxy/haproxy.cfg

global
    log 127.0.0.1 local0 notice
    maxconn 2000
    user haproxy
    group haproxy

defaults
    log     global
    mode    http
#    option  httplog
    option  dontlognull
    retries 3
    option redispatch
    timeout connect  70000
    timeout client  50000
    timeout server  50000

#
listen appname 0.0.0.0:80
    mode http
    stats enable
    stats uri /haproxy?stats
    stats realm Strictly\ Private
    stats auth user:123
    balance static-rr
    option httpclose
    option forwardfor
    server lamp1 10.184.211.151:80 check 
    server lamp2 10.184.211.152:80 check 




listen mysql-cluster
    bind 0.0.0.0:3306   #you must change this ip to 10.185.0.15   after migrate from master mysql srv
    mode tcp
    balance roundrobin
    maxconn 5200
    option mysql-check user haproxy_check
    server mysql1 10.185.0.19:3306 check port 3306
    server mysql2 10.185.0.18:3306 check port 3306 backup

section global:
log 127.0.0.1 local0 notice
we can use rsyslog for logging, by default rsyslog runs but is not listennig, let's fixed it.

Edit or create file if it doesn't exist /etc/rsyslog.d/haproxy.conf:

# Create an additional socket in haproxy's chroot in order to allow logging via
# /dev/log to chroot'ed HAProxy processes
$AddUnixListenSocket /var/lib/haproxy/dev/log

# Send HAProxy messages to a dedicated logfile
if $programname startswith 'haproxy' then /var/log/haproxy.log
&~

in file:
/etc/rsyslog.conf
we should uncomment the following lines:

$ModLoad imudp
$UDPServerRun 514

#add line
$UDPServerAddress 127.0.0.1

then restart rsyslog:
$sudo service rsyslog restart

check:

$sudo netstat -anp|grep rsyslog
udp   0   0 0.0.0.0:514   0.0.0.0:*     1563/rsyslogd

Good.
Section
listen appname 0.0.0.0:80
for load balancing web servers
we have two servers:
lamb1
lamp2

also we have statistic:

    stats enable
    stats uri /haproxy?stats
    stats realm Strictly\ Private
    stats auth user:123

in browser url:

http://10.185.0.16/haproxy?stats
user: user
password: 123 #use complex password!!!

Load balancing for Mysql servers

section

listen mysql-cluster
    server mysql1 10.185.0.19:3306 check port 3306
    server mysql2 10.185.0.18:3306 check port 3306 backup

all connections will be in mysql1
if mysql1 doens't work for any reason haproxy will switch all traffic to mysql2

option mysql-check user haproxy_check
needs to check active mysql server
we need to create user haproxy_check in mysql server which will allow permissions from the haproxy host only

mysql -u root -p -e "INSERT INTO mysql.user (Host,User) values ('10.185.0.16','haproxy_check'); FLUSH PRIVILEGES;"

Should be work!

Current rating: 1.3

Comments

There are currently no comments

New Comment

required

required (not published)

optional

required