github.com/hspak/nomad@v0.7.2-0.20180309000617-bc4ae22a39a5/nomad/config.go (about) 1 package nomad 2 3 import ( 4 "fmt" 5 "io" 6 "net" 7 "os" 8 "runtime" 9 "time" 10 11 "github.com/hashicorp/memberlist" 12 "github.com/hashicorp/nomad/helper/tlsutil" 13 "github.com/hashicorp/nomad/helper/uuid" 14 "github.com/hashicorp/nomad/nomad/structs" 15 "github.com/hashicorp/nomad/nomad/structs/config" 16 "github.com/hashicorp/nomad/scheduler" 17 "github.com/hashicorp/raft" 18 "github.com/hashicorp/serf/serf" 19 ) 20 21 const ( 22 DefaultRegion = "global" 23 DefaultDC = "dc1" 24 DefaultSerfPort = 4648 25 ) 26 27 // These are the protocol versions that Nomad can understand 28 const ( 29 ProtocolVersionMin uint8 = 1 30 ProtocolVersionMax = 1 31 ) 32 33 // ProtocolVersionMap is the mapping of Nomad protocol versions 34 // to Serf protocol versions. We mask the Serf protocols using 35 // our own protocol version. 36 var protocolVersionMap map[uint8]uint8 37 38 func init() { 39 protocolVersionMap = map[uint8]uint8{ 40 1: 4, 41 } 42 } 43 44 var ( 45 DefaultRPCAddr = &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 4647} 46 ) 47 48 // Config is used to parameterize the server 49 type Config struct { 50 // Bootstrap mode is used to bring up the first Nomad server. It is 51 // required so that it can elect a leader without any other nodes 52 // being present 53 Bootstrap bool 54 55 // BootstrapExpect mode is used to automatically bring up a 56 // collection of Nomad servers. This can be used to automatically 57 // bring up a collection of nodes. All operations on BootstrapExpect 58 // must be handled via `atomic.*Int32()` calls. 59 BootstrapExpect int32 60 61 // DataDir is the directory to store our state in 62 DataDir string 63 64 // DevMode is used for development purposes only and limits the 65 // use of persistence or state. 66 DevMode bool 67 68 // DevDisableBootstrap is used to disable bootstrap mode while 69 // in DevMode. This is largely used for testing. 70 DevDisableBootstrap bool 71 72 // LogOutput is the location to write logs to. If this is not set, 73 // logs will go to stderr. 74 LogOutput io.Writer 75 76 // ProtocolVersion is the protocol version to speak. This must be between 77 // ProtocolVersionMin and ProtocolVersionMax. 78 ProtocolVersion uint8 79 80 // RPCAddr is the RPC address used by Nomad. This should be reachable 81 // by the other servers and clients 82 RPCAddr *net.TCPAddr 83 84 // RPCAdvertise is the address that is advertised to other nodes for 85 // the RPC endpoint. This can differ from the RPC address, if for example 86 // the RPCAddr is unspecified "0.0.0.0:4646", but this address must be 87 // reachable 88 RPCAdvertise *net.TCPAddr 89 90 // RaftConfig is the configuration used for Raft in the local DC 91 RaftConfig *raft.Config 92 93 // RaftTimeout is applied to any network traffic for raft. Defaults to 10s. 94 RaftTimeout time.Duration 95 96 // (Enterprise-only) NonVoter is used to prevent this server from being added 97 // as a voting member of the Raft cluster. 98 NonVoter bool 99 100 // (Enterprise-only) RedundancyZone is the redundancy zone to use for this server. 101 RedundancyZone string 102 103 // (Enterprise-only) UpgradeVersion is the custom upgrade version to use when 104 // performing upgrade migrations. 105 UpgradeVersion string 106 107 // SerfConfig is the configuration for the serf cluster 108 SerfConfig *serf.Config 109 110 // Node name is the name we use to advertise. Defaults to hostname. 111 NodeName string 112 113 // NodeID is the uuid of this server. 114 NodeID string 115 116 // Region is the region this Nomad server belongs to. 117 Region string 118 119 // AuthoritativeRegion is the region which is treated as the authoritative source 120 // for ACLs and Policies. This provides a single source of truth to resolve conflicts. 121 AuthoritativeRegion string 122 123 // Datacenter is the datacenter this Nomad server belongs to. 124 Datacenter string 125 126 // Build is a string that is gossiped around, and can be used to help 127 // operators track which versions are actively deployed 128 Build string 129 130 // NumSchedulers is the number of scheduler thread that are run. 131 // This can be as many as one per core, or zero to disable this server 132 // from doing any scheduling work. 133 NumSchedulers int 134 135 // EnabledSchedulers controls the set of sub-schedulers that are 136 // enabled for this server to handle. This will restrict the evaluations 137 // that the workers dequeue for processing. 138 EnabledSchedulers []string 139 140 // ReconcileInterval controls how often we reconcile the strongly 141 // consistent store with the Serf info. This is used to handle nodes 142 // that are force removed, as well as intermittent unavailability during 143 // leader election. 144 ReconcileInterval time.Duration 145 146 // EvalGCInterval is how often we dispatch a job to GC evaluations 147 EvalGCInterval time.Duration 148 149 // EvalGCThreshold is how "old" an evaluation must be to be eligible 150 // for GC. This gives users some time to debug a failed evaluation. 151 EvalGCThreshold time.Duration 152 153 // JobGCInterval is how often we dispatch a job to GC jobs that are 154 // available for garbage collection. 155 JobGCInterval time.Duration 156 157 // JobGCThreshold is how old a job must be before it eligible for GC. This gives 158 // the user time to inspect the job. 159 JobGCThreshold time.Duration 160 161 // NodeGCInterval is how often we dispatch a job to GC failed nodes. 162 NodeGCInterval time.Duration 163 164 // NodeGCThreshold is how "old" a node must be to be eligible 165 // for GC. This gives users some time to view and debug a failed nodes. 166 NodeGCThreshold time.Duration 167 168 // DeploymentGCInterval is how often we dispatch a job to GC terminal 169 // deployments. 170 DeploymentGCInterval time.Duration 171 172 // DeploymentGCThreshold is how "old" a deployment must be to be eligible 173 // for GC. This gives users some time to view terminal deployments. 174 DeploymentGCThreshold time.Duration 175 176 // EvalNackTimeout controls how long we allow a sub-scheduler to 177 // work on an evaluation before we consider it failed and Nack it. 178 // This allows that evaluation to be handed to another sub-scheduler 179 // to work on. Defaults to 60 seconds. This should be long enough that 180 // no evaluation hits it unless the sub-scheduler has failed. 181 EvalNackTimeout time.Duration 182 183 // EvalDeliveryLimit is the limit of attempts we make to deliver and 184 // process an evaluation. This is used so that an eval that will never 185 // complete eventually fails out of the system. 186 EvalDeliveryLimit int 187 188 // EvalNackInitialReenqueueDelay is the delay applied before reenqueuing a 189 // Nacked evaluation for the first time. This value should be small as the 190 // initial Nack can be due to a down machine and the eval should be retried 191 // quickly for liveliness. 192 EvalNackInitialReenqueueDelay time.Duration 193 194 // EvalNackSubsequentReenqueueDelay is the delay applied before reenqueuing 195 // an evaluation that has been Nacked more than once. This delay is 196 // compounding after the first Nack. This value should be significantly 197 // longer than the initial delay as the purpose it severs is to apply 198 // back-pressure as evaluatiions are being Nacked either due to scheduler 199 // failures or because they are hitting their Nack timeout, both of which 200 // are signs of high server resource usage. 201 EvalNackSubsequentReenqueueDelay time.Duration 202 203 // EvalFailedFollowupBaselineDelay is the minimum time waited before 204 // retrying a failed evaluation. 205 EvalFailedFollowupBaselineDelay time.Duration 206 207 // EvalFailedFollowupDelayRange defines the range of additional time from 208 // the baseline in which to wait before retrying a failed evaluation. The 209 // additional delay is selected from this range randomly. 210 EvalFailedFollowupDelayRange time.Duration 211 212 // MinHeartbeatTTL is the minimum time between heartbeats. 213 // This is used as a floor to prevent excessive updates. 214 MinHeartbeatTTL time.Duration 215 216 // MaxHeartbeatsPerSecond is the maximum target rate of heartbeats 217 // being processed per second. This allows the TTL to be increased 218 // to meet the target rate. 219 MaxHeartbeatsPerSecond float64 220 221 // HeartbeatGrace is the additional time given as a grace period 222 // beyond the TTL to account for network and processing delays 223 // as well as clock skew. 224 HeartbeatGrace time.Duration 225 226 // FailoverHeartbeatTTL is the TTL applied to heartbeats after 227 // a new leader is elected, since we no longer know the status 228 // of all the heartbeats. 229 FailoverHeartbeatTTL time.Duration 230 231 // ConsulConfig is this Agent's Consul configuration 232 ConsulConfig *config.ConsulConfig 233 234 // VaultConfig is this Agent's Vault configuration 235 VaultConfig *config.VaultConfig 236 237 // RPCHoldTimeout is how long an RPC can be "held" before it is errored. 238 // This is used to paper over a loss of leadership by instead holding RPCs, 239 // so that the caller experiences a slow response rather than an error. 240 // This period is meant to be long enough for a leader election to take 241 // place, and a small jitter is applied to avoid a thundering herd. 242 RPCHoldTimeout time.Duration 243 244 // TLSConfig holds various TLS related configurations 245 TLSConfig *config.TLSConfig 246 247 // ACLEnabled controls if ACL enforcement and management is enabled. 248 ACLEnabled bool 249 250 // ReplicationBackoff is how much we backoff when replication errors. 251 // This is a tunable knob for testing primarily. 252 ReplicationBackoff time.Duration 253 254 // ReplicationToken is the ACL Token Secret ID used to fetch from 255 // the Authoritative Region. 256 ReplicationToken string 257 258 // SentinelGCInterval is the interval that we GC unused policies. 259 SentinelGCInterval time.Duration 260 261 // SentinelConfig is this Agent's Sentinel configuration 262 SentinelConfig *config.SentinelConfig 263 264 // StatsCollectionInterval is the interval at which the Nomad server 265 // publishes metrics which are periodic in nature like updating gauges 266 StatsCollectionInterval time.Duration 267 268 // DisableTaggedMetrics determines whether metrics will be displayed via a 269 // key/value/tag format, or simply a key/value format 270 DisableTaggedMetrics bool 271 272 // BackwardsCompatibleMetrics determines whether to show methods of 273 // displaying metrics for older verions, or to only show the new format 274 BackwardsCompatibleMetrics bool 275 276 // AutopilotConfig is used to apply the initial autopilot config when 277 // bootstrapping. 278 AutopilotConfig *structs.AutopilotConfig 279 280 // ServerHealthInterval is the frequency with which the health of the 281 // servers in the cluster will be updated. 282 ServerHealthInterval time.Duration 283 284 // AutopilotInterval is the frequency with which the leader will perform 285 // autopilot tasks, such as promoting eligible non-voters and removing 286 // dead servers. 287 AutopilotInterval time.Duration 288 } 289 290 // CheckVersion is used to check if the ProtocolVersion is valid 291 func (c *Config) CheckVersion() error { 292 if c.ProtocolVersion < ProtocolVersionMin { 293 return fmt.Errorf("Protocol version '%d' too low. Must be in range: [%d, %d]", 294 c.ProtocolVersion, ProtocolVersionMin, ProtocolVersionMax) 295 } else if c.ProtocolVersion > ProtocolVersionMax { 296 return fmt.Errorf("Protocol version '%d' too high. Must be in range: [%d, %d]", 297 c.ProtocolVersion, ProtocolVersionMin, ProtocolVersionMax) 298 } 299 return nil 300 } 301 302 // DefaultConfig returns the default configuration 303 func DefaultConfig() *Config { 304 hostname, err := os.Hostname() 305 if err != nil { 306 panic(err) 307 } 308 309 c := &Config{ 310 Region: DefaultRegion, 311 AuthoritativeRegion: DefaultRegion, 312 Datacenter: DefaultDC, 313 NodeName: hostname, 314 NodeID: uuid.Generate(), 315 ProtocolVersion: ProtocolVersionMax, 316 RaftConfig: raft.DefaultConfig(), 317 RaftTimeout: 10 * time.Second, 318 LogOutput: os.Stderr, 319 RPCAddr: DefaultRPCAddr, 320 SerfConfig: serf.DefaultConfig(), 321 NumSchedulers: 1, 322 ReconcileInterval: 60 * time.Second, 323 EvalGCInterval: 5 * time.Minute, 324 EvalGCThreshold: 1 * time.Hour, 325 JobGCInterval: 5 * time.Minute, 326 JobGCThreshold: 4 * time.Hour, 327 NodeGCInterval: 5 * time.Minute, 328 NodeGCThreshold: 24 * time.Hour, 329 DeploymentGCInterval: 5 * time.Minute, 330 DeploymentGCThreshold: 1 * time.Hour, 331 EvalNackTimeout: 60 * time.Second, 332 EvalDeliveryLimit: 3, 333 EvalNackInitialReenqueueDelay: 1 * time.Second, 334 EvalNackSubsequentReenqueueDelay: 20 * time.Second, 335 EvalFailedFollowupBaselineDelay: 1 * time.Minute, 336 EvalFailedFollowupDelayRange: 5 * time.Minute, 337 MinHeartbeatTTL: 10 * time.Second, 338 MaxHeartbeatsPerSecond: 50.0, 339 HeartbeatGrace: 10 * time.Second, 340 FailoverHeartbeatTTL: 300 * time.Second, 341 ConsulConfig: config.DefaultConsulConfig(), 342 VaultConfig: config.DefaultVaultConfig(), 343 RPCHoldTimeout: 5 * time.Second, 344 StatsCollectionInterval: 1 * time.Minute, 345 TLSConfig: &config.TLSConfig{}, 346 ReplicationBackoff: 30 * time.Second, 347 SentinelGCInterval: 30 * time.Second, 348 AutopilotConfig: &structs.AutopilotConfig{ 349 CleanupDeadServers: true, 350 LastContactThreshold: 200 * time.Millisecond, 351 MaxTrailingLogs: 250, 352 ServerStabilizationTime: 10 * time.Second, 353 }, 354 ServerHealthInterval: 2 * time.Second, 355 AutopilotInterval: 10 * time.Second, 356 } 357 358 // Enable all known schedulers by default 359 c.EnabledSchedulers = make([]string, 0, len(scheduler.BuiltinSchedulers)) 360 for name := range scheduler.BuiltinSchedulers { 361 c.EnabledSchedulers = append(c.EnabledSchedulers, name) 362 } 363 c.EnabledSchedulers = append(c.EnabledSchedulers, structs.JobTypeCore) 364 365 // Default the number of schedulers to match the coores 366 c.NumSchedulers = runtime.NumCPU() 367 368 // Increase our reap interval to 3 days instead of 24h. 369 c.SerfConfig.ReconnectTimeout = 3 * 24 * time.Hour 370 371 // Serf should use the WAN timing, since we are using it 372 // to communicate between DC's 373 c.SerfConfig.MemberlistConfig = memberlist.DefaultWANConfig() 374 c.SerfConfig.MemberlistConfig.BindPort = DefaultSerfPort 375 376 // Disable shutdown on removal 377 c.RaftConfig.ShutdownOnRemove = false 378 379 // Enable interoperability with new raft APIs, requires all servers 380 // to be on raft v1 or higher. 381 c.RaftConfig.ProtocolVersion = 2 382 383 return c 384 } 385 386 // tlsConfig returns a TLSUtil Config based on the server configuration 387 func (c *Config) tlsConfig() *tlsutil.Config { 388 return &tlsutil.Config{ 389 VerifyIncoming: true, 390 VerifyOutgoing: true, 391 VerifyServerHostname: c.TLSConfig.VerifyServerHostname, 392 CAFile: c.TLSConfig.CAFile, 393 CertFile: c.TLSConfig.CertFile, 394 KeyFile: c.TLSConfig.KeyFile, 395 KeyLoader: c.TLSConfig.GetKeyLoader(), 396 } 397 }