consul服務(wù)器搭建

安裝consul

官網(wǎng)下載合適的版本即可
windows下直接下載解壓即可
linux下可以使用wget命令下載

//1.下載壓縮包
wget https://releases.hashicorp.com/consul/1.4.2/consul_1.4.2_linux_amd64.zip  
//2.解壓
unzip consul_1.4.2_linux_amd64.zip  
//3.檢查安裝
./consul -v
Consul v1.4.2
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)  

啟動(dòng)consul

開發(fā)服務(wù)器模式-dev

consul支持開發(fā)服務(wù)器模式來快速啟動(dòng)單節(jié)點(diǎn)consul:
consul agent -dev
這種模式下啟動(dòng)的是內(nèi)存服務(wù)器,所有數(shù)據(jù)不會(huì)持久化. 僅用于開發(fā).

瀏覽器中訪問http://localhost:8500即可看到consul管理界面,如下圖所示:

TIM圖片20190218165212.png

集群模式

consul使用raft協(xié)議實(shí)現(xiàn)一致性,集群至少部署三個(gè)節(jié)點(diǎn).
集群部署方式,agent先各自啟動(dòng),通過將某個(gè)agent加入cluster來實(shí)現(xiàn)集群部署.

集群規(guī)劃:
consul server1:172.16.22.2
consul server2:172.16.22.51
consul server3:10.45.82.76

啟動(dòng)consul服務(wù)器

172.16.22.2  
[test@node1 consul]$ nohup ./consul agent -server -bind=172.16.22.2 -client=0.0.0.0 -bootstrap-expect=3 -data-dir=/usr/test/consul/data -node=server-xh-1 -ui &  

172.16.22.51
[test@node2 consul]$nohup ./consul agent -server -bind=172.16.22.51 -client=0.0.0.0 -bootstrap-expect=3 -data-dir=/usr/test/consul/data -node=server-xh-2 -ui &  

10.45.82.76  
[test@node3 consul]$nohup ./consul agent -server -bind=10.45.82.76 -client=0.0.0.0 -bootstrap-expect=3 -data-dir=/usr/test/consul/data -node=server-xh-3 -ui &  

啟動(dòng)命令參數(shù)可參考Command-line Options

這個(gè)時(shí)候查看下consul agent,會(huì)發(fā)現(xiàn)每個(gè)agent下都只有各自自身,即每個(gè)agent都是相互獨(dú)立的.

172.16.22.2 
[test@node1 consul]$ ./consul members
Node         Address           Status  Type    Build  Protocol  DC   Segment
server-xh-1  172.16.22.2:8301  alive   server  1.4.2  2         dc1  <all>  

172.16.22.51  
[test@node2 consul]$./consul members
Node         Address            Status  Type    Build  Protocol  DC   Segment
server-xh-2  172.16.22.51:8301  alive   server  1.4.2  2         dc1  <all>

10.45.82.76   
[test@node3 consul]$./consul members
Node         Address           Status  Type    Build  Protocol  DC   Segment
server-xh-3  10.45.82.76:8301  alive   server  1.4.2  2         dc1  <all>

通過將172.16.22.2和172.16.22.51加入到10.45.82.76來實(shí)現(xiàn)集群部署

[test@node1 consul]$./consul join 10.45.82.76
Successfully joined cluster by contacting 1 nodes.
[test@node1 consul]$./consul members
Node       Address            Status  Type    Build  Protocol  DC   Segment
server-01  172.16.22.2:8301   alive   server  1.4.2  2         dc1  <all>
server-03  10.45.82.76:8301   alive   server  1.4.2  2         dc1  <all> 
[test@node2 consul]$./consul join 10.45.82.76
Successfully joined cluster by contacting 1 nodes.
[test@node2 consul]$./consul members
Node       Address            Status  Type    Build  Protocol  DC   Segment
server-01  172.16.22.2:8301   alive   server  1.4.2  2         dc1  <all>
server-02  172.16.22.51:8301  alive   server  1.4.2  2         dc1  <all>
server-03  10.45.82.76:8301   alive   server  1.4.2  2         dc1  <all>

至此,包含三個(gè)節(jié)點(diǎn)的consul cluster搭建好了
注意:集群部署的時(shí)候可以看下啟動(dòng)日志,能比較清楚的看到Leader選舉的流程等

啟動(dòng)consul時(shí)打印的一些重要信息

以下是開發(fā)服務(wù)器模式啟動(dòng)的部分日志:

