Giter VIP home page Giter VIP logo

docker-swarm-sdk's Introduction

docker-swarm-api

Ruby GEM providing API for managing Docker Swarm clusters.

MIT License

Must use Docker Engine Version of 1.12 or above. Docker Engine version 1.12.5 required to make overlay networks with API.

Must use Docker API Version of 1.24 or above.

Docker Swarm is improving rapidly. The controls for services has seen great improvements lately. This GEM helps connect your Ruby scripts/applications to create and extend your swarm and then manage services upon the swarm.

This project leverages swipely/docker-api (https://github.com/swipely/docker-api), and adds Docker Swarm capability.

Sample Usage

 # Make a connection to the Swarm manager's API.  (Assumes port 2375 exposed for API)
master_connection = Docker::Swarm::Connection.new('http://10.20.30.1:2375')

# If swarm on the swarm master and using socket:
master_connection = Docker::Swarm::Connection.new('unix:///var/run/docker.sock')

 # Manager node intializes swarm
swarm_init_options = { "ListenAddr" => "0.0.0.0:2377" }
swarm = Docker::Swarm::Swarm.init(swarm_init_options, master_connection)

 # Gather all nodes available to swarm (overlay and bridges)
nodes = swarm.nodes()
expect(nodes.length).to eq 1

 # Worker joins swarm
worker_connection = Docker::Swarm::Connection.new('http://10.20.30.2:2375')
swarm.join_worker(worker_connection)

# Worker joins without master api connection
swarm_options = { "manager_ip" => "10.20.30.1", "node_ip" => "10.20.30.2", "JoinTokens" => {"Worker" => "FooBar" }}
swarm = Docker::Swarm::Swarm.new(swarm_options)
local_connection = Docker::Swarm::Connection.new('unix:///var/run/docker.sock')
swarm.join_worker(local_connection)

 # Join another manager to the swarm
manager_2_connection = Docker::Swarm::Connection.new('http://10.20.30.3:2375')
swarm.join_manager(manager_2_connection)

# Manager joins without master api connection
swarm_options = { "manager_ip" => "10.20.30.1", "node_ip" => "10.20.30.2", "JoinTokens" => {"Master" => "FooBar" }}
swarm = Docker::Swarm::Swarm.new(swarm_options)
local_connection = Docker::Swarm::Connection.new('unix:///var/run/docker.sock')
swarm.join_manager(local_connection)

 # Gather all nodes of swarm
nodes = swarm.nodes()

 # Create a network which connect services
network = swarm.create_overlay_network(network_name)

 # Find all networks in swarm cluster
networks = swarm.networks()

 # Create a service with 5 replicas
service_create_options = {"Name"=>"nginx",
 "TaskTemplate" =>
  {"ContainerSpec" =>
    {"Networks" => [], "Image" => "nginx:1.11.7", "Mounts" => [], "User" => "root"},
   "Env" => ["TEST_ENV=test"],
   "LogDriver" => {"Name"=>"json-file", "Options"=>{"max-file"=>"3", "max-size"=>"10M"}},
   "Placement" => {},
   "Resources" => {"Limits"=>{"MemoryBytes"=>104857600}, "Reservations"=>{}},
   "RestartPolicy" => {"Condition"=>"on-failure", "Delay"=>1, "MaxAttempts"=>3}},
 "Mode"=>{"Replicated" => {"Replicas" => 5}},
 "UpdateConfig" => {"Delay" => 2, "Parallelism" => 2, "FailureAction" => "pause"},
 "EndpointSpec"=>
  {"Ports" => [{"Protocol"=>"tcp", "PublishedPort" => 8181, "TargetPort" => 80}]},
 "Labels" => {"layer" => "database"},
 "Networks" => [{"Target" => "my-network"}]
}
service = swarm.create_service(service_create_options)

 # Retrieve all manager nodes of swarm
manager_nodes = swarm.manager_nodes()

 # Retrieve all worker nodes (that aren't managers)
worker_nodes = swarm.worker_nodes()

 # Drain a worker node - stop hosting tasks/containers of services
worker_node = worker_nodes.first
worker_node.drain()

 # Gather all tasks (containers for service) being hosted by the swarm cluster
tasks = swarm.tasks()

 # Scale up or down the number of replicas on a service
service.scale(20)

 # Worker leaves the swarm - no forcing
swarm.leave(worker_node, node)

 # Manager leaves the swarm - forced because last manager needs to use 'force' to leave the issue.
swarm.leave(manager_node, true)

docker-swarm-sdk's People

Contributors

mmoore1915 avatar elthariel avatar dpattmann avatar mikejmoore avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.