Sign inSign up

openbridge/haproxy

By openbridge

•Updated almost 8 years ago

HAProxy is a free, open source for load balancing and proxying for TCP and HTTP-based applications

Image
0

1.9K

openbridge/haproxy repository overview

⁠HAProxy Docker With Logging

HAProxy is a free, open source high availability solution, providing load balancing and proxying for TCP and HTTP-based applications by spreading requests across multiple servers. It is written in C and has a reputation for being fast and efficient (in terms of processor and memory usage).

⁠Install HaProxy

Installing openbridge/haproxy is pretty simple. You can either 1) build an image or 2) pull an image from Docker Hub. Below we show you how to build or pull the image:

⁠Pull

To build the latest image execute the pull command:

docker pull openbridge/haproxy
⁠Build

To build the image execute the build command:

docker build -t openbridge/haproxy .

You can also use a different version of HAProxy simply by pulling a build with the HAProxy version you want. For example;

docker pull openbridge/haproxy:1.18.4
docker pull openbridge/haproxy:1.18.3
docker pull openbridge/haproxy:1.18.2

To see the available versions visit: https://hub.docker.com/r/openbridge/haproxy/tags/⁠

⁠Running Your HAProxy Service

⁠Step 1: Create your haproxy.cfg configuration file

One of the first things you need to do is create your haproxy.cfg. We include a generic config file with the build. However, you will want to use your own. There are a ton of great resources online that detail different types of configurations that can be put into your haproxy.cfg. Please refer to upstream's excellent (and comprehensive) documentation⁠ on the subject of configuring HAProxy for your needs. It is also worth checking out the examples/ directory⁠from git.

Below is the template config to help you get a sense of what a config looks like:

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.5/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
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

    pidfile     /run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
    bind *:5000
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    use_backend static          if url_static
    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 127.0.0.1:4331 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server  app1 127.0.0.1:5001 check
    server  app2 127.0.0.1:5002 check
    server  app3 127.0.0.1:5003 check
    server  app4 127.0.0.1:5004 check

⁠Step 2: Starting your HAProxy service

So you have your haproxy.cfg file ready to go, awesome!

⁠Option 1: Using docker run to start your service
docker run -d --name openbridge/haproxy -p 22:22 -p 443:443 -v /path/to/etc/haproxy/haproxy.cfg:/etc/haproxy.cfg:ro haproxy:1.8:latest

NOTE: The image is expecting haproxy.cfg to present inside the container here: /etc/haproxy.cfg. You want to make sure you mount the /path/to/haproxy.cfg.

Without haproxy.cfg your service will not run as expected (or at all!).

Also, you may need to set the ports HAProxy is listening on by specifying the -p option, for example -p 8080:80. This will publish port 8080 from the container host to port 80 in the container. Make sure the port you want to use is free!

⁠Option 2: Using docker-compose to start your service
version: '3.1'
services:
  haproxy:
    image: openbridge/haproxy
    container_name: haproxy
    ports:
      - "22:22"
      - "443:443"
    tty: true
    env_file:
      - ./env/myenvvariables.env
    volumes:
      - /home/ec2-user/haproxy.cfg:/etc/haproxy.cfg
    restart: unless-stopped
    ulimits:
      nproc: 65535
      nofile:
          soft: 49999
          hard: 99999

Via Docker compose:

docker-compose up -d

⁠Monitoring

The service(s) running in the container are monitored via Monit. The principle goal is to automate a few basic tasks to keep the services running and operational.

check process haproxy-server with pidfile /run/haproxy.pid
      every 4 cycles
      start program = "/bin/bash -c 'haproxy -W -db -p /run/haproxy.pid -f /etc/haproxy.cfg'" with timeout 60 seconds
      stop program = "/bin/bash -c 'pkill haproxy'"
      if 3 restarts within 5 cycles then stop
      if cpu > 90% for 8 cycles then stop
      if failed port 22 for 6 cycles then stop
      if failed port 443 for 6 cycles then stop

check process ryslogd with pidfile /run/rsyslogd.pid
      every 4 cycles
      start program = "/usr/sbin/rsyslogd" with timeout 60 seconds
      stop program = "/bin/bash -c 'pkill rsyslogd'"

⁠Logging

Logging is redirected to stdout via rsyslogd. This allows you to see activity via commands like docker logs haproxy. You can dispatch the logs to a service like Amazon Cloudwatch.

⁠Issues

If you have any problems with or questions about this image, please contact us through a GitHub issue.

⁠Contributing

You are invited to contribute new features, fixes, or updates, large or small; we are always thrilled to receive pull requests, and do our best to process them as fast as we can.

Before you start to code, we recommend discussing your plans through a GitHub issue, especially for more ambitious contributions. This gives other contributors a chance to point you in the right direction, give you feedback on your design, and help you find out if someone else is working on the same thing.

⁠License

This project is licensed under the MIT License - see the LICENSE⁠ file for details

Tag summary

Content type

Image

Digest

Size

46.2 MB

Last updated

almost 8 years ago

docker pull openbridge/haproxy