added hautoproxy auto-configuring haproxy

This image configures haproxy to forward connections for all available
kubernetes services.  It is meant to be run alongside other contains in
a kubernetes pod to provide access to "remote" services at a consistent
address so that keystone api endpoints can be configured in a sane
fashion.

Change-Id: Ic923c6a772f1bdf36b97b05a1d04de9e5b841ddd
This commit is contained in:
Lars Kellogg-Stedman 2014-10-04 17:54:03 -04:00
parent 9414ab5cad
commit 154e2781d9
4 changed files with 100 additions and 0 deletions

View File

@ -0,0 +1,8 @@
FROM fedora
RUN yum -y install haproxy python-jinja2; yum clean all
RUN mkdir -p /etc/haproxy/templates
ADD haproxy.cfg.tmpl /etc/haproxy/templates/haproxy.cfg.tmpl
ADD start.py /start.py
CMD ["/start.py"]

1
docker/hautoproxy/build Symbolic link
View File

@ -0,0 +1 @@
../../tools/build-docker-image

View File

@ -0,0 +1,26 @@
global
daemon
maxconn 4096
pidfile /var/run/haproxy.pid
defaults
mode tcp
timeout connect 5s
timeout client 1m
timeout server 1m
option redispatch
balance roundrobin
listen stats :1936
mode http
stats enable
stats hide-version
#stats realm Haproxy\ Statistics
stats uri /
#stats auth Username:Password
{% for service in services %}
listen {{ service.service_name }}
bind 127.0.0.1:{{service.local_port}}
server {{ service.remote_name }} {{ service.remote_addr }}:{{ service.remote_port}} check inter 2s rise 3 fall 2
{% endfor %}

65
docker/hautoproxy/start.py Executable file
View File

@ -0,0 +1,65 @@
#!/usr/bin/python
'''This script configures and starts a local haproxy instances, bound to
127.0.0.1, that forwards connections all of the discovered
docker/kubernetes environment variables.'''
import argparse
import os
import sys
from jinja2 import Environment, FileSystemLoader
import re
import urlparse
re_url = re.compile(
'^(?P<name>.*)_PORT_(?P<port>\d+)_(?P<proto>(UDP|TCP))$')
def parse_args():
p = argparse.ArgumentParser()
p.add_argument('--output', '-o',
default='/etc/haproxy/haproxy.cfg')
p.add_argument('--no-start', '-n',
action='store_true')
p.add_argument('--template-dir', '-t',
default='/etc/haproxy/templates')
return p.parse_args()
def discover_services():
services = []
for k in os.environ:
mo = re_url.match(k)
if mo:
parts = urlparse.urlparse(os.environ[k])
remote_host,remote_port = parts.netloc.split(':')
service_name = '%(name)s-%(port)s' % mo.groupdict()
services.append({
'remote_name': mo.group('name'),
'remote_addr': remote_host,
'remote_port': remote_port,
'remote_proto': parts.scheme,
'local_port': mo.group('port'),
'service_name': service_name,
})
return services
def main():
args = parse_args()
services = discover_services()
env = Environment(loader=FileSystemLoader(['.',
args.template_dir]))
template = env.get_template('haproxy.cfg.tmpl')
with open(args.output, 'w') as fd:
fd.write(template.render(services=services))
if args.no_start:
return
os.execlp('haproxy', 'haproxy', '-f', args.output, '-db')
if __name__ == '__main__':
main()