consul服务器搭建

安装consul

官网下载合适的版本即可
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)  

启动consul

开发服务器模式-dev

consul支持开发服务器模式来快速启动单节点consul:
consul agent -dev
这种模式下启动的是内存服务器,所有数据不会持久化. 仅用于开发.

浏览器中访问http://localhost:8500即可看到consul管理界面,如下图所示:

TIM图片20190218165212.png

集群模式

consul使用raft协议实现一致性,集群至少部署三个节点.
集群部署方式,agent先各自启动,通过将某个agent加入cluster来实现集群部署.

集群规划:
consul server1:172.16.22.2
consul server2:172.16.22.51
consul server3:10.45.82.76

启动consul服务器

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 &  

启动命令参数可参考Command-line Options

这个时候查看下consul agent,会发现每个agent下都只有各自自身,即每个agent都是相互独立的.

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来实现集群部署

[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>

至此,包含三个节点的consul cluster搭建好了
注意:集群部署的时候可以看下启动日志,能比较清楚的看到Leader选举的流程等

启动consul时打印的一些重要信息

以下是开发服务器模式启动的部分日志:

==> Starting Consul agent...
==> Consul agent running!  
           //consul版本号
           Version: 'v1.4.2'  
           //agent节点的ID,随机生成的唯一ID
           Node ID: '4e02ef52-9690-5fc6-b16c-de9724422f84'  
         //agent节点的唯一名称,默认情况下是主机名,可以使用-node指定节点名称
         Node name: 'localhost.localdomain'  
        //数据中心.consul支持多数据中心. 每个agent节点都需要指定其使用的数据中心,默认为dc1.可以通过-datacenter指定使用的数据中心. 
        Datacenter: 'dc1' (Segment: '<all>')  
            //服务器.true表示当前agent是以服务器模式运行.false表示客户端模式.
            //Bootstrap引导模式,带上启动参数-bootstrap可以开启引导模式.一个数据中心下最多只有一个agent节点可以设置为引导模式,因为在这种模式下agent节点可以自选为Raft leader,多个会导致不一致性.  
            Server: true (Bootstrap: false)  
       //客户端连接agent节点的IP地址及端口号.默认情况下只绑定到本机.  
       Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600)  
      //集群中consul agent节点之间通信的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

通过启动日志可以发现:
1.建议使用启动参数-node指定agent节点名称,因为默认的主机名可能包含一些无效字符.节点名称有效字符包括所有字母数字和破折号.
2.一致性协议使用Raft协议

consul web ui

我们可以通过web UI查看集群信息
注意,启动agent的时候带参数-ui即可启动consul自带的web管理界面,默认端口号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
    //优雅的停掉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  
    //查看当前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查看具体某个指令支持的参数.比如./consul agent --help

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,047评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,807评论 3 386
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,501评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,839评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,951评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,117评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,188评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,929评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,372评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,679评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,837评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,536评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,168评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,886评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,129评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,665评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,739评论 2 351

推荐阅读更多精彩内容