==> Starting Consul agent...
==> Consul agent running!  
           //consul版本號(hào)
           Version: 'v1.4.2'  
           //agent節(jié)點(diǎn)的ID,隨機(jī)生成的唯一ID
           Node ID: '4e02ef52-9690-5fc6-b16c-de9724422f84'  
         //agent節(jié)點(diǎn)的唯一名稱,默認(rèn)情況下是主機(jī)名,可以使用-node指定節(jié)點(diǎn)名稱
         Node name: 'localhost.localdomain'  
        //數(shù)據(jù)中心.consul支持多數(shù)據(jù)中心. 每個(gè)agent節(jié)點(diǎn)都需要指定其使用的數(shù)據(jù)中心,默認(rèn)為dc1.可以通過-datacenter指定使用的數(shù)據(jù)中心. 
        Datacenter: 'dc1' (Segment: '<all>')  
            //服務(wù)器.true表示當(dāng)前agent是以服務(wù)器模式運(yùn)行.false表示客戶端模式.
            //Bootstrap引導(dǎo)模式,帶上啟動(dòng)參數(shù)-bootstrap可以開啟引導(dǎo)模式.一個(gè)數(shù)據(jù)中心下最多只有一個(gè)agent節(jié)點(diǎn)可以設(shè)置為引導(dǎo)模式,因?yàn)樵谶@種模式下agent節(jié)點(diǎn)可以自選為Raft leader,多個(gè)會(huì)導(dǎo)致不一致性.  
            Server: true (Bootstrap: false)  
       //客戶端連接agent節(jié)點(diǎn)的IP地址及端口號(hào).默認(rèn)情況下只綁定到本機(jī).  
       Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600)  
      //集群中consul agent節(jié)點(diǎn)之間通信的IP地址和端口
      Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)  
           //
           Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false

==> Log data will now stream in as it occurs:

    2019/02/18 18:49:41 [DEBUG] agent: Using random ID "4e02ef52-9690-5fc6-b16c-de9724422f84" as node ID  
    2019/02/18 18:49:41 [WARN] agent: Node name "localhost.localdomain" will not be discoverable via DNS due to invalid characters. Valid characters include all alpha-numerics and dashes.
    2019/02/18 18:49:41 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:4e02ef52-9690-5fc6-b16c-de9724422f84 Address:127.0.0.1:8300}]
    2019/02/18 18:49:41 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")
    2019/02/18 18:49:41 [INFO] serf: EventMemberJoin: localhost.localdomain.dc1 127.0.0.1
    2019/02/18 18:49:41 [INFO] serf: EventMemberJoin: localhost.localdomain 127.0.0.1
    2019/02/18 18:49:41 [INFO] consul: Handled member-join event for server "localhost.localdomain.dc1" in area "wan"
    2019/02/18 18:49:41 [DEBUG] agent/proxy: managed Connect proxy manager started
    2019/02/18 18:49:41 [INFO] consul: Adding LAN server localhost.localdomain (Addr: tcp/127.0.0.1:8300) (DC: dc1)
    2019/02/18 18:49:41 [INFO] agent: Started DNS server 127.0.0.1:8600 (udp)
    2019/02/18 18:49:41 [INFO] agent: Started DNS server 127.0.0.1:8600 (tcp)
    2019/02/18 18:49:41 [INFO] agent: Started HTTP server on 127.0.0.1:8500 (tcp)
    2019/02/18 18:49:41 [INFO] agent: started state syncer
    2019/02/18 18:49:41 [INFO] agent: Started gRPC server on 127.0.0.1:8502 (tcp)
    2019/02/18 18:49:41 [WARN] raft: Heartbeat timeout from "" reached, starting election
    2019/02/18 18:49:41 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2
    2019/02/18 18:49:41 [DEBUG] raft: Votes needed: 1
    2019/02/18 18:49:41 [DEBUG] raft: Vote granted from 4e02ef52-9690-5fc6-b16c-de9724422f84 in term 2. Tally: 1
    2019/02/18 18:49:41 [INFO] raft: Election won. Tally: 1
    2019/02/18 18:49:41 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
    2019/02/18 18:49:41 [INFO] consul: cluster leadership acquired
    2019/02/18 18:49:41 [INFO] consul: New leader elected: localhost.localdomain
    2019/02/18 18:49:41 [INFO] connect: initialized primary datacenter CA with provider "consul"
    2019/02/18 18:49:41 [DEBUG] consul: Skipping self join check for "localhost.localdomain" since the cluster is too small
    2019/02/18 18:49:41 [INFO] consul: member 'localhost.localdomain' joined, marking health alive
    2019/02/18 18:49:42 [DEBUG] agent: Skipping remote check "serfHealth" since it is managed automatically
    2019/02/18 18:49:42 [INFO] agent: Synced node info
    2019/02/18 18:49:42 [DEBUG] agent: Node info in sync
    2019/02/18 18:49:44 [DEBUG] agent: Skipping remote check "serfHealth" since it is managed automatically
    2019/02/18 18:49:44 [DEBUG] agent: Node info in sync

