Knowledge preheating

Consul Common commands

command explain Example
agent Run one consul agent consul agent -dev
join take agent Add to consul colony consul join IP
members List consul cluster In the cluster members consul members
leave Remove the node from the cluster consul leave

Reference documents

Here are just a few common commands ,consul There are nearly 20 An order , This article is not expanded , See :

consul agent Detailed command

Common options

consul agent Common options for commands are as follows :

  • -data-dir
    • effect : Appoint agent A directory of stored data
    • This is all. agent It's all necessary
    • about server Especially important , Because they have to persist the state of the cluster
  • -config-dir
    • effect : Appoint service The location of the configuration file and check definition for
    • It is usually designated as ” A certain path /consul.d”( Usually ,.d Represents the directory where a series of configuration files are stored )
  • -config-file
    • effect : Specify a profile to load
    • This option can be configured multiple times , And then configure multiple configuration files ( The back one will merge the front one , The same value covers )
  • -dev
    • effect : Create a development environment server node
    • Under this parameter configuration , There will be no persistence operations , That is, no data is written to the disk
    • This pattern cannot be used in production environments ( Because the second one )
  • -bootstrap-expect
    • effect : Order notification consul server We are now ready to join server Number of nodes , This parameter is used to delay the start of log replication until we specify the number of server Start after the node successfully joins .
  • -node
    • effect : Specify the name of the node in the cluster
    • The name must be unique in the cluster ( The default is machine host)
    • recommend : Direct use of machine IP
  • -bind
    • effect : Specify the IP Address
    • Sometimes no binding is specified IP, Will be submitted to the Failed to get advertise address: Multiple private IPs found. Please configure one. It's abnormal
  • -server
    • effect : Specify the node as server
    • Every data center (DC) Of server The number recommended is at least 1, At most 5
    • be-all server All use raft Consistency algorithm to ensure the consistency and linearization of transactions , The transaction modifies the state of the cluster , And the state of the cluster is saved in each server To ensure availability
    • server Also with other DC The facade of interaction (gateway)
  • -client
    • effect : Specify the node as client, Specify the binding address of the client interface , Include :HTTP、DNS、RPC
    • The default is, Only loopback interfaces are allowed to access
    • If not specified as -server, In fact, that is -client
  • -join
    • effect : Add nodes to the cluster
  • -datacenter( The old version is called -dc,-dc It's invalid )
    • effect : Specify which data center the machine will join

Example 1: Command parameter mode execution

consul agent -data-dir /tmp/node2 -node=node2 -bind= -datacenter=dc1 -ui -client=

Example 2: Profile mode execution

Prepare a profile , Any name , for example consul.json, The contents are as follows :

"datacenter": "us-east-1",
"data_dir": "/Users/zhouli/develop/software/consul-data/",
"addresses": {
"https": ""
"ports": {
"http": 8500
"acl" : {
"enabled": true,
"default_policy": "deny",
"down_policy": "extend-cache",
"tokens" : {
"master" : "b1gs33cr3t"

Startup time , Give Way consul Read configuration file :

consul agent -dev -config-file="consul-config/consul.json"

Reference documents

consul agent Yes 20 Here's an option , This article lists only the common options , Other options are not expanded , See :

Consul Cluster building

Consul Architecture diagram

Hosting plan

Prepare three CentOS 7 Virtual machine , The host is planned as follows :

Host name IP effect Whether remote access is allowed
node0 consul server yes
node1 consul client no
node2 consul client yes

Steps to build

  • start-up node0 On the machine Consul(node0 Executed on machine ):
consul agent -data-dir /tmp/node0 -node=node0 -bind= -datacenter=dc1 -ui -client= -server -bootstrap-expect 1
  • start-up node1 On the machine Consul(node1 Executed on machine ):
consul agent -data-dir /tmp/node1 -node=node1 -bind= -datacenter=dc1 -ui
  • start-up node2 On the machine Consul(node2 Executed on machine ):
consul agent -data-dir /tmp/node2 -node=node2 -bind= -datacenter=dc1 -ui -client=
  • take node1 Node join to node0 On (node1 Executed on machine ):
consul join
  • take node2 Node join to node0 On (node2 Executed on machine ):
consul join -rpc-addr=
  • It's a simple one Consul The cluster is set up , stay node1 View the current cluster node on :
consul members -rpc-addr=

give the result as follows :

Node   Address              Status  Type    Build  Protocol  DC
node0 alive server 0.7.0 2 dc1
node1 alive client 0.7.0 2 dc1
node2 alive client 0.7.0 2 dc1

It indicates that the cluster has been successfully built .

Let's analyze , Why the 5 And the first step 6 Step needs to add -rpc-addr Options , And the first 4 Step does not need to add any options ? as a result of -client The binding address of the client interface is specified , Include :HTTP、DNS、RPC, and consul joinconsul members It's all through RPC And Consul The interaction of .

Access test

Above , We've added... To all three nodes -ui Parameters start the built-in interface . We can go through : perhaps Visit , It can also be in node1 Machine pass Visit , as a result of node1 Remote access is not turned on , The results of the three access methods are the same .

I have to read the article

Related articles