Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Added support for running on Worker nodes in Swarm Mode #216

Open
wants to merge 6 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Docker 1.12 supports SwarmMode natively. `dockercloud/haproxy` will auto config
- You **HAVE TO** set the environment variable `SERVICE_PORTS=<port1>, <port2>` 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.
Expand Down Expand Up @@ -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://<host-ip>:<STATS_PORT>/`|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|
Expand Down
3 changes: 2 additions & 1 deletion haproxy/config.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import logging
import os
import re
import enum

logger = logging.getLogger("haproxy")


class RunningMode():
LegacyMode, ComposeMode, SwarmMode, CloudMode = range(4)


def parse_extra_bind_settings(extra_bind_settings):
bind_dict = {}
if extra_bind_settings:
Expand Down Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions haproxy/eventhandler.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion haproxy/haproxycfg.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,24 @@ 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)
return None
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)))
Expand Down
Empty file modified haproxy/helper/swarm_mode_link_helper.py
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions haproxy/main.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions requirements.txt
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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