Ubuntu16.04安裝Consul


1.下載安裝包

https://www.consul.io/downloads.html
wget https://releases.hashicorp.com/consul/1.5.3/consul_1.5.3_linux_amd64.zip

2.解壓

unzip consul_1.5.3_linux_amd64.zip

3.mv

sudo mv consul /usr/local/bin/consul

4.啟動

參考:https://blog.csdn.net/u010046908/article/details/61916389

-dev 開發模式啟動的時候,數據是存儲在內存中,重啟之后數據將丟失

consul agent -dev

-server 生成模式啟動的時候,如果是server的話需要指定-server,如果是client的話,需要指定-client,比如

consul agent -ui -server -bootstrap-expect 1 -data-dir /tmp/consul -node=consul-server -bind=192.168.1.100 -client=192.168.1.100

-bootstrap-expect 1 通知consul server我們現在准備加入的server節點個數,該參數是為了延遲日志復制的啟動直到我們指定數量的server節點成功的加入后啟動

-data-dir /tmp/consul 數據持久的路徑

-node=consul-server 指定節點在集群中的名稱

-bind=192.168.1.100 該地址用來在集群內部的通訊,集群內的所有節點到地址都必須是可達的,默認是0.0.0.0,這意味着Consulo會使用第一個可用的私有IP地址,Consul可以使用TCP和UDP並且可以使用共同的端口,如果存在防火牆,這兩者協議必須是允許的

-client 指定節點為client,指定客戶端接口的綁定地址,包括:HTTP、DNS、RPC,默認是127.0.0.1只允許回環接口訪問,也就是本機訪問,如果要想同一局域網內的其他機器訪問,需要修改成自己的內網ip

server節點,指定client等於內網ip,統一局域網的機器可以訪問,指定client=0.0.0.0,外網機器可以訪問

nohup ./consul agent -ui -server -bootstrap-expect 1 -data-dir /home/dl/projects/consul-1.5.3/consul-data -node=xxx -client=xxx >> ./logs/consul.log 2>&1 &

client節點,不指定client的話,只能本機訪問client節點,指定client=0.0.0.0,外網機器可以訪問

nohup ./consul agent -ui -data-dir /home/dl/projects/consul-1.5.3/consul-data -node=xxx -bind=xxxx -client=xxx >> ./logs/consul.log 2>&1 &

join

./consul join server的ip

當3台分布式部署的時候,需要如下部署

參考:https://blog.csdn.net/chenchong08/article/details/77885989

nohup ./consul agent -ui -server -bootstrap-expect 3 -data-dir /home/dl/projects/consul-1.5.3/consul-data -node=host1 -client=0.0.0.0 -bind=ip1 >> ./logs/consul.log 2>&1 &
nohup ./consul agent -ui -server -bootstrap-expect 3 -data-dir /home/dl/projects/consul-1.5.3/consul-data -node=host2 -client=0.0.0.0 -bind=ip2 >> ./logs/consul.log 2>&1 &
./consul join host1
nohup ./consul agent -ui -server -bootstrap-expect 3 -data-dir /home/dl/projects/consul-1.5.3/consul-data -node=host3 -client=0.0.0.0 -bind=ip3 >> ./logs/consul.log 2>&1 &
./consul join host2

 查看集群狀態

./consul operator raft list-peers 
Node           ID                                    Address           State     Voter  RaftProtocol
host1  f6ec724d-95ec-00da-97a2-4412d1070889  10.90.1.111:8300  follower  true   3
host2  a79165a3-7ffe-0bcf-9222-c50538d7f394  10.90.1.112:8300  leader    true   3
host3  54fed87b-b76a-432b-9ede-1933bb9d88c5  10.90.1.113:8300  follower  true   3

這時候host2是leader,當host2掛掉的時候,會有短暫不可用,之后leader選出,host1成為新的leader

./consul operator raft list-peers 
Error getting peers: Failed to retrieve raft configuration: Unexpected response code: 500 (rpc error getting client: failed to get conn: dial tcp 10.90.1.113:0->10.90.1.112:8300: connect: connection refused)

./consul operator raft list-peers 
Node           ID                                    Address           State     Voter  RaftProtocol
host1  f6ec724d-95ec-00da-97a2-4412d1070889  10.90.1.111:8300  leader    true   3
host2  a79165a3-7ffe-0bcf-9222-c50538d7f394  10.90.1.112:8300  follower  true   3
host3  54fed87b-b76a-432b-9ede-1933bb9d88c5  10.90.1.113:8300  follower  true   3

 

