Haproxy with Lua Support

haproxy

 

Compiling HAProxy with Lua support

Haproxy  requires Lua 5.3 or later. Lua version 5.3  is young and you may not find it with many  Linux distros. But its very easy to build from source as there are not much dependancies

First download source file for lua from here

1 2 3 4 wget http://www.lua.org/ftp/lua-5.3.3.tar.gz   tar xzf lua5.3.3.tar.gz cd lua5.3.3

Build it for linux

1 make linux

Install it

1 make install

Once installed ,verify the lua  version using

1 2 3 # lua -v Lua 5.3.3 Copyright (C) 19942016 Lua.org, PUCRio  

Install Haproxy

Download the source from here

1 2 3 wget http://www.haproxy.org/download/1.6/src/haproxy-1.6.0.tar.gz tar xzf haproxy1.6.0.tar.gz cd haproxy1.6.0

Now you can build it using following command

1 make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_CRYPT_H=1 USE_LIBCRYPT=1 USE_LUA=1

Then install haproxy using

1 make install

Once installed you can verify the haproxy version using

1 2 3 # haproxy -v HAProxy version 1.6.0 2015/10/13 Copyright 20002015 Willy Tarreau <willy@haproxy.org>

Here are few example configurations  for  different haproxy-lua implementations

1. Dynamic backends for different Host headers

Consider the situation you have multiple  backend servers for different websites and your loadbalancer has to identify the host header and distribute traffic accordingly

Please check below lua code , which does the vhost-backend mapping

1 2 3 4 5 6 7 8 9 10 11   #cat /scripts/getbackend.lua core.register_fetches(“choose_backend”, function(txn) if txn.sf:req_fhdr(“host”) == ‘test1.com’ then return “backend1” elseif txn.sf:req_fhdr(“host”) == ‘test2.com’ then return “backend2” end return “backend1” end)   

Here is the haproxy config file which uses above lua script

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 global   daemon   log /dev/log local0   log /dev/log local1 notice   maxconn 50000   serverstatebase /var/state/haproxy/ #  lua-load /scripts/getbackend.lua luaload /scripts/matchsourceip.lua defaults   loadserverstatefromfile global   log               global   retries                   3   backlog               10000   maxconn               10000   timeout connect          3s   timeout client          30s   timeout server          30s   timeout tunnel        3600s   timeout httpkeepalive  1s   timeout httprequest    15s   timeout queue           30s   timeout tarpit          60s   option            redispatch   option            httpserverclose   option            dontlognull   frontend mywebserver_10001   bind *:10001   mode tcp   use_backend %[lua.choose_backend]   backend backend1   balance roundrobin   mode tcp   server 192_168_2_16_80 192.168.2.16:80   backend backend2   balance roundrobin     mode tcp         server 192_168_2_8_80  192.168.2.8:80

2. Dynamic backend which matches request’s source IP

Another example, consider the situation you have to route the traffic to multiple backends according to source IP of the incoming request

1 2 3 4 5 6 7 8 9 10 11 12   #cat /scripts/matchsourceip.lua core.register_fetches(“choose_backend”, function(txn)   if txn.f:src() == ‘192.168.2.21’ then return “backend1” elseif txn.f:src() == ‘192.168.2.8’ then return “backend2” end return “backend1” end)   

I have posted more sample haproxy-lua scripts here , please checkout.

 
Author: , 0000-00-00