Vagrant HAProxy Demo using CentOS7

I wanted to get a little more familiar with both HAProxy config and Vagrant so put the two together and bam, magic!  Sort of.  After trying multiple Vagrantfile config options to set a static IP I gave up; my custom CentOS7 network config would get wiped shortly after boot*.  I then began testing with ifcfg files and they did exactly what I needed along with a few well placed Vagrantfile options.  Sure, I could have used Vagrants internal network scheme but where is the fun in that?

*potential user error

Take that a step further and I threw together a package that that will create the interface configs, the Vagrantfile, the haproxy.cfg file, boot three CentOS7 VMs, configure NICs, and install the necessary components.  The existing package doesn’t have input validation, assumes a working Vagrant environment, you are deploying to a routable /24 network, have an internet connection, and are using CentOS as host – if everything goes well you’ll have the demo running in about 7 minutes.

Clone the repo, run bash configureSupportFiles.sh, enter available IPs on a /24 subnet within your LAN, select the interface that should bridge with the VMs, and off ya go.

Customizations to the HAProxy VM will:
enable logging
echo Setup HAProxy logging 
sed -i 's/\#\$ModLoad imudp/\$ModLoad imudp/' /etc/rsyslog.conf 
sed -i 's/\#\$UDPServerRun 514/\$UDPServerRun 514/' /etc/rsyslog.conf 
echo local2.* /var/log/haproxy.log > /etc/rsyslog.d/haproxy.conf 
systemctl restart rsyslog
enable the stats webpage
listen stats :8080
mode http
stats enable
stats hide-version
stats realm HAproxy\ Statistics
stats uri /stats
stats auth admin:plaintext
enable granular control of the HAProxy servers
stats socket /var/lib/haproxy/stats level admin

and by default will send 66% of the traffic to NGINX2.  Remove the # and “weight x00” from the lines below, systemctl restart haproxy, and you’ll be back to standard round robin load balancing

backend web
    #balance roundrobin
    server  web1 <NGINX1>:80 check weight 100
    server  web2 <NGINX2>:80 check weight 200

 

Review HAProxy logs
# refresh the main page to populate
sudo tail -f /var/log/haproxy.log

 

Review HAProxy status and disable/enable servers from the HA pool
# haproxy vm was set as default in vagrantfile during setup, no need to specify name
vagrant ssh

# display available commands
echo "show help" | sudo socat stdio /var/lib/haproxy/stats

# review state
echo "show info" | sudo socat stdio /var/lib/haproxy/stats

# remove web1 from service, existing connection should be fine
# refreshing website will show all new connections going to web2
echo "disable server web/web1" | sudo socat stdio /var/lib/haproxy/stats

# put web1 back into service
echo "enable server web/web1" | sudo socat stdio /var/lib/haproxy/stats

 

Silence the DUP! messages when pinging from inside a Vagrant VM
# run on host machine
sudo sysctl -w net.ipv4.ip_forward=0