Skip to content

Instantly share code, notes, and snippets.

@peterkappus
Last active January 6, 2019 23:20
Show Gist options
  • Save peterkappus/6f414ca01ab62c41899bbc6207cc5307 to your computer and use it in GitHub Desktop.
Save peterkappus/6f414ca01ab62c41899bbc6207cc5307 to your computer and use it in GitHub Desktop.
HAProxy on CentOS

Installing a load balancer on CentOS

...cuz we use CentOS on the work VMs :(

Create some docker images to use as upstream hosts:

mkdir s2 s1
echo "Hello World." > s1/index.html
echo "Good evening, Pasadena." > s2/index.html

docker run -p 8000:80 -v "$(PWD)/1":/usr/share/nginx/html:ro -d nginx
docker run -p 8001:80 -v "$(PWD)/s1":/usr/share/nginx/html:ro -d nginx

HAProxy for Centos

Installation

Do this (but use newer version) https://upcloud.com/community/tutorials/haproxy-load-balancer-centos/

Others I didn't use:

https://lists.centos.org/pipermail/centos-announce/2018-June/022915.html https://pario.no/2018/07/17/install-haproxy-1-8-on-centos-7/

Make sure CentOS can connect to the upstream servers:

setsebool -P haproxy_connect_any 1

Config file

Put this in /etc/haproxy/haproxy.cfg

global
   log /dev/log local0
   log /dev/log local1 notice
   chroot /var/lib/haproxy
   stats timeout 30s
   user haproxy
   group haproxy
   daemon

defaults
   log global
   mode http
   option httplog
   option dontlognull
   timeout connect 5000
   timeout client 50000
   timeout server 50000

frontend http_front
   bind *:80
   stats uri /stats
   default_backend http_back
   stats realm Haproxy\ Statistics
   # change username and password to something harder...
	 stats auth username:password
			
backend http_back
   balance roundrobin
	 cookie SERVERID insert indirect nocache
	 server host1 192.168.0.30:8000 check cookie s1
	    #server host1 192.168.0.30:8000 check weight 2 cookie s1
	    server host2 192.168.0.30:8001 check cookie s2

NGINX:

Looks cool but seems to require a $2500/year license to do what we want...

Starting/stopping in CentOS

service nginx stop | start | reload

Not connecting from CentOS?

setsebool -P httpd_can_network_connect on

Setup:

ensure no servers are already listening on port 80 (check /etc/nginx/conf.d/default.conf).

Copy the following to /etc/nginx/conf.d/rev_proxy.conf:

upstream myapp1 {
	# unsupported on free version :(
	# sticky cookie

	# you can use this but makes it tough to test since it
	# always routes a given IP to a specific server
	# ip_hash;
	server 192.168.0.30:8000;
	server 192.168.0.30:8001;
}

server {
	listen 80;
	#server_name revprox;

	location / {
		proxy_pass http://myapp1;
	}
}
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "Centos/7"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
config.vm.network "forwarded_port", guest: 80, host: 80
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
config.vm.provision "shell", inline: <<-SHELL
yum install vim sudo curl wget gcc pcre-static pcre-devel -y
wget https://www.haproxy.org/download/1.9/src/haproxy-1.9.0.tar.gz -O ~/haproxy.tar.gz
tar xzvf ~/haproxy.tar.gz -C ~/
cd ~/haproxy-1.9.0
make TARGET=linux2628
make install
mkdir -p /etc/haproxy
mkdir -p /var/lib/haproxy
touch /var/lib/haproxy/stats
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
cp ~/haproxy-1.9.0/examples/haproxy.init /etc/init.d/haproxy
chmod 755 /etc/init.d/haproxy
systemctl daemon-reload
chkconfig haproxy on
useradd -r haproxy
SHELL
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment