Adding support for advertise address

This commit is contained in:
Armon Dadgar 2013-12-31 16:45:13 -08:00
parent 3f6f9cc33a
commit 93dac80a86
5 changed files with 56 additions and 15 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/hashicorp/serf/serf" "github.com/hashicorp/serf/serf"
"io" "io"
"log" "log"
"net"
"os" "os"
"sync" "sync"
) )
@ -53,6 +54,24 @@ func Create(config *Config, logOutput io.Writer) (*Agent, error) {
return nil, fmt.Errorf("Must configure a DataDir") return nil, fmt.Errorf("Must configure a DataDir")
} }
// Ensure the RPC Addr is sane
if _, err := net.ResolveTCPAddr("tcp", config.ServerAddr); err != nil {
return nil, fmt.Errorf("Bad server address: %v", err)
}
// Try to get an advertise address
if config.AdvertiseAddr != "" {
if ip := net.ParseIP(config.AdvertiseAddr); ip == nil {
return nil, fmt.Errorf("Failed to parse advertise address: %v", config.AdvertiseAddr)
}
} else {
ip, err := consul.GetPrivateIP()
if err != nil {
return nil, fmt.Errorf("Failed to get advertise address: %v", err)
}
config.AdvertiseAddr = ip.IP.String()
}
agent := &Agent{ agent := &Agent{
config: config, config: config,
logger: log.New(logOutput, "", log.LstdFlags), logger: log.New(logOutput, "", log.LstdFlags),
@ -105,12 +124,23 @@ func (a *Agent) consulConfig() *consul.Config {
} }
if a.config.SerfLanPort != 0 { if a.config.SerfLanPort != 0 {
base.SerfLANConfig.MemberlistConfig.BindPort = a.config.SerfLanPort base.SerfLANConfig.MemberlistConfig.BindPort = a.config.SerfLanPort
base.SerfLANConfig.MemberlistConfig.AdvertisePort = a.config.SerfLanPort
} }
if a.config.SerfWanPort != 0 { if a.config.SerfWanPort != 0 {
base.SerfWANConfig.MemberlistConfig.BindPort = a.config.SerfWanPort base.SerfWANConfig.MemberlistConfig.BindPort = a.config.SerfWanPort
base.SerfWANConfig.MemberlistConfig.AdvertisePort = a.config.SerfWanPort
} }
if a.config.ServerAddr != "" { if a.config.ServerAddr != "" {
base.RPCAddr = a.config.ServerAddr addr, _ := net.ResolveTCPAddr("tcp", a.config.ServerAddr)
base.RPCAddr = addr
}
if a.config.AdvertiseAddr != "" {
base.SerfLANConfig.MemberlistConfig.AdvertiseAddr = a.config.AdvertiseAddr
base.SerfWANConfig.MemberlistConfig.AdvertiseAddr = a.config.AdvertiseAddr
base.RPCAdvertise = &net.TCPAddr{
IP: net.ParseIP(a.config.AdvertiseAddr),
Port: base.RPCAddr.Port,
}
} }
if a.config.Bootstrap { if a.config.Bootstrap {
base.Bootstrap = true base.Bootstrap = true

View File

@ -185,10 +185,11 @@ func (c *Command) Run(args []string) int {
c.Ui.Output("Consul agent running!") c.Ui.Output("Consul agent running!")
c.Ui.Info(fmt.Sprintf(" Node name: '%s'", config.NodeName)) c.Ui.Info(fmt.Sprintf(" Node name: '%s'", config.NodeName))
c.Ui.Info(fmt.Sprintf(" Datacenter: '%s'", config.Datacenter)) c.Ui.Info(fmt.Sprintf(" Datacenter: '%s'", config.Datacenter))
c.Ui.Info(fmt.Sprintf("Advertise addr: '%s'", config.AdvertiseAddr))
c.Ui.Info(fmt.Sprintf(" RPC addr: '%s'", config.RPCAddr)) c.Ui.Info(fmt.Sprintf(" RPC addr: '%s'", config.RPCAddr))
c.Ui.Info(fmt.Sprintf(" HTTP addr: '%s'", config.HTTPAddr)) c.Ui.Info(fmt.Sprintf(" HTTP addr: '%s'", config.HTTPAddr))
c.Ui.Info(fmt.Sprintf(" Encrypted: %#v", config.EncryptKey != "")) c.Ui.Info(fmt.Sprintf(" Encrypted: %#v", config.EncryptKey != ""))
c.Ui.Info(fmt.Sprintf(" Server: %v", config.Server)) c.Ui.Info(fmt.Sprintf(" Server: %v (bootstrap: %v)", config.Server, config.Bootstrap))
// Enable log streaming // Enable log streaming
c.Ui.Info("") c.Ui.Info("")

View File

@ -59,6 +59,11 @@ type Config struct {
// Defaults to 0.0.0.0:8300 // Defaults to 0.0.0.0:8300
ServerAddr string ServerAddr string
// AdvertiseAddr is the address we use for advertising our Serf,
// and Consul RPC IP. If not specified, the first private IP we
// find is used.
AdvertiseAddr string
// Server controls if this agent acts like a Consul server, // Server controls if this agent acts like a Consul server,
// or merely as a client. Servers have more state, take part // or merely as a client. Servers have more state, take part
// in leader election, etc. // in leader election, etc.
@ -85,6 +90,8 @@ func DefaultConfig() *Config {
HTTPAddr: "127.0.0.1:8500", HTTPAddr: "127.0.0.1:8500",
LogLevel: "INFO", LogLevel: "INFO",
RPCAddr: "127.0.0.1:8400", RPCAddr: "127.0.0.1:8400",
SerfLanPort: consul.DefaultLANSerfPort,
SerfWanPort: consul.DefaultWANSerfPort,
Server: false, Server: false,
} }
} }

View File

@ -11,11 +11,14 @@ import (
const ( const (
DefaultDC = "dc1" DefaultDC = "dc1"
DefaultRPCAddr = "0.0.0.0:8300"
DefaultLANSerfPort = 8301 DefaultLANSerfPort = 8301
DefaultWANSerfPort = 8302 DefaultWANSerfPort = 8302
) )
var (
DefaultRPCAddr = &net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: 8300}
)
// Config is used to configure the server // Config is used to configure the server
type Config struct { type Config struct {
// Bootstrap mode is used to bring up the first Consul server. // Bootstrap mode is used to bring up the first Consul server.
@ -37,7 +40,7 @@ type Config struct {
// RPCAddr is the RPC address used by Consul. This should be reachable // RPCAddr is the RPC address used by Consul. This should be reachable
// by the WAN and LAN // by the WAN and LAN
RPCAddr string RPCAddr *net.TCPAddr
// RPCAdvertise is the address that is advertised to other nodes for // RPCAdvertise is the address that is advertised to other nodes for
// the RPC endpoint. This can differ from the RPC address, if for example // the RPC endpoint. This can differ from the RPC address, if for example

View File

@ -228,7 +228,7 @@ func (s *Server) setupRPC() error {
s.rpcServer.Register(&Raft{server: s}) s.rpcServer.Register(&Raft{server: s})
s.rpcServer.Register(&Catalog{s}) s.rpcServer.Register(&Catalog{s})
list, err := net.Listen("tcp", s.config.RPCAddr) list, err := net.ListenTCP("tcp", s.config.RPCAddr)
if err != nil { if err != nil {
return err return err
} }