Consul is very simple to use. This article continues to introduce the usage of Consul Agent in an example way.
Preparation beforehand
liumiaocn:~ liumiao$ consul --version Consul v1.7.1 Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents) liumiaocn:~ liumiao$
Start Consul Agent
To be able to use Consul, you need to start the Consul Agent after installation. Execute the command as follows:
Execute command: consumer agent - Dev
liumiaocn:~ liumiao$ consul agent -dev ==> Starting Consul agent... Version: 'v1.7.1' Node ID: 'e95df364-000e-e590-6bd4-ceb947b8c90b' Node name: 'liumiaocn' Datacenter: 'dc1' (Segment: '<all>') Server: true (Bootstrap: false) Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600) Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302) Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-Encrypt-TLS: false
Detailed log information is as follows
==> Log data will now stream in as it occurs: 2020-02-29T06:58:17.094+0800 [DEBUG] agent: Using random ID as node ID: id=e95df364-000e-e590-6bd4-ceb947b8c90b 2020-02-29T06:58:17.094+0800 [DEBUG] agent.tlsutil: Update: version=1 2020-02-29T06:58:17.095+0800 [DEBUG] agent.tlsutil: OutgoingRPCWrapper: version=1 2020-02-29T06:58:17.096+0800 [INFO] agent.server.raft: initial configuration: index=1 servers="[{Suffrage:Voter ID:e95df364-000e-e590-6bd4-ceb947b8c90b Address:127.0.0.1:8300}]" 2020-02-29T06:58:17.096+0800 [INFO] agent.server.raft: entering follower state: follower="Node at 127.0.0.1:8300 [Follower]" leader= 2020-02-29T06:58:17.096+0800 [INFO] agent.server.serf.wan: serf: EventMemberJoin: liumiaocn.dc1 127.0.0.1 2020-02-29T06:58:17.096+0800 [INFO] agent.server.serf.lan: serf: EventMemberJoin: liumiaocn 127.0.0.1 2020-02-29T06:58:17.096+0800 [INFO] agent.server: Adding LAN server: server="liumiaocn (Addr: tcp/127.0.0.1:8300) (DC: dc1)" 2020-02-29T06:58:17.096+0800 [INFO] agent.server: Handled event for server in area: event=member-join server=liumiaocn.dc1 area=wan 2020-02-29T06:58:17.097+0800 [INFO] agent: Started DNS server: address=127.0.0.1:8600 network=tcp 2020-02-29T06:58:17.097+0800 [INFO] agent: Started DNS server: address=127.0.0.1:8600 network=udp 2020-02-29T06:58:17.097+0800 [INFO] agent: Started HTTP server: address=127.0.0.1:8500 network=tcp 2020-02-29T06:58:17.097+0800 [INFO] agent: Started gRPC server: address=127.0.0.1:8502 network=tcp 2020-02-29T06:58:17.097+0800 [INFO] agent: started state syncer ==> Consul agent running! 2020-02-29T06:58:17.161+0800 [WARN] agent.server.raft: heartbeat timeout reached, starting election: last-leader= 2020-02-29T06:58:17.161+0800 [INFO] agent.server.raft: entering candidate state: node="Node at 127.0.0.1:8300 [Candidate]" term=2 2020-02-29T06:58:17.161+0800 [DEBUG] agent.server.raft: votes: needed=1 2020-02-29T06:58:17.161+0800 [DEBUG] agent.server.raft: vote granted: from=e95df364-000e-e590-6bd4-ceb947b8c90b term=2 tally=1 2020-02-29T06:58:17.161+0800 [INFO] agent.server.raft: election won: tally=1 2020-02-29T06:58:17.161+0800 [INFO] agent.server.raft: entering leader state: leader="Node at 127.0.0.1:8300 [Leader]" 2020-02-29T06:58:17.161+0800 [INFO] agent.server: cluster leadership acquired 2020-02-29T06:58:17.162+0800 [INFO] agent.server: New leader elected: payload=liumiaocn 2020-02-29T06:58:17.162+0800 [DEBUG] connect.ca.consul: consul CA provider configured: id=07:80:c8:de:f6:41:86:29:8f:9c:b8:17:d6:48:c2:d5:c5:5c:7f:0c:03:f7:cf:97:5a:a7:c1:68:aa:23:ae:81 is_primary=true 2020-02-29T06:58:17.173+0800 [INFO] agent.server.connect: initialized primary datacenter CA with provider: provider=consul 2020-02-29T06:58:17.173+0800 [INFO] agent.leader: started routine: routine="CA root pruning" 2020-02-29T06:58:17.173+0800 [DEBUG] agent.server: Skipping self join check for node since the cluster is too small: node=liumiaocn 2020-02-29T06:58:17.173+0800 [INFO] agent.server: member joined, marking health alive: member=liumiaocn 2020-02-29T06:58:17.499+0800 [DEBUG] agent: Skipping remote check since it is managed automatically: check=serfHealth 2020-02-29T06:58:17.499+0800 [INFO] agent: Synced node info 2020-02-29T06:58:17.499+0800 [DEBUG] agent: Node info in sync 2020-02-29T06:58:19.165+0800 [DEBUG] agent.tlsutil: OutgoingRPCWrapper: version=1 2020-02-29T06:58:20.295+0800 [DEBUG] agent: Skipping remote check since it is managed automatically: check=serfHealth 2020-02-29T06:58:20.295+0800 [DEBUG] agent: Node info in sync
Note: Consul will use hostname as the default node name, such as liumiaocn in the example of this article. However, if the hostname contains,. The DNS query in Consul will not work. In this case, you need to use the - node option to set the node name.
Query node information
Start another terminal and execute the following command to confirm the Consul node information
Execute command: consumer members
liumiaocn:~ liumiao$ consul members Node Address Status Type Build Protocol DC Segment liumiaocn 127.0.0.1:8301 alive server 1.7.1 2 dc1 <all> liumiaocn:~ liumiao$
During execution, you can confirm the following similar information:
2020-02-29T07:10:25.609+0800 [DEBUG] agent.http: Request finished: method=GET url=/v1/agent/members?segment=_all from=127.0.0.1:53489 latency=113.689µs
Use the detailed option to confirm more detailed information
liumiaocn:~ liumiao$ consul members -detailed Node Address Status Tags liumiaocn 127.0.0.1:8301 alive acls=0,build=1.7.1:2cf0a3c8,dc=dc1,id=e95df364-000e-e590-6bd4-ceb947b8c90b,port=8300,raft_vsn=3,role=consul,segment=<all>,vsn=2,vsn_max=3,vsn_min=2,wan_join_port=8302 liumiaocn:~ liumiao$
Stop Agent
Start another terminal and execute the following command to stop the Agent. To be exact, disconnect this node from the Consul cluster and stop the Consul Agent
Execute command: consumer leave
An example of stopping command execution is as follows:
liumiaocn:~ liumiao$ consul leave Graceful leave complete liumiaocn:~ liumiao$
The log information is as follows:
2020-02-29T07:12:20.113+0800 [INFO] agent.server: server starting leave 2020-02-29T07:12:20.113+0800 [INFO] agent.server.serf.wan: serf: EventMemberLeave: liumiaocn.dc1 127.0.0.1 2020-02-29T07:12:20.113+0800 [INFO] agent.server: Handled event for server in area: event=member-leave server=liumiaocn.dc1 area=wan 2020-02-29T07:12:20.113+0800 [INFO] agent.server.router.manager: shutting down 2020-02-29T07:12:23.113+0800 [INFO] agent.server.serf.lan: serf: EventMemberLeave: liumiaocn 127.0.0.1 2020-02-29T07:12:23.113+0800 [INFO] agent.server: Removing LAN server: server="liumiaocn (Addr: tcp/127.0.0.1:8300) (DC: dc1)" 2020-02-29T07:12:23.113+0800 [WARN] agent.server: deregistering self should be done by follower: name=liumiaocn 2020-02-29T07:12:23.138+0800 [ERROR] agent.server.autopilot: Error updating cluster health: error="error getting server raft protocol versions: No servers found" 2020-02-29T07:12:25.138+0800 [ERROR] agent.server.autopilot: Error updating cluster health: error="error getting server raft protocol versions: No servers found" 2020-02-29T07:12:26.113+0800 [INFO] agent.server: Waiting to drain RPC traffic: drain_time=5s 2020-02-29T07:12:27.137+0800 [ERROR] agent.server.autopilot: Error updating cluster health: error="error getting server raft protocol versions: No servers found" 2020-02-29T07:12:27.137+0800 [ERROR] agent.server.autopilot: Error promoting servers: error="error getting server raft protocol versions: No servers found" 2020-02-29T07:12:29.137+0800 [ERROR] agent.server.autopilot: Error updating cluster health: error="error getting server raft protocol versions: No servers found" 2020-02-29T07:12:31.116+0800 [INFO] agent: Requesting shutdown 2020-02-29T07:12:31.116+0800 [INFO] agent.server: shutting down server 2020-02-29T07:12:31.116+0800 [DEBUG] agent.leader: stopping routine: routine="CA root pruning" 2020-02-29T07:12:31.116+0800 [DEBUG] agent.leader: stopped routine: routine="CA root pruning" 2020-02-29T07:12:31.116+0800 [INFO] agent: consul server down 2020-02-29T07:12:31.116+0800 [INFO] agent: shutdown complete 2020-02-29T07:12:31.117+0800 [DEBUG] agent.http: Request finished: method=PUT url=/v1/agent/leave from=127.0.0.1:53565 latency=11.004059585s 2020-02-29T07:12:31.117+0800 [INFO] agent: Stopping server: protocol=DNS address=127.0.0.1:8600 network=tcp 2020-02-29T07:12:31.117+0800 [INFO] agent: Stopping server: protocol=DNS address=127.0.0.1:8600 network=udp 2020-02-29T07:12:31.117+0800 [INFO] agent: Stopping server: protocol=HTTP address=127.0.0.1:8500 network=tcp 2020-02-29T07:12:31.117+0800 [INFO] agent: Waiting for endpoints to shut down 2020-02-29T07:12:31.117+0800 [INFO] agent: Endpoints down 2020-02-29T07:12:31.117+0800 [INFO] agent: Exit code: code=0 liumiaocn:~ liumiao$