通過啟動(dòng)日志可以發(fā)現(xiàn):
1.建議使用啟動(dòng)參數(shù)-node指定agent節(jié)點(diǎn)名稱,因?yàn)槟J(rèn)的主機(jī)名可能包含一些無效字符.節(jié)點(diǎn)名稱有效字符包括所有字母數(shù)字和破折號(hào).
2.一致性協(xié)議使用Raft協(xié)議

consul web ui

我們可以通過web UI查看集群信息
注意,啟動(dòng)agent的時(shí)候帶參數(shù)-ui即可啟動(dòng)consul自帶的web管理界面,默認(rèn)端口號(hào)8500
http://10.45.82.76:8500

consulWebUI.png

consul command

查看下consul命令支持的功能:

[test@node3 consul]$./consul --help
Usage: consul [--version] [--help] <command> [<args>]

Available commands are:
    acl            Interact with Consul's ACLs
    agent          Runs a Consul agent
    catalog        Interact with the catalog
    connect        Interact with Consul Connect
    debug          Records a debugging archive for operators
    event          Fire a new event
    exec           Executes a command on Consul nodes
    force-leave    Forces a member of the cluster to enter the "left" state
    info           Provides debugging information for operators.
    intention      Interact with Connect service intentions  
    //將consul server加入consul cluster  ./consul join 172.16.22.2
    join           Tell Consul agent to join cluster
    keygen         Generates a new encryption key
    keyring        Manages gossip layer encryption keys
    kv             Interact with the key-value store
    //優(yōu)雅的停掉consul server    ./consul leave
    leave          Gracefully leaves the Consul cluster and shuts down
    lock           Execute a command holding a lock
    maint          Controls node or service maintenance mode  
    //查看當(dāng)前consul下的成員    ./consul members 
    members        Lists the members of a Consul cluster
    monitor        Stream logs from a Consul agent
    operator       Provides cluster-level tools for Consul operators
    reload         Triggers the agent to reload configuration files
    rtt            Estimates network round trip time between nodes
    services       Interact with services
    snapshot       Saves, restores and inspects snapshots of Consul server state
    tls            Builtin helpers for creating CAs and certificates
    validate       Validate config files/directories
    version        Prints the Consul version
    watch          Watch for changes in Consul

可以通過./consul command --help查看具體某個(gè)指令支持的參數(shù).比如./consul agent --help

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末旨怠,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖芥挣,帶你破解...
    沈念sama閱讀 211,348評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件平匈,死亡現(xiàn)場(chǎng)離奇詭異腮考,居然都是意外死亡忘巧,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,122評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門桨啃,熙熙樓的掌柜王于貴愁眉苦臉地迎上來车胡,“玉大人,你說我怎么就攤上這事照瘾⌒偌” “怎么了?”我有些...
    開封第一講書人閱讀 156,936評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵析命,是天一觀的道長主卫。 經(jīng)常有香客問我,道長鹃愤,這世上最難降的妖魔是什么簇搅? 我笑而不...
    開封第一講書人閱讀 56,427評(píng)論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮昼浦,結(jié)果婚禮上馍资,老公的妹妹穿的比我還像新娘筒主。我一直安慰自己关噪,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,467評(píng)論 6 385
  • 文/花漫 我一把揭開白布乌妙。 她就那樣靜靜地躺著使兔,像睡著了一般。 火紅的嫁衣襯著肌膚如雪藤韵。 梳的紋絲不亂的頭發(fā)上虐沥,一...
    開封第一講書人閱讀 49,785評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼欲险。 笑死镐依,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的天试。 我是一名探鬼主播槐壳,決...
    沈念sama閱讀 38,931評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼喜每!你這毒婦竟也來了务唐?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,696評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤带兜,失蹤者是張志新(化名)和其女友劉穎枫笛,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體刚照,經(jīng)...
    沈念sama閱讀 44,141評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡刑巧,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,483評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了无畔。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片海诲。...
    茶點(diǎn)故事閱讀 38,625評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖檩互,靈堂內(nèi)的尸體忽然破棺而出特幔,到底是詐尸還是另有隱情,我是刑警寧澤闸昨,帶...
    沈念sama閱讀 34,291評(píng)論 4 329
  • 正文 年R本政府宣布蚯斯,位于F島的核電站,受9級(jí)特大地震影響饵较,放射性物質(zhì)發(fā)生泄漏拍嵌。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,892評(píng)論 3 312
  • 文/蒙蒙 一循诉、第九天 我趴在偏房一處隱蔽的房頂上張望横辆。 院中可真熱鬧,春花似錦茄猫、人聲如沸狈蚤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,741評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽脆侮。三九已至,卻和暖如春勇劣,著一層夾襖步出監(jiān)牢的瞬間靖避,已是汗流浹背潭枣。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留幻捏,地道東北人盆犁。 一個(gè)月前我還...
    沈念sama閱讀 46,324評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像篡九,于是被迫代替她去往敵國和親蚣抗。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,492評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容