• Consul安装配置


    前言

    官网地址:https://www.consul.io/docs/intro
    截止当前,最新版本是1.13,本篇使用的还是1.0.6版本的方式,可能会有少许偏差。

    简介

    Consul有很多组件,但总体来说,它是一个发现和配置服务工具,特性:
    服务发现 Service Discovery: Clients of Consul can provide a service, such as api or mysql, and other clients can use Consul to discover providers of a given service. Using either DNS or HTTP, applications can easily find the services they depend upon.

    健康检查 Health Checking: Consul clients can provide any number of health checks, either associated with a given service (“is the webserver returning 200 OK”), or with the local node (“is memory utilization below 90%”). This information can be used by an operator to monitor cluster health, and it is used by the service discovery components to route traffic away from unhealthy hosts.

    key-value存储 KV Store: Applications can make use of Consul’s hierarchical key/value store for any number of purposes, including dynamic configuration, feature flagging, coordination, leader election, and more. The simple HTTP API makes it easy to use.

    数据中心 Multi Datacenter: Consul supports multiple datacenters out of the box. This means users of Consul do not have to worry about building additional layers of abstraction to grow to multiple regions.

    安装流程

    下载文件安装

    下载地址:https://www.consul.io/downloads.html
    解压缩安装包,并把目录解压缩出来的文件夹添加到环境变量即可。
    当然,也可以直接把解压缩出来的consul文件直接放到现有环境变量目录中,比如ubuntu下

    sudo scp consul /usr/local/bin/
    
    • 1
    Mac下安装

    除了上面的方法,mac下也可以使用homebrew安装

    brew install consul
    
    • 1
    查看

    版本信息

    consul -v
    
    • 1

    其他提示

    consul
    Usage: consul [--version] [--help]  []
    
    Available commands are:
        agent          Runs a Consul agent
        catalog        Interact with the catalog
        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.
        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
        leave          Gracefully leaves the Consul cluster and shuts down
        lock           Execute a command holding a lock
        maint          Controls node or service maintenance mode
        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
        snapshot       Saves, restores and inspects snapshots of Consul server state
        validate       Validate config files/directories
        version        Prints the Consul version
        watch          Watch for changes in Consul
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    开发者模式启动

    consul agent -dev
    ==> Starting Consul agent...
    ==> Consul agent running!
               Version: 'v1.0.6'
               Node ID: '2d31fc4d-698b-b0f7-61f6-edc7878ce192'
             Node name: 'bogon'
            Datacenter: 'dc1' (Segment: '')
                Server: true (Bootstrap: false)
           Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, DNS: 8600)
          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:
    
        2018/04/08 14:52:03 [DEBUG] Using random ID "2d31fc4d-698b-b0f7-61f6-edc7878ce192" as node ID
        2018/04/08 14:52:03 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:2d31fc4d-698b-b0f7-61f6-edc7878ce192 Address:127.0.0.1:8300}]
        2018/04/08 14:52:03 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")
        2018/04/08 14:52:03 [INFO] serf: EventMemberJoin: bogon.dc1 127.0.0.1
        2018/04/08 14:52:03 [INFO] serf: EventMemberJoin: bogon 127.0.0.1
        2018/04/08 14:52:03 [INFO] consul: Adding LAN server bogon (Addr: tcp/127.0.0.1:8300) (DC: dc1)
        2018/04/08 14:52:03 [INFO] consul: Handled member-join event for server "bogon.dc1" in area "wan"
        2018/04/08 14:52:03 [INFO] agent: Started DNS server 127.0.0.1:8600 (udp)
        2018/04/08 14:52:03 [INFO] agent: Started DNS server 127.0.0.1:8600 (tcp)
        2018/04/08 14:52:03 [INFO] agent: Started HTTP server on 127.0.0.1:8500 (tcp)
        2018/04/08 14:52:03 [INFO] agent: started state syncer
        2018/04/08 14:52:03 [WARN] raft: Heartbeat timeout from "" reached, starting election
        2018/04/08 14:52:03 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2
        2018/04/08 14:52:03 [DEBUG] raft: Votes needed: 1
        2018/04/08 14:52:03 [DEBUG] raft: Vote granted from 2d31fc4d-698b-b0f7-61f6-edc7878ce192 in term 2. Tally: 1
        2018/04/08 14:52:03 [INFO] raft: Election won. Tally: 1
        2018/04/08 14:52:03 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
        2018/04/08 14:52:03 [INFO] consul: cluster leadership acquired
        2018/04/08 14:52:03 [INFO] consul: New leader elected: bogon
        2018/04/08 14:52:03 [DEBUG] consul: Skipping self join check for "bogon" since the cluster is too small
        2018/04/08 14:52:03 [INFO] consul: member 'bogon' joined, marking health alive
        2018/04/08 14:52:03 [DEBUG] Skipping remote check "serfHealth" since it is managed automatically
        2018/04/08 14:52:03 [INFO] agent: Synced node info
        2018/04/08 14:52:03 [DEBUG] agent: Node info in sync
        2018/04/08 14:52:06 [DEBUG] Skipping remote check "serfHealth" since it is managed automatically
        2018/04/08 14:52:06 [DEBUG] agent: Node info in sync
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    其中:
    -dev(该节点的启动不能用于生产环境,因为该模式下不会持久化任何状态),该启动模式仅仅是为了快速便捷的启动单节点consul
    该节点处于server模式
    该节点是leader
    该节点是一个健康节点

    查看节点信息

    查看consul cluster中的每一个consul节点的信息

    consul members
    Node   Address         Status  Type    Build  Protocol  DC   Segment
    fymod  127.0.0.1:8301  alive   server  1.0.6  2         dc1  
    
    • 1
    • 2
    • 3

    Address:节点地址
    Status:alive表示节点健康
    Type:server运行状态是server状态
    DC:dc1表示该节点属于DataCenter1
    注意:
    members命令的输出是基于gossip协议的,并且是最终一致的(也就是说,某一个时刻你去运用该命令查到的consul节点的状态信息可能是有误的)

    输入http://localhost:8500/ui/ 访问Consul,可查看到如下界面:
    在这里插入图片描述

    常用命令

    命令解释示例
    agent运行一个consul agentconsul agent -dev
    join将agent加入到consul集群consul join IP
    members列出consul cluster集群中的membersconsul members
    leave将节点移除所在集群consul leave

    consul agent 命令详解
    输入consul agent --help ,可以看到consul agent 的选项,如下:

    bogon:~ dev$ consul agent --help
    Usage: consul agent [options]
    
      Starts the Consul agent and runs until an interrupt is received. The
      agent represents a single node in a cluster.
    
    HTTP API Options
    
      -datacenter=
         Datacenter of the agent.
         作用:指定机器加入到哪一个数据中心中
    
    Command Options
    
      -advertise=
         Sets the advertise address to use.
    
      -advertise-wan=
         Sets address to advertise on WAN instead of -advertise address.
    
      -bind=
         Sets the bind address for cluster communication.
         作用:指明节点的IP地址,有时候不指定绑定IP,会报Failed to get advertise address: Multiple private IPs found. Please configure one. 的异常
    
      -bootstrap
         Sets server to bootstrap mode.
    
      -bootstrap-expect=
         Sets server to expect bootstrap mode.
         作用:该命令通知consul server我们现在准备加入的server节点个数,该参数是为了延迟日志复制的启动直到我们指定数量的server节点成功的加入后启动。
    
      -client=
         Sets the address to bind for client access. This includes RPC, DNS,
         HTTP and HTTPS (if configured).
         作用:指定节点为client,指定客户端接口的绑定地址,包括:HTTP、DNS、RPC。默认是127.0.0.1,只允许回环接口访问。若不指定为-server,其实就是-client
    
      -config-dir=
         Path to a directory to read configuration files from. This
         will read every file ending in '.json' as configuration in this
         directory in alphabetical order. Can be specified multiple times.
    	 作用:指定service的配置文件和检查定义所在的位置。通常会指定为”某一个路径/consul.d”(通常情况下,.d表示一系列配置文件存放的目录)
    
      -config-file=
         Path to a JSON file to read configuration from. Can be specified
         multiple times.
         作用:指定一个要装载的配置文件。该选项可以配置多次,进而配置多个配置文件(后边的会合并前边的,相同的值覆盖)
    
      -config-format=
         Config files are in this format irrespective of their extension.
         Must be 'hcl' or 'json'
    
      -data-dir=
         Path to a data directory to store agent state.
    	 作用:指定agent储存状态的数据目录。这是所有agent都必须的。对于server尤其重要,因为他们必须持久化集群的状态
    
      -dev
         Starts the agent in development mode.
         作用:创建一个开发环境下的server节点。该参数配置下,不会有任何持久化操作,即不会有任何数据写入到磁盘。这种模式不能用于生产环境。
    
      -disable-host-node-id
         Setting this to true will prevent Consul from using information
         from the host to generate a node ID, and will cause Consul to
         generate a random node ID instead.
    
      -disable-keyring-file
         Disables the backing up of the keyring to a file.
    
      -dns-port=
         DNS port to use.
    
      -domain=
         Domain to use for DNS interface.
    
      -enable-script-checks
         Enables health check scripts.
    
      -encrypt=
         Provides the gossip encryption key.
    
      -hcl=
         hcl config fragment. Can be specified multiple times.
    
      -http-port=
         Sets the HTTP API port to listen on.
    
      -join=
         Address of an agent to join at start time. Can be specified
         multiple times.
         作用:将节点加入到集群
    
      -join-wan=
         Address of an agent to join -wan at start time. Can be specified
         multiple times.
    
      -log-level=
         Log level of the agent.
    
      -node=
         Name of this node. Must be unique in the cluster.
         作用:指定节点在集群中的名称。该名称在集群中必须是唯一的(默认采用机器的host)推荐:直接采用机器的IP
    
      -node-id=
         A unique ID for this node across space and time. Defaults to a
         randomly-generated ID that persists in the data-dir.
    
      -node-meta=
         An arbitrary metadata key/value pair for this node, of the format
         `key:value`. Can be specified multiple times.
    
      -non-voting-server
         (Enterprise-only) This flag is used to make the server not
         participate in the Raft quorum, and have it only receive the data
         replication stream. This can be used to add read scalability to
         a cluster in cases where a high volume of reads to servers are
         needed.
    
      -pid-file=
         Path to file to store agent PID.
    
      -protocol=
         Sets the protocol version. Defaults to latest.
    
      -raft-protocol=
         Sets the Raft protocol version. Defaults to latest.
    
      -recursor=
         Address of an upstream DNS server. Can be specified multiple times.
    
      -rejoin
         Ignores a previous leave and attempts to rejoin the cluster.
    
      -retry-interval=
         Time to wait between join attempts.
    
      -retry-interval-wan=
         Time to wait between join -wan attempts.
    
      -retry-join=
         Address of an agent to join at start time with retries enabled. Can
         be specified multiple times.
    
      -retry-join-wan=
         Address of an agent to join -wan at start time with retries
         enabled. Can be specified multiple times.
    
      -retry-max=
         Maximum number of join attempts. Defaults to 0, which will retry
         indefinitely.
    
      -retry-max-wan=
         Maximum number of join -wan attempts. Defaults to 0, which will
         retry indefinitely.
    
      -segment=
         (Enterprise-only) Sets the network segment to join.
    
      -serf-lan-bind=
         Address to bind Serf LAN listeners to.
    
      -serf-wan-bind=
         Address to bind Serf WAN listeners to.
    
      -server
         Switches agent to server mode.
         作用:指定节点为server。每个数据中心(DC)的server数推荐至少为1,至多为5。所有的server都采用raft一致性算法来确保事务的一致性和线性化,事务修改了集群的状态,且集群的状态保存在每一台server上保证可用性。server也是与其他DC交互的门面(gateway)
    
      -syslog
         Enables logging to syslog.
    
      -ui
         Enables the built-in static web UI server.
    
      -ui-dir=
         Path to directory containing the web UI resources.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174

    使用-client 参数可指定允许客户端使用什么ip去访问,例如-client 192.168.1.100 表示可以使用http://192.168.1.100:8500/ui 去访问。

    我们尝试一下:
    consul agent -dev -client 192.168.1.100
    发现果然可以使用http://192.168.1.100:8500/ui 访问了。

    集群部分移步 http://www.zhaoguojian.com/2018/06/23/Consul%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA/
    https://blog.csdn.net/u010046908/article/details/61916389

    集群搭建

    概述

    作为服务发现的几种产品,比较可以查看这里。Consul官方也提供了几种产品之间的比较,点击查看
    在这里插入图片描述

    Consul有很多组件,但总体来说,它是一个发现和配置服务工具,特性:
    服务发现 Service Discovery: Clients of Consul can provide a service, such as api or mysql, and other clients can use Consul to discover providers of a given service. Using either DNS or HTTP, applications can easily find the services they depend upon.

    健康检查 Health Checking: Consul clients can provide any number of health checks, either associated with a given service (“is the webserver returning 200 OK”), or with the local node (“is memory utilization below 90%”). This information can be used by an operator to monitor cluster health, and it is used by the service discovery components to route traffic away from unhealthy hosts.

    key-value存储 KV Store: Applications can make use of Consul’s hierarchical key/value store for any number of purposes, including dynamic configuration, feature flagging, coordination, leader election, and more. The simple HTTP API makes it easy to use.

    多数据中心 Multi Datacenter: Consul supports multiple datacenters out of the box. This means users of Consul do not have to worry about building additional layers of abstraction to grow to multiple regions.

    更详细的信息可以查看官网

    选择了三台服务器,ip地址分别为192.168.1.12、192.168.1.13、192.168.1.14。

    安装Consul

    下载地址是:https://www.consul.io/downloads.html
    从里面选择系统对应的版本,我使用了Linux 64-bit,Consul版本为1.1.0。下载完成后解压缩,只有一个文件,将文件添加到环境变量或者移动到已有环境变量的目录中。

    sudo wget https://releases.hashicorp.com/consul/1.1.0/consul_1.1.0_linux_amd64.zip
    sudo apt-get install unzip
    unzip consul_1.1.0_linux_amd64.zip
    sudo cp consul /usr/local/sbin/
    
    • 1
    • 2
    • 3
    • 4

    三台服务器上都完成上述相同的步骤。

    如果是Mac,可以使用Homebrew进行安装

    brew install consul
    
    • 1

    启动Consul

    192.168.1.12上运行

    consul agent -server -bootstrap -bind=0.0.0.0 -client=192.168.1.12 -data-dir=/home/ubuntu/data -ui -node=s12
    
    • 1

    192.168.1.13上运行

    consul agent -server -bind=0.0.0.0 -client=192.168.1.13 -data-dir=/home/ubuntu/data -ui -node=s13 -join 192.168.1.12
    
    • 1

    192.168.1.14上运行

    consul agent -server -bind=0.0.0.0 -client=192.168.1.14 -data-dir=/home/ubuntu/data -ui -node=s14 -join 192.168.1.12
    
    • 1

    参数说明:

    • server: 以server身份启动。
    • data-dir:data存放的目录
    • node:节点id,在同一集群不能重复。
    • bind:监听的ip地址。
    • client 客户端的ip地址
    • 其他参数见consul官方说明: https://www.consul.io/docs/agent/options.html

    查看

    网页查看:http://192.168.1.12:8500/
    查看Leader: http://192.168.1.12:8500/v1/status/leader
    查看Peers: http://192.168.1.12:8500/v1/status/peers

    版本信息:

    consul -v
    
    • 1

    使用命令有
    consul members:查看集群成员
    consul info:查看当前服务器的状况
    consul leave:退出当前服务集群

  • 相关阅读:
    每日一题 102二叉树的层序遍历
    后端面试话术集锦第 十八 篇:JVM面试话术
    C. Build Permutation(构造,思维)
    大猩猩优化算法(Matlab代码实现)
    数据同步工具DataX介绍和原理
    Docker实用篇
    Java异常、SpringBoot中全局捕获处理异常
    git修改commit历史提交时间、作者
    Linux内核的基础设施:工作队列、等待队列
    性能测试知多少---系统架构分析
  • 原文地址:https://blog.csdn.net/m0_58095675/article/details/126675368