- Go to https://consul.io/downloads.html and download the appropriate archive, depending on the operating system that you are using. Consul supports Windows, OS X, and Linux, so it should work for the majority of readers.
If you are an OS X user, you can install Consul using Homebrew by running brew install caskroom/cask/brew-cask followed by brew cask install consul.
- After the installation, we should be able to run consul --version and see the following output:
Consul v1.0.1
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)
- With Consul successfully installed, we should be able to start it by running the consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul command and our terminal window will display the following:
==> WARNING: BootstrapExpect Mode is specified as 1; this is the same as Bootstrap mode. ==> WARNING: Bootstrap mode enabled! Do not enable unless necessary ==> WARNING: It is highly recommended to set GOMAXPROCS higher than 1 ==> Starting Consul agent... ==> Starting Consul agent RPC... ==> Consul agent running! Node name: <your machine name>' Datacenter: 'dc1' Server: true (bootstrap: true) Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400) Cluster Addr: 192.168.1.227 (LAN: 8301, WAN: 8302) Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false Atlas: <disabled> ==> Log data will now stream in as it occurs: 2017/12/17 20:34:43 [INFO] serf: EventMemberJoin: <your machine name> 192.168.1.227 2017/12/17 20:34:43 [INFO] serf: EventMemberJoin: <your machine name>.dc1 192.168.1.227 2017/12/17 20:34:43 [INFO] raft: Node at 192.168.1.227:8300 [Follower] entering Follower state 2017/12/17 20:34:43 [INFO] consul: adding server <your machine name> (Addr: 192.168.1.227:8300) (DC: dc1) 2017/12/17 20:34:43 [INFO] consul: adding server <your machine name>.dc1 (Addr: 192.168.1.227:8300) (DC: dc1) 2017/12/17 20:34:43 [ERR] agent: failed to sync remote state: No cluster leader 2017/12/17 20:34:45 [WARN] raft: Heartbeat timeout reached, starting election 2017/12/17 20:34:45 [INFO] raft: Node at 192.168.1.227:8300 [Candidate] entering Candidate state 2017/12/17 20:34:45 [INFO] raft: Election won. Tally: 1 2017/12/17 20:34:45 [INFO] raft: Node at 192.168.1.227:8300 [Leader] entering Leader state 2017/12/17 20:34:45 [INFO] consul: cluster leadership acquired 2017/12/17 20:34:45 [INFO] consul: New leader elected: <your machine name> 2017/12/17 20:34:45 [INFO] raft: Disabling EnableSingleNode (bootstrap) 2017/12/17 20:34:45 [INFO] consul: member '<your machine name>' joined, marking health alive 2017/12/17 20:34:47 [INFO] agent: Synced service 'consul'
- With the Consul service running, we can verify that it contains one member by running the consul members command, and should see the following result:
Node Address Status Type Build Protocol DC <your_machine_name> 2.168.1.227:8301 alive server 0.5.2 2 dc1
- While Consul can also provide discovery for services, health checks, distributed locks, and more, we are going to focus on the key/value service as this is what will be used to provide the configuration in the next recipe. So, let's put the From Consul Config value in the key/value store by executing the curl -X PUT -d 'From Consul Config' http://localhost:8500/v1/kv/bookpub/my/config/value command.
- We can also retrieve the data by running the curl http://localhost:8500/v1/kv/bookpub/my/config/value command and should see the following output:
[{"CreateIndex":20,"ModifyIndex":20,"LockIndex":0,"Key":"bookpub/my/config/value","Flags":0,"Value":"RnJvbSBDb25zdWwgQ29uZmln"}]
- We can delete this value by running the curl -X DELETE http://localhost:8500/v1/kv/bookpub/my/config/value command.
- In order to modify the existing value and change it for something else, execute the curl -X PUT -d 'newval' http://localhost:8500/v1/kv/bookpub/my/config/value?cas=20 command.