diff --git a/README.md b/README.md index 78263e3..fb919ab 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ Docker 1.12 supports SwarmMode natively. `dockercloud/haproxy` will auto config - You **HAVE TO** set the environment variable `SERVICE_PORTS=, ` in your application service, which are the ports you would like to expose. - For `dockercloud/haproxy` service: If you mount `/var/run/docker.sock`, it can only be run on swarm manager nodes. - If you want the haproxy service to run on worker nodes, you need to setup DOCKER_HOST envvar that points to the manager address. + If you want the haproxy service to run on worker nodes, you need to setup SWARM_MASTER_ADDRESS envvar that points to the manager address. * If your application services need to access other services(database, for example), you can attach your application services to two different network, one is for database and the other one for the proxy * This feature is still experimental, please let us know if you find any bugs or have any suggestions. @@ -233,6 +233,7 @@ If the setting has a list as a value (e.g. `ADDITIONAL_SERVICES`), multipe envir |SSL_BIND_OPTIONS|no-sslv3|explicitly set which SSL bind options will be used for the SSL server. This sets the HAProxy `ssl-default-bind-options` configuration setting. The default will allow only TLSv1.0+ to be used on the SSL server.|N| |STATS_AUTH|stats:stats|username and password required to access the Haproxy stats.|N| |STATS_PORT|1936|port for the HAProxy stats section. If this port is published, stats can be accessed at `http://:/`|N| +|SWARM_MASTER_ADDRESS| |If set, allows HAProxy to run on worker nodes. Use this in conjunction with mounting `/var/run/docker.sock` to make this work. |TIMEOUT|connect 5000, client 50000, server 50000|comma-separated list of HAProxy `timeout` entries to the `default` section.|N| |NBPROC|1|sets the `nbproc` entry to the `global` section. By default, only one process is created, which is the recommended mode of operation.|N| |HAPROXY_USER|haproxy|sets the user of the UNIX sockets to the designated system user name|N| diff --git a/haproxy/config.py b/haproxy/config.py old mode 100644 new mode 100755 index bf640d0..739de71 --- a/haproxy/config.py +++ b/haproxy/config.py @@ -1,6 +1,7 @@ import logging import os import re +import enum logger = logging.getLogger("haproxy") @@ -8,7 +9,6 @@ class RunningMode(): LegacyMode, ComposeMode, SwarmMode, CloudMode = range(4) - def parse_extra_bind_settings(extra_bind_settings): bind_dict = {} if extra_bind_settings: @@ -130,6 +130,7 @@ def getExtendedEnv(baseName, joinWith=',', envvars=os.environ, default=''): HAPROXY_USER = os.getenv("HAPROXY_USER", "haproxy") HAPROXY_GROUP = os.getenv("HAPROXY_GROUP", "haproxy") RELOAD_TIMEOUT = os.getenv("RELOAD_TIMEOUT", "0") +SWARM_MASTER_ADDRESS = os.getenv("SWARM_MASTER_ADDRESS") # global RUNNING_MODE = None diff --git a/haproxy/eventhandler.py b/haproxy/eventhandler.py old mode 100644 new mode 100755 index 946d23c..17a6af2 --- a/haproxy/eventhandler.py +++ b/haproxy/eventhandler.py @@ -103,12 +103,15 @@ def polling_service_status_swarm_mode(): time.sleep(config.SWARM_MODE_POLLING_INTERVAL) try: try: - docker = docker_client() + swarmMaster = docker_client() except: - docker = docker_client(os.environ) + swarmMaster = docker_client(os.environ) + + if config.SWARM_MASTER_ADDRESS: + swarmMaster = docker_client(os.environ, host=config.SWARM_MASTER_ADDRESS) - services = docker.services() - tasks = docker.tasks(filters={"desired-state": "running"}) + services = swarmMaster.services() + tasks = swarmMaster.tasks(filters={"desired-state": "running"}) _, linked_tasks = SwarmModeLinkHelper.get_task_links(tasks, services, Haproxy.cls_service_id, Haproxy.cls_nets) if cmp(Haproxy.cls_linked_tasks, linked_tasks) != 0: diff --git a/haproxy/haproxycfg.py b/haproxy/haproxycfg.py old mode 100644 new mode 100755 index efc3af8..985d1cd --- a/haproxy/haproxycfg.py +++ b/haproxy/haproxycfg.py @@ -108,10 +108,16 @@ def _init_swarm_mode_links(): try: try: docker = docker_client() + swarmMaster = docker_client() except: docker = docker_client(os.environ) + swarmMaster = docker_client(os.environ) + + if config.SWARM_MASTER_ADDRESS: + swarmMaster = docker_client(os.environ, host=config.SWARM_MASTER_ADDRESS) docker.ping() + swarmMaster.ping() except Exception as e: logger.info("Docker API error, regressing to legacy links mode: %s" % e) @@ -119,7 +125,7 @@ def _init_swarm_mode_links(): haproxy_container_id = os.environ.get("HOSTNAME", "") Haproxy.cls_service_id, Haproxy.cls_nets = SwarmModeLinkHelper.get_swarm_mode_haproxy_id_nets(docker, haproxy_container_id) - links, Haproxy.cls_linked_tasks = SwarmModeLinkHelper.get_swarm_mode_links(docker, Haproxy.cls_service_id, + links, Haproxy.cls_linked_tasks = SwarmModeLinkHelper.get_swarm_mode_links(swarmMaster, Haproxy.cls_service_id, Haproxy.cls_nets) logger.info("Linked service: %s", ", ".join(SwarmModeLinkHelper.get_service_links_str(links))) logger.info("Linked container: %s", ", ".join(SwarmModeLinkHelper.get_container_links_str(links))) diff --git a/haproxy/helper/swarm_mode_link_helper.py b/haproxy/helper/swarm_mode_link_helper.py old mode 100644 new mode 100755 diff --git a/haproxy/main.py b/haproxy/main.py old mode 100644 new mode 100755 index 1377038..30124e2 --- a/haproxy/main.py +++ b/haproxy/main.py @@ -82,6 +82,7 @@ def is_process_running(p): def check_running_mode(container_uri, service_uri, api_auth): mode, msg = None, "" + if container_uri and service_uri and api_auth: if container_uri and service_uri: if api_auth: diff --git a/requirements.txt b/requirements.txt old mode 100644 new mode 100755 index de2d3e0..fa01381 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ PyYAML==3.11 cached-property==1.2.0 -docker-py==1.10.3 -dockerpty==0.4.1 +docker-py>=1.10.3 +dockerpty>=0.4.1 docopt==0.6.1 enum34==1.0.4 jsonschema==2.5.1 @@ -10,6 +10,6 @@ future==0.15.0 requests==2.7.0 six==1.9.0 websocket-client==0.37.0 -docker-compose==1.6.0 -python-dockercloud==1.0.10 +docker-compose>=1.6.0 +python-dockercloud>=1.0.10 gevent==1.1.1