Docker Swarm
的join
命令的定义:
{
Name: "join",
ShortName: "j",
Usage: "Join a docker cluster",
Flags: []cli.Flag{flJoinAdvertise, flHeartBeat, flTTL, flJoinRandomDelay, flDiscoveryOpt},
Action: join,
},
flHeartBeat
的默认值是60s
,而flTTL
默认值是180s
:
flHeartBeat = cli.StringFlag{
Name: "heartbeat",
Value: "60s",
Usage: "period between each heartbeat",
}
flTTL = cli.StringFlag{
Name: "ttl",
Value: "180s",
Usage: "set the expiration of an ephemeral node",
}
join
函数的核心代码:
......
for {
log.WithFields(log.Fields{"addr": addr, "discovery": dflag}).Infof("Registering on the discovery service every %s...", hb)
if err := d.Register(addr); err != nil {
log.Error(err)
}
time.Sleep(hb)
}
......
token.Register
函数实现:
func (s *Discovery) Register(addr string) error {
buf := strings.NewReader(addr)
resp, err := http.Post(fmt.Sprintf("%s/%s/%s?ttl=%d", s.url,
"clusters", s.token, uint64(s.ttl.Seconds())), "application/json", buf)
if err != nil {
return err
}
resp.Body.Close()
return nil
}
join
命令其实就是每隔heartbeat
时间(例如,60s
),向https://discovery.hub.docker.com/v1/clusters/token/ttl=180
(ttl
取默认值),注册一下当前Docker
的地址(IP:PORT
)。
有疑问加站长微信联系(非本文作者)