Start/Run Haproxy in foreground

run-haproxy-in-foreground

 

In this article we will explain how to start/Run HAproxy in foreground.
HAproxy is an open source software loadbalancer which is offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It is suited for suited for very high traffic web sites and powers quite a number of the world’s most visited ones. Ove the years it has become the default standard software load balancer, is now shipped with most mainstream Linux distributions.
With default configuration HAproxy runs as a deomon ( in background) which is good for most of the use cases. But sometimes we require it to start in foreground , one use case case is running haproxy on Docker, where the application has to keep running in foreground. Let us see how to start/run HAproxy in foreground .

Start/run HAproxy in foreground

You can see haproxy command line options using the command “haproxy –help”

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19   v displays version ; vv shows known build options. d enters debug mode ; db only disables background mode. dM[<byte>] poisons memory with <byte> (defaults to 0x50) V enters verbose mode (disables quiet mode) D goes daemon ; C changes to <dir> before loading files. q quiet mode : dont display messages c check mode : only check config files and exit n sets the maximum total # of connections (2000) m limits the usable amount of memory (in MB) N sets the default, perproxy maximum # of connections (2000) L set local peer name (default to hostname) p writes pids of all children to this file de disables epoll() usage even when available dp disables poll() usage even when available dS disables splice usage (broken on old kernels) dV disables SSL verify on servers side sf/st [pid ]* finishes/terminates old pids. Must be last arguments.  

The command line flag “-D” will launch HAproxy in background or  as a daemon. When you start HAproxy using command line options make sure you don’t use the option “D”

In the haproxy configuration file , you will see an option “daemon”, if you keep it commented HAproxy will not go to daemon mode and start on foreground
Sample config snippet is below

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24   global # to have these messages end up in /var/log/haproxy.log you will # need to: # # 1) configure syslog to accept network log events. This is done # by adding the ‘-r’ option to the SYSLOGD_OPTIONS in # /etc/sysconfig/syslog # # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like the following can be added to # /etc/sysconfig/syslog # # local2.* /var/log/haproxy.log # log 127.0.0.1 local2   chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy # daemon  

You can see that “deamon” option is commented. Now if we start haproxy

1 2 3 4 5 6   [root@test.test.com ~]# haproxy -f /etc/haproxy/haproxy.cfg [WARNING] 071/085241 (29884) : Server static/static is DOWN, reason: Layer4 connection problem, info: “Connection refused”, check duration: 0ms. 0 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue. [ALERT] 071/085241 (29884) : backend ‘static’ has no server available! [WARNING] 071/085242 (29884) : Server app/app4 is DOWN, reason: Layer4 connection problem, info: “Connection refused”, check duration: 0ms. 3 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.  

You can see that HAproxy started on foreground and printing debug logs to console.

Author: , 0000-00-00