Created
June 25, 2014 20:35
-
-
Save ranjib/0b57a6c44f116851b6db to your computer and use it in GitHub Desktop.
Dynamic haproxy using chef based on unprivileged containers (exposing containers port 80)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| template '/etc/haproxy/haproxy.cfg' do | |
| extend Helper | |
| variables( | |
| containers: container_ips('goatos'), | |
| start_port: 8000 | |
| ) | |
| source 'haproxy.cfg.erb' | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| global | |
| log /dev/log local0 | |
| log /dev/log local1 notice | |
| chroot /var/lib/haproxy | |
| user haproxy | |
| group haproxy | |
| daemon | |
| defaults | |
| log global | |
| option dontlognull | |
| contimeout 5000 | |
| clitimeout 50000 | |
| srvtimeout 50000 | |
| <% @containers.each |name, ip|%> | |
| listen <%=name.upcase %> :<%= (@start_port+=1) %> | |
| mode http | |
| server <%= name %> <%= ip %>:<%= 80 %> | |
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Helper | |
| def container_ips | |
| data = {} | |
| user = Etc.getpwnam(name) | |
| config = File.join(user.dir, '.local/share/lxc') # this can be injected from outside as well | |
| LXC.list_containers(config_path: config).each do |n| | |
| ct = LXC::Container.new(n, config) | |
| if ct.running? and (not ct.ip_addresses.empty?) | |
| data[ct.name] = ct.ip_addresses.first | |
| end | |
| end | |
| data | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment