diff --git a/client/client.go b/client/client.go index de7f10a31..bd82bb33d 100644 --- a/client/client.go +++ b/client/client.go @@ -1261,18 +1261,26 @@ func (c *Client) setupConsulSyncer() error { dcs = append([]string{nearestDC}, otherDCs...) } + // Forward RPCs to our region + nomadRPCArgs := structs.GenericRequest{ + QueryOptions: structs.QueryOptions{ + Region: c.Region(), + }, + } + nomadServerServiceName := c.config.ConsulConfig.ServerServiceName var mErr multierror.Error const defaultMaxNumNomadServers = 8 nomadServerServices := make([]string, 0, defaultMaxNumNomadServers) + c.logger.Printf("[DEBUG] client.consul: bootstrap contacting following Consul DCs: %q", dcs) for _, dc := range dcs { - opts := &consulapi.QueryOptions{ + consulOpts := &consulapi.QueryOptions{ AllowStale: true, Datacenter: dc, Near: "_agent", WaitTime: consul.DefaultQueryWaitDuration, } - consulServices, _, err := consulCatalog.Service(nomadServerServiceName, consul.ServiceTagRPC, opts) + consulServices, _, err := consulCatalog.Service(nomadServerServiceName, consul.ServiceTagRPC, consulOpts) if err != nil { mErr.Errors = append(mErr.Errors, fmt.Errorf("unable to query service %+q from Consul datacenter %+q: %v", nomadServerServiceName, dc, err)) continue @@ -1290,13 +1298,16 @@ func (c *Client) setupConsulSyncer() error { mErr.Errors = append(mErr.Errors, err) continue } - var ok bool - if ok, err = c.connPool.PingNomadServer(c.Region(), c.RPCMajorVersion(), serverEndpoint); err != nil { + var peers []string + if err := c.connPool.RPC(c.Region(), serverEndpoint.Addr, c.RPCMajorVersion(), "Status.Peers", nomadRPCArgs, &peers); err != nil { mErr.Errors = append(mErr.Errors, err) continue } - if ok { - nomadServerServices = append(nomadServerServices, serverAddr) + // Successfully received the Server peers list of the correct + // region + if len(peers) != 0 { + nomadServerServices = append(nomadServerServices, peers...) + break } } // Break if at least one Nomad Server was successfully pinged @@ -1309,12 +1320,12 @@ func (c *Client) setupConsulSyncer() error { return mErr.ErrorOrNil() } - for i := range dcs { - dcs[i] = fmt.Sprintf("%+q", dcs[i]) - } - return fmt.Errorf("no Nomad Servers advertising service %+q in Consul datacenters: %+q", nomadServerServiceName, dcs) + return fmt.Errorf("no Nomad Servers advertising service %q in Consul datacenters: %q", nomadServerServiceName, dcs) } + // Log the servers we are adding + c.logger.Printf("[DEBUG] client.consul: bootstrap adding following Servers: %q", nomadServerServices) + c.heartbeatLock.Lock() if atomic.LoadInt32(&c.lastHeartbeatFromQuorum) == 1 && now.Before(c.consulPullHeartbeatDeadline) { c.heartbeatLock.Unlock() diff --git a/demo/vagrant/client1.hcl b/demo/vagrant/client1.hcl index c8c311630..ace6412b7 100644 --- a/demo/vagrant/client1.hcl +++ b/demo/vagrant/client1.hcl @@ -6,6 +6,8 @@ data_dir = "/tmp/client1" enable_debug = true +name = "client1" + # Enable the client client { enabled = true diff --git a/demo/vagrant/client1_consul_bootstrap.hcl b/demo/vagrant/client1_consul_bootstrap.hcl new file mode 100644 index 000000000..c2a595150 --- /dev/null +++ b/demo/vagrant/client1_consul_bootstrap.hcl @@ -0,0 +1,30 @@ +# Increase log verbosity +log_level = "DEBUG" + +# Setup data dir +data_dir = "/tmp/client1" + +enable_debug = true + +name = "client1" + +# Enable the client +client { + enabled = true + + # For demo assume we are talking to server1. For production, + # this should be like "nomad.service.consul:4647" and a system + # like Consul used for service discovery. + node_class = "foo" + options { + "driver.raw_exec.enable" = "1" + } + reserved { + cpu = 500 + } +} + +# Modify our port to avoid a collision with server1 +ports { + http = 5656 +}