Skip to content

Instantly share code, notes, and snippets.

@res0nat0r
Forked from stefhen/haproxy.rb
Created March 9, 2017 20:38
Show Gist options
  • Save res0nat0r/419d4225459b097ca2df644342cb9348 to your computer and use it in GitHub Desktop.
Save res0nat0r/419d4225459b097ca2df644342cb9348 to your computer and use it in GitHub Desktop.
generate haproxy.cfg with chef
# Wrapper cookbook snippet and upstream cookbook here: https://github.com/sous-chefs/haproxy
haproxy_lb 'backend-service-name-here' do
params([
'bind 0.0.0.0:80',
'mode http',
'balance roundrobin',
'option forwardfor',
'option http-server-close'
])
@params << "maxconn #{node['webloadbalancer']['dispatchers']['maxconn']}"
# populate backend servers via chef search
search_criteria = ''
search_criteria << %(chef_environment:#{node.chef_environment})
search_criteria << ' AND '
search_criteria << %(run_list:recipe\\[#{service_cookbook}\\:\\:#{service_recipe}\\])
search('node', search_criteria).sort.each do |n|
@params << "server #{n['name']} #{n['ipaddress']}:#{node['webloadbalancer']['forward-port']} weight 100 check inter 5s rise 2 fall 3"
end
end
include_recipe 'haproxy::manual'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment