Swarm发现服务

Docker Swarm带有多个Discovery后台

用例

使用hosted discovery服务

  1. # create a cluster
  2. $ swarm create6856663cdefdec325839a4b7e1de38e8
  3. # <- this is your unique <cluster_id>
  4. # on each of your nodes, start the swarm agent# <node_ip> doesn't have to be public (eg. 192.168.0.X),
  5. # as long as the swarm manager can access it.
  6. $ swarm join --addr=<node_ip:2375> token://<cluster_id>
  7. # start the manager on any machine or your laptop
  8. $ swarm manage -H tcp://<swarm_ip:swarm_port> token://<cluster_id>
  9. # use the regular docker cli
  10. $ docker -H tcp://<swarm_ip:swarm_port> info
  11. $ docker -H tcp://<swarm_ip:swarm_port> run ...
  12. $ docker -H tcp://<swarm_ip:swarm_port> ps
  13. $ docker -H tcp://<swarm_ip:swarm_port> logs ......
  14. # list nodes in your cluster
  15. $ swarm list token://<cluster_id><node_ip:2375>

使用静态文件描述服务

  1. # for each of your nodes, add a line to a file
  2. # <node_ip> doesn't have to be public (eg. 192.168.0.X),
  3. # as long as the swarm manager can access it
  4. $ echo <node_ip1:2375> >> /tmp/my_cluster
  5. $ echo <node_ip2:2375> >> /tmp/my_cluster
  6. $ echo <node_ip3:2375> >> /tmp/my_cluster# start the manager on any machine or your laptop
  7. $ swarm manage -H tcp://<swarm_ip:swarm_port> file:///tmp/my_cluster# use the regular docker cli
  8. $ docker -H tcp://<swarm_ip:swarm_port> info
  9. $ docker -H tcp://<swarm_ip:swarm_port> run ...
  10. $ docker -H tcp://<swarm_ip:swarm_port> ps
  11. $ docker -H tcp://<swarm_ip:swarm_port> logs ......
  12. # list nodes in your cluster
  13. $ swarm list file:///tmp/my_cluster<node_ip1:2375><node_ip2:2375><node_ip3:2375>

使用 etcd

  1. # on each of your nodes, start the swarm agent
  2. # <node_ip> doesn't have to be public (eg. 192.168.0.X),
  3. # as long as the swarm manager can access it.
  4. $ swarm join --addr=<node_ip:2375> etcd://<etcd_ip>/<path>
  5. # start the manager on any machine or your laptop
  6. $ swarm manage -H tcp://<swarm_ip:swarm_port> etcd://<etcd_ip>/<path>
  7. # use the regular docker cli
  8. $ docker -H tcp://<swarm_ip:swarm_port> info
  9. $ docker -H tcp://<swarm_ip:swarm_port> run ...
  10. $ docker -H tcp://<swarm_ip:swarm_port> ps
  11. $ docker -H tcp://<swarm_ip:swarm_port> logs ......
  12. # list nodes in your cluster
  13. $ swarm list etcd://<etcd_ip>/<path><node_ip:2375>

使用consul

  1. # on each of your nodes, start the swarm agent
  2. # <node_ip> doesn't have to be public (eg. 192.168.0.X),
  3. # as long as the swarm manager can access it.
  4. $ swarm join --addr=<node_ip:2375> consul://<consul_addr>/<path>
  5. # start the manager on any machine or your laptop
  6. $ swarm manage -H tcp://<swarm_ip:swarm_port> consul://<consul_addr>/<path>
  7. # use the regular docker cli
  8. $ docker -H tcp://<swarm_ip:swarm_port> info
  9. $ docker -H tcp://<swarm_ip:swarm_port> run ...
  10. $ docker -H tcp://<swarm_ip:swarm_port> ps
  11. $ docker -H tcp://<swarm_ip:swarm_port> logs ......
  12. # list nodes in your cluster
  13. $ swarm list consul://<consul_addr>/<path><node_ip:2375>

使用zookeeper

  1. # on each of your nodes, start the swarm agent
  2. # <node_ip> doesn't have to be public (eg. 192.168.0.X),
  3. # as long as the swarm manager can access it.
  4. $ swarm join --addr=<node_ip:2375> zk://<zookeeper_addr1>,<zookeeper_addr2>/<path>
  5. # start the manager on any machine or your laptop
  6. $ swarm manage -H tcp://<swarm_ip:swarm_port> zk://<zookeeper_addr1>,<zookeeper_addr2>/<path>
  7. # use the regular docker cli
  8. $ docker -H tcp://<swarm_ip:swarm_port> info
  9. $ docker -H tcp://<swarm_ip:swarm_port> run ...
  10. $ docker -H tcp://<swarm_ip:swarm_port> ps
  11. $ docker -H tcp://<swarm_ip:swarm_port> logs ......
  12. # list nodes in your cluster
  13. $ swarm list zk://<zookeeper_addr1>,<zookeeper_addr2>/<path><node_ip:2375>

 使用静态ip列表

  1. # start the manager on any machine or your laptop
  2. $ swarm manage -H <swarm_ip:swarm_port> nodes://<node_ip1:2375>,<node_ip2:2375>
  3. # or
  4. $ swarm manage -H <swarm_ip:swarm_port> <node_ip1:2375>,<node_ip2:2375>
  5. # use the regular docker cli
  6. $ docker -H <swarm_ip:swarm_port> info
  7. $ docker -H <swarm_ip:swarm_port> run ...
  8. $ docker -H <swarm_ip:swarm_port> ps
  9. $ docker -H <swarm_ip:swarm_port> logs ......

IP地址范围匹配模式

文件和节点发现支持用范围匹配模式设定IP地址,如,10.0.0.[10:200] 为10.0.0.10 到10.0.0.200一系列节点。

例如

  1. # file example$ echo "10.0.0.[11:100]:2375" >> /tmp/my_cluster
  2. $ echo "10.0.1.[15:20]:2375" >> /tmp/my_cluster
  3. $ echo "192.168.1.2:[2:20]375" >> /tmp/my_cluster
  4. # start the manager
  5. swarm manage -H tcp://<swarm_ip:swarm_port> file:///tmp/my_cluster
  6. # nodes example
  7. $ swarm manage -H <swarm_ip:swarm_port> "nodes://10.0.0.[10:200]:2375,10.0.1.[2:250]:2375"

启用一个新发现后台(discovery backend)

启用新的发现后台很简单,只需实现下面的接口:

  1. type DiscoveryService interface {
  2. Initialize(string, int) error
  3. Fetch() ([]string, error)
  4. Watch(WatchCallback)
  5. Register(string) error
  6. }

初始化(Initialize)

这些参数是无需schema和心跳的定位。

Fetch

从discovery返回节点列表。

Watch

触发更新(Fetch)可以通过定时器(例如token)或者使用后台某些特点(如 etcd)实现。

Register

给发现服务添加新节点。