新的leader的日志不斷刷說有個follow掛了

2019/12/30 16:42:17 [ERROR] raft: Failed to heartbeat to 10.90.1.111:8300: dial tcp 10.90.1.113:0->10.90.1.111:8300: connect: connection refused
    2019/12/30 16:42:18 [WARN] consul: error getting server health from "host1": rpc error getting client: failed to get conn: dial tcp 10.90.1.113:0->10.90.1.111:8300: connect: connection refused

啟動host2的進程,自動加入集群,不用再join,集群狀態恢復

 

5.使用

訪問8500端口

5.kv存儲

consul支持通過HTTP API和通過CLI API將配置進行存儲

put命令

consul kv put app1/config/parameter1 1
Success! Data written to: app1/config/parameter1

put成功之后,就可以在頁面中看到該配置

具體數值

 

put之后可以使用get命令來獲取這個配置

consul kv get app1/config/parameter1
1

遞歸獲取

consul kv get -recurse app1
app1/config/parameter1:1
app1/config/parameter2:2
app1/config/parameter3:3

刪除

consul kv delete app1/config/parameter2 2

 

HTTP api

get kv

curl http://xxxx:8500/v1/kv/app1?recurse
[{"LockIndex":0,"Key":"app1/config/parameter1","Flags":0,"Value":"MQ==","CreateIndex":5986,"ModifyIndex":5986},{"LockIndex":0,"Key":"app1/config/parameter2","Flags":0,"Value":"Mg==","CreateIndex":5987,"ModifyIndex":5987}]

get單個

curl http://xxx:8500/v1/kv/app1/config/parameter1
[{"LockIndex":0,"Key":"app1/config/parameter1","Flags":0,"Value":"MQ==","CreateIndex":5986,"ModifyIndex":5986}]

其中kv存儲的v的值是經過base64編碼過的,需要進行解碼

echo 'MQ==' | base64 -d
1

put kv

curl -X PUT -d '1111111' http://xxxx:8500/v1/kv/app1/config/parameter1

 

delete kv

curl -X DELETE -d http://xxx:8500/v1/kv/app1/config/parameter2

 

consul配置導入和導出

export命令

./consul kv export app1/config
[
	{
		"key": "app1/config/parameter1",
		"flags": 0,
		"value": "MTExMTExMWZmcmZmZg=="
	},
	{
		"key": "app1/config/parameter2",
		"flags": 0,
		"value": "Mg=="
	},
	{
		"key": "app1/config/parameter3",
		"flags": 0,
		"value": "Mw=="
	}
]

import命令

./consul kv import @test.json
Imported: app1/config/parameter4
Imported: app1/config/parameter5
Imported: app1/config/parameter6

 

 

watch

https://www.consul.io/docs/agent/watches.html

當配置更新能自動回調

需要寫一個配置文件,比如 consul-watch.json

{
  "watches": [
    {
      "type": "key",
      "key": "app1/conf/parameter1",
      "handler_type": "http",
      "http_handler_config": {
         "path":"http://localhost:8000",
         "method": "GET",
         "header": {"x-foo":["bar", "baz"]},
         "timeout": "10s",
         "tls_skip_verify": false
      }
    }
  ]
}

上面的配置是監聽單個key的變化,如果想監聽多個

"type": "keyprefix",
"prefix": "app1/",

然后在啟動命令中加上配置文件夾的地址,比如

-config-dir /home/lintong/software/consul-1.5.3/consul-conf

然后當配置發生變動的時候,會自動調用這個回調接口

如果修改了配置,比如把GET接口換成POST接口,需要reload這個配置,執行下面命令

consul reload

如果是post的回調接口的話,consul回調的時候會往post請求的request中放入更新的這一條配置,headers和body如下

Host: localhost:8000
User-Agent: Go-http-client/1.1
Content-Length: 124
Content-Type: application/json
X-Consul-Index: 5131
X-Foo: bar
X-Foo: baz
Accept-Encoding: gzip
Connection: close

{"Key":"app1/config/parameter1","CreateIndex":7,"ModifyIndex":5131,"LockIndex":0,"Flags":0,"Value":"IjEyMyI=","Session":""}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM