2014-02-08 00:41:03 +00:00
|
|
|
---
|
2020-04-07 18:55:19 +00:00
|
|
|
layout: intro
|
|
|
|
page_title: Run the Agent
|
2015-12-27 15:45:59 +00:00
|
|
|
description: >
|
2020-04-07 18:55:19 +00:00
|
|
|
The Consul agent can run in either server or client mode. Each datacenter must
|
|
|
|
have at least one server, though a cluster of 3 or 5 servers is recommended. A
|
|
|
|
single server deployment is highly discouraged in production as data loss is
|
|
|
|
inevitable in a failure scenario.
|
2014-02-08 00:41:03 +00:00
|
|
|
---
|
|
|
|
|
2014-04-11 00:41:49 +00:00
|
|
|
# Run the Consul Agent
|
|
|
|
|
2014-11-26 11:37:28 +00:00
|
|
|
After Consul is installed, the agent must be run. The agent can run either
|
2015-02-19 22:32:31 +00:00
|
|
|
in server or client mode. Each datacenter must have at least one server,
|
2015-03-02 13:50:13 +00:00
|
|
|
though a cluster of 3 or 5 servers is recommended. A single server deployment
|
|
|
|
is _**highly**_ discouraged as data loss is inevitable in a failure scenario.
|
2015-02-19 22:12:56 +00:00
|
|
|
|
2015-03-02 13:50:13 +00:00
|
|
|
All other agents run in client mode. A client is a very lightweight
|
2015-02-19 22:12:56 +00:00
|
|
|
process that registers services, runs health checks, and forwards queries to
|
2017-07-14 05:30:10 +00:00
|
|
|
servers. The agent must be running on every node that is part of the cluster.
|
2014-02-08 00:41:03 +00:00
|
|
|
|
2015-03-02 13:50:13 +00:00
|
|
|
For more detail on bootstrapping a datacenter, see
|
2020-04-09 23:46:54 +00:00
|
|
|
[this guide](/docs/guides/bootstrapping).
|
2015-03-02 13:50:13 +00:00
|
|
|
|
2014-02-08 00:41:03 +00:00
|
|
|
## Starting the Agent
|
|
|
|
|
2015-12-27 15:45:59 +00:00
|
|
|
For simplicity, we'll start the Consul agent in development mode for now. This
|
|
|
|
mode is useful for bringing up a single-node Consul environment quickly and
|
|
|
|
easily. It is **not** intended to be used in production as it does not persist
|
|
|
|
any state.
|
2014-02-08 00:41:03 +00:00
|
|
|
|
2014-10-19 23:40:10 +00:00
|
|
|
```text
|
2016-09-15 14:37:08 +00:00
|
|
|
-$ consul agent -dev
|
2014-04-11 00:41:49 +00:00
|
|
|
==> Starting Consul agent...
|
|
|
|
==> Starting Consul agent RPC...
|
|
|
|
==> Consul agent running!
|
2016-09-15 14:25:23 +00:00
|
|
|
Version: 'v0.7.0'
|
2015-12-27 15:45:59 +00:00
|
|
|
Node name: 'Armons-MacBook-Air'
|
|
|
|
Datacenter: 'dc1'
|
|
|
|
Server: true (bootstrap: false)
|
|
|
|
Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)
|
2016-09-15 14:25:23 +00:00
|
|
|
Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
|
2015-12-27 15:45:59 +00:00
|
|
|
Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false
|
2014-02-08 00:41:03 +00:00
|
|
|
|
|
|
|
==> Log data will now stream in as it occurs:
|
|
|
|
|
2016-09-15 14:25:23 +00:00
|
|
|
2016/09/15 10:21:10 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:127.0.0.1:8300 Address:127.0.0.1:8300}]
|
|
|
|
2016/09/15 10:21:10 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")
|
|
|
|
2016/09/15 10:21:10 [INFO] serf: EventMemberJoin: Armons-MacBook-Air 127.0.0.1
|
|
|
|
2016/09/15 10:21:10 [INFO] serf: EventMemberJoin: Armons-MacBook-Air.dc1 127.0.0.1
|
|
|
|
2016/09/15 10:21:10 [INFO] consul: Adding LAN server Armons-MacBook-Air (Addr: tcp/127.0.0.1:8300) (DC: dc1)
|
|
|
|
2016/09/15 10:21:10 [INFO] consul: Adding WAN server Armons-MacBook-Air.dc1 (Addr: tcp/127.0.0.1:8300) (DC: dc1)
|
|
|
|
2016/09/15 10:21:13 [DEBUG] http: Request GET /v1/agent/services (180.708µs) from=127.0.0.1:52369
|
|
|
|
2016/09/15 10:21:13 [DEBUG] http: Request GET /v1/agent/services (15.548µs) from=127.0.0.1:52369
|
|
|
|
2016/09/15 10:21:17 [WARN] raft: Heartbeat timeout from "" reached, starting election
|
|
|
|
2016/09/15 10:21:17 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2
|
|
|
|
2016/09/15 10:21:17 [DEBUG] raft: Votes needed: 1
|
|
|
|
2016/09/15 10:21:17 [DEBUG] raft: Vote granted from 127.0.0.1:8300 in term 2. Tally: 1
|
|
|
|
2016/09/15 10:21:17 [INFO] raft: Election won. Tally: 1
|
|
|
|
2016/09/15 10:21:17 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
|
|
|
|
2016/09/15 10:21:17 [INFO] consul: cluster leadership acquired
|
|
|
|
2016/09/15 10:21:17 [DEBUG] consul: reset tombstone GC to index 3
|
|
|
|
2016/09/15 10:21:17 [INFO] consul: New leader elected: Armons-MacBook-Air
|
|
|
|
2016/09/15 10:21:17 [INFO] consul: member 'Armons-MacBook-Air' joined, marking health alive
|
|
|
|
2016/09/15 10:21:17 [INFO] agent: Synced service 'consul'
|
2014-02-08 00:41:03 +00:00
|
|
|
```
|
|
|
|
|
2014-04-11 00:41:49 +00:00
|
|
|
As you can see, the Consul agent has started and has output some log
|
2015-02-19 22:12:56 +00:00
|
|
|
data. From the log data, you can see that our agent is running in server mode
|
2014-04-11 00:41:49 +00:00
|
|
|
and has claimed leadership of the cluster. Additionally, the local member has
|
|
|
|
been marked as a healthy member of the cluster.
|
2014-02-08 00:41:03 +00:00
|
|
|
|
2014-10-19 23:40:10 +00:00
|
|
|
~> **Note for OS X Users:** Consul uses your hostname as the
|
2014-04-17 16:20:51 +00:00
|
|
|
default node name. If your hostname contains periods, DNS queries to
|
|
|
|
that node will not work with Consul. To avoid this, explicitly set
|
2014-10-19 23:40:10 +00:00
|
|
|
the name of your node with the `-node` flag.
|
2014-04-17 16:17:54 +00:00
|
|
|
|
2014-02-08 00:41:03 +00:00
|
|
|
## Cluster Members
|
|
|
|
|
2020-04-09 23:46:54 +00:00
|
|
|
If you run [`consul members`](/docs/commands/members) in another terminal, you
|
2015-03-02 13:50:13 +00:00
|
|
|
can see the members of the Consul cluster. We'll cover joining clusters in the next
|
|
|
|
section, but for now, you should only see one member (yourself):
|
2014-02-08 00:41:03 +00:00
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
```shell-session
|
2014-04-11 00:41:49 +00:00
|
|
|
$ consul members
|
2015-12-27 15:45:59 +00:00
|
|
|
Node Address Status Type Build Protocol DC
|
|
|
|
Armons-MacBook-Air 172.20.20.11:8301 alive server 0.6.1dev 2 dc1
|
2014-02-08 00:41:03 +00:00
|
|
|
```
|
|
|
|
|
2014-04-14 19:22:03 +00:00
|
|
|
The output shows our own node, the address it is running on, its
|
2015-02-19 22:12:56 +00:00
|
|
|
health state, its role in the cluster, and some version information.
|
2014-07-01 22:02:26 +00:00
|
|
|
Additional metadata can be viewed by providing the `-detailed` flag.
|
2014-02-08 00:41:03 +00:00
|
|
|
|
2020-04-09 23:46:54 +00:00
|
|
|
The output of the [`members`](/docs/commands/members) command is based on
|
|
|
|
the [gossip protocol](/docs/internals/gossip) and is eventually consistent.
|
2015-02-19 22:12:56 +00:00
|
|
|
That is, at any point in time, the view of the world as seen by your local
|
|
|
|
agent may not exactly match the state on the servers. For a strongly consistent
|
2020-04-09 23:20:00 +00:00
|
|
|
view of the world, use the [HTTP API](/api) as it forwards the
|
2015-02-19 22:18:58 +00:00
|
|
|
request to the Consul servers:
|
2014-04-11 00:41:49 +00:00
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
```shell-session
|
2014-04-11 00:41:49 +00:00
|
|
|
$ curl localhost:8500/v1/catalog/nodes
|
2016-09-15 14:25:23 +00:00
|
|
|
[{"Node":"Armons-MacBook-Air","Address":"127.0.0.1","TaggedAddresses":{"lan":"127.0.0.1","wan":"127.0.0.1"},"CreateIndex":4,"ModifyIndex":110}]
|
2014-04-11 00:41:49 +00:00
|
|
|
```
|
|
|
|
|
2020-04-09 23:46:54 +00:00
|
|
|
In addition to the HTTP API, the [DNS interface](/docs/agent/dns) can
|
2015-03-02 13:50:13 +00:00
|
|
|
be used to query the node. Note that you have to make sure to point your DNS
|
|
|
|
lookups to the Consul agent's DNS server which runs on port 8600 by default.
|
|
|
|
The format of the DNS entries (such as "Armons-MacBook-Air.node.consul") will
|
|
|
|
be covered in more detail later.
|
2014-04-11 00:41:49 +00:00
|
|
|
|
2020-05-19 18:32:38 +00:00
|
|
|
```shell-session
|
2014-04-11 00:41:49 +00:00
|
|
|
$ dig @127.0.0.1 -p 8600 Armons-MacBook-Air.node.consul
|
2014-04-14 19:22:03 +00:00
|
|
|
...
|
2014-04-11 00:41:49 +00:00
|
|
|
|
|
|
|
;; QUESTION SECTION:
|
|
|
|
;Armons-MacBook-Air.node.consul. IN A
|
|
|
|
|
|
|
|
;; ANSWER SECTION:
|
2016-09-15 14:25:23 +00:00
|
|
|
Armons-MacBook-Air.node.consul. 0 IN A 127.0.0.1
|
2014-04-11 00:41:49 +00:00
|
|
|
```
|
|
|
|
|
2015-03-14 18:13:41 +00:00
|
|
|
## <a name="stopping"></a>Stopping the Agent
|
2014-02-08 00:41:03 +00:00
|
|
|
|
|
|
|
You can use `Ctrl-C` (the interrupt signal) to gracefully halt the agent.
|
2015-02-19 22:12:56 +00:00
|
|
|
After interrupting the agent, you should see it leave the cluster
|
2014-02-08 00:41:03 +00:00
|
|
|
and shut down.
|
|
|
|
|
2014-04-11 00:41:49 +00:00
|
|
|
By gracefully leaving, Consul notifies other cluster members that the
|
2014-02-08 00:41:03 +00:00
|
|
|
node _left_. If you had forcibly killed the agent process, other members
|
2014-04-11 00:41:49 +00:00
|
|
|
of the cluster would have detected that the node _failed_. When a member leaves,
|
2014-05-03 22:23:16 +00:00
|
|
|
its services and checks are removed from the catalog. When a member fails,
|
2014-11-26 11:37:28 +00:00
|
|
|
its health is simply marked as critical, but it is not removed from the catalog.
|
2015-02-19 22:12:56 +00:00
|
|
|
Consul will automatically try to reconnect to _failed_ nodes, allowing it
|
2014-04-11 00:41:49 +00:00
|
|
|
to recover from certain network conditions, while _left_ nodes are no longer contacted.
|
|
|
|
|
|
|
|
Additionally, if an agent is operating as a server, a graceful leave is important
|
2015-03-02 13:50:13 +00:00
|
|
|
to avoid causing a potential availability outage affecting the
|
2020-04-09 23:46:54 +00:00
|
|
|
[consensus protocol](/docs/internals/consensus). See the
|
2020-04-09 23:20:00 +00:00
|
|
|
[guides section](/docs/guides) for details on how to safely add
|
2014-11-26 11:37:28 +00:00
|
|
|
and remove servers.
|
2015-03-02 13:50:13 +00:00
|
|
|
|
|
|
|
## Next Steps
|
|
|
|
|
2015-03-14 18:13:41 +00:00
|
|
|
Your simple Consul cluster is up and running. Let's give it some
|
2020-04-09 23:46:54 +00:00
|
|
|
[services](/intro/getting-started/services)!
|