项目作者: muayyad-alsadi

项目描述 :
Automated unattended pluggable docker management
高级语言: Python
项目地址: git://github.com/muayyad-alsadi/docker-glue.git
创建时间: 2015-09-01T16:56:38Z
项目社区:https://github.com/muayyad-alsadi/docker-glue

开源协议:

下载


Docker Glue

Automated unattended pluggable docker management based on docker events.
Can be used to update load-balancers, DNS, service discovery, …etc.
Managing docker containers would be as simple as tagging them with some labels

Use Cases

  • Dynamically add/remove containers to/from load-balancer (currently haproxy using jinja2 templates)
  • can send traffic of a specific domain to corresponding containers based on Host HTTP header
  • can send traffic of a specific path prefix to corresponding containers
  • Replace docker0 bridge,docker-proxy, …etc with more advanced SDN (like OVS or OpenStack Neutron).
  • Run a specific handler code (python plugins) or handler script based on docker events
  • publish containers inspection to discovery service (like etcd)

Daemons

  • docker-glue the modular pluggable daemon that can run handlers and scripts
  • docker-balancer a standalone daemon that just updates haproxy (a special case of glue)

You can pass -1 to run once, and -w to wait for events, and -h for details.

Requirements and Installation

  1. yum install haproxy python-docker-py python-jinja2
  2. cp docker-balancer.service /etc/systemd/system/docker-balancer.service
  3. cp docker-glue.service /etc/systemd/system/docker-glue.service

Using Docker Balancer

you can set labels like (replace 80 with any port):

  • glue_http_80_host the HTTP host to which this container would be attached in the load-balancer
  • glue_http_80_prefix the prefix (without leading /) to attach it to
  • glue_http_80_strip_prefix pass 1 if the prefix should be stripped before passing to backend
  • glue_http_80_weight the weight (defaults to 100)

let’s assume that you have started docker-balancer -w or the docker-balancer service

  1. docker run -d --name wp1 -l glue_http_80_host='wp1.example.com' mywordpress/wordpress
  2. docker run -d --name wp2 -l glue_http_80_host='wp2.example.com' mywordpress/wordpress
  3. docker run -d --name os-ui1 -l glue_http_80_host=openstack.example.com -l glue_http_80_prefix=dashboard/horizon myopenstack/horizon
  4. docker run -d --name os-id1 -l glue_http_80_host=openstack.example.com -l glue_http_80_prefix=identity/keystone myopenstack/keystone

Using Docker Glue

in docker-glue.d you have many .example files copy the files you need to remove that extension, for example

  1. cd docker-glue.d
  2. cp lb.ini.example lb.ini
  3. cp test.ini.example test.ini
  4. cd ..

then start docker-glue daemon or run docker-glue -w

Handlers INI files

files in docker-glue.d/*.ini (we have included examples) looks like this

  1. [handler]
  2. class=DockerGlue.handlers.exec.ScriptHandler
  3. events=all
  4. enabled=1
  5. triggers-none=0
  6. [params]
  7. script=test-handler.sh
  8. demo-option=some value

handler section specifies what and when to run

  • class the handler plugin, which can be one of
    • DockerGlue.handlers.exec.ScriptHandler executes a shell script
      • test-handler.sh a demo script that logs the event to /tmp/docker-glue-test.log
      • ovs-handler.sh connect the container to OpenVSwicth
    • DockerGlue.handlers.lb.HAProxyHandler load balancer that uses HAProxy
    • DockerGlue.handlers.publishers.distconfig.Publisher publish containers to discovery service like etcd
  • events the even statuses (comma separated) that triggers this handler, can be none (which is dummy event) or all (which does not include none)
  • enabled
  • triggers-none - set it if you want this handler to triggers none dummy event

params section is custom params to be passed to the handler

Writing script plugins

scripts in handler-scripts will be passed the ini path and docker event and the docker container id for example it might be like this test-handler.sh test.ini start 123456

the code of test-handler.sh look like this

  1. #! /bin/bash
  2. cd `dirname $0`
  3. function error() {
  4. echo "$@"
  5. exit -1
  6. }
  7. [ $# -ne 3 ] && error "Usage `basename $0` config.ini status container_id"
  8. ini="$1"
  9. status="$2"
  10. container_id="$3"
  11. ini_demo_option=$( crudini --inplace --get $ini params demo-option 2>/dev/null || : )
  12. echo "`date +%F` container_id=[$container_id] status=[$status] ini_demo_option=[$ini_demo_option]" >> /tmp/docker-glue-test.log

as you can see you can read options using crudini from the passed ini file.

Writing python plugins

just extend BaseHandler and in the __init__ do what ever you need, and read your custom params from the ini and implement a handle method like this

  1. from . import BaseHandler
  2. logger = logging.getLogger(__name__)
  3. class DemoHandler(BaseHandler):
  4. def __init__(self, agent, ini_file, ini):
  5. BaseHandler.__init__(self, agent, ini_file, ini)
  6. self.custom_param=ini.get('params', 'custom_param') if ini.has_option('params', 'custom_param') else None
  7. def handle(self, event, container_id):
  8. logger.info('got event=%r on container=%r and custom_param=%r', event, container_id, self.custom_param)

TODO

  • implement TCP/UDP load balancing using ipvsadm or keepalived