Consul 简介、安装、常用命令的使用

article/2025/9/20 13:17:58

1 Consul简介

Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其他分布式服务注册与发现的方案,Consul的方案更“一站式”,内置了服务注册与发现框 架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案,不再需要依赖其他工具(比如ZooKeeper等)。使用起来也较 为简单。Consul使用Go语言编写,因此具有天然可移植性(支持Linux、windows和Mac OS X);安装包仅包含一个可执行文件,方便部署,与Docker等轻量级容器可无缝配合 。

2 Consul安装

安装环境:
mac:64bit(查看mac位数:打开终端–>”uname -a”)
consul_0.6.4_darwin_amd64.zip和consul_0.6.4_web_ui.zip,从consul官网https://www.consul.io/downloads.html进行下载就好(选择好OS和位数)

1、解压consul_0.6.4_darwin_amd64.zip
2、将解压后的二进制文件consul(上边画红框的部分拷贝到/usr/local/bin下)

sudo scp consul /usr/local/bin/

说明:使用sudo是因为权限问题。
3、查看是否安装成功,

直接在家目录下执行consul命令即可。出现如下结果,表示安装成功。

lidongdeMacBook-Pro:bin lidong$ consul
usage: consul [--version] [--help] <command> [<args>]Available commands are:agent          Runs a Consul agentconfigtest     Validate config fileevent          Fire a new eventexec           Executes a command on Consul nodesforce-leave    Forces a member of the cluster to enter the "left" stateinfo           Provides debugging information for operatorsjoin           Tell Consul agent to join clusterkeygen         Generates a new encryption keykeyring        Manages gossip layer encryption keyskv             Interact with the key-value storeleave          Gracefully leaves the Consul cluster and shuts downlock           Execute a command holding a lockmaint          Controls node or service maintenance modemembers        Lists the members of a Consul clustermonitor        Stream logs from a Consul agentoperator       Provides cluster-level tools for Consul operatorsreload         Triggers the agent to reload configuration filesrtt            Estimates network round trip time between nodessnapshot       Saves, restores and inspects snapshots of Consul server stateversion        Prints the Consul versionwatch          Watch for changes in Consul

3 Consul 启动

1、执行命令

./consul agent -dev           # -dev表示开发模式运行,另外还有-server表示服务模式运行

查看显示结果:

lidongdeMacBook-Pro:bin lidong$ consul agent -dev
==> Starting Consul agent...
==> Starting Consul agent RPC...
==> Consul agent running!Version: 'v0.7.5'Node ID: 'c67a8d03-deac-35b8-8f68-06ff7b687215'Node name: 'lidongdeMacBook-Pro.local'Datacenter: 'dc1'Server: true (bootstrap: false)Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: falseAtlas: <disabled>==> Log data will now stream in as it occurs:2017/03/13 12:44:31 [DEBUG] Using unique ID "c67a8d03-deac-35b8-8f68-06ff7b687215" from host as node ID2017/03/13 12:44:31 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:127.0.0.1:8300 Address:127.0.0.1:8300}]2017/03/13 12:44:31 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")2017/03/13 12:44:31 [INFO] serf: EventMemberJoin: lidongdeMacBook-Pro.local 127.0.0.12017/03/13 12:44:31 [INFO] consul: Adding LAN server lidongdeMacBook-Pro.local (Addr: tcp/127.0.0.1:8300) (DC: dc1)2017/03/13 12:44:31 [INFO] serf: EventMemberJoin: lidongdeMacBook-Pro.local.dc1 127.0.0.12017/03/13 12:44:31 [INFO] consul: Adding WAN server lidongdeMacBook-Pro.local.dc1 (Addr: tcp/127.0.0.1:8300) (DC: dc1)2017/03/13 12:44:36 [WARN] raft: Heartbeat timeout from "" reached, starting election2017/03/13 12:44:36 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 22017/03/13 12:44:36 [DEBUG] raft: Votes needed: 12017/03/13 12:44:36 [DEBUG] raft: Vote granted from 127.0.0.1:8300 in term 2. Tally: 12017/03/13 12:44:36 [INFO] raft: Election won. Tally: 12017/03/13 12:44:36 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state2017/03/13 12:44:36 [INFO] consul: cluster leadership acquired2017/03/13 12:44:36 [INFO] consul: New leader elected: lidongdeMacBook-Pro.local2017/03/13 12:44:36 [DEBUG] consul: reset tombstone GC to index 32017/03/13 12:44:36 [INFO] consul: member 'lidongdeMacBook-Pro.local' joined, marking health alive2017/03/13 12:44:37 [INFO] agent: Synced service 'consul'2017/03/13 12:44:37 [DEBUG] agent: Node info in sync2017/03/13 12:44:39 [INFO] agent.rpc: Accepted client: 127.0.0.1:540952017/03/13 12:44:50 [DEBUG] http: Request GET /v1/catalog/datacenters (580.862µs) from=127.0.0.1:541142017/03/13 12:44:50 [DEBUG] http: Request GET /v1/catalog/datacenters (34.955µs) from=127.0.0.1:541142017/03/13 12:44:50 [DEBUG] http: Request GET /v1/internal/ui/nodes?dc=dc1&token=<hidden> (1.024476ms) from=127.0.0.1:541122017/03/13 12:44:50 [DEBUG] http: Request GET /v1/coordinate/nodes?dc=dc1&token=<hidden> (361.25µs) from=127.0.0.1:541112017/03/13 12:44:50 [DEBUG] http: Request GET /v1/internal/ui/services?dc=dc1&token=<hidden> (315.405µs) from=127.0.0.1:541112017/03/13 12:44:53 [DEBUG] http: Request GET /v1/internal/ui/nodes?dc=dc1&token=<hidden> (140.095µs) from=127.0.0.1:541112017/03/13 12:44:54 [DEBUG] http: Request GET /v1/kv/?keys&seperator=/&dc=dc1&token=<hidden> (1.368486ms) from=127.0.0.1:541112017/03/13 12:44:55 [DEBUG] http: Request GET /v1/acl/list?dc=dc1&token=<hidden> (9.253µs) from=127.0.0.1:541112017/03/13 12:44:57 [DEBUG] http: Request GET /v1/internal/ui/services?dc=dc1&token=<hidden> (109.196µs) from=127.0.0.1:541112017/03/13 12:44:59 [DEBUG] http: Request GET /v1/internal/ui/services?dc=dc1&token=<hidden> (98.48µs) from=127.0.0.1:541112017/03/13 12:45:58 [DEBUG] agent: Service 'consul' in sync2017/03/13 12:45:58 [DEBUG] agent: Node info in sync2017/03/13 12:47:55 [DEBUG] agent: Service 'consul' in sync2017/03/13 12:47:55 [DEBUG] agent: Node info in sync

说明:

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

lidongdeMacBook-Pro:~ lidong$ consul members
Node                       Address         Status  Type    Build  Protocol  DC
lidongdeMacBook-Pro.local  127.0.0.1:8301  alive   server  0.7.5  2         dc1

说明:

  • Address:节点地址
  • Status:alive表示节点健康
  • Type:server运行状态是server状态
  • DC:dc1表示该节点属于DataCenter1
    注意:

members命令的输出是基于gossip协议的,并且是最终一致的(也就是说,某一个时刻你去运用该命令查到的consul节点的状态信息可能是有误的)

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

4 停止服务(优雅退出)

命令:CTRL+C

^C==> Caught signal: interrupt2017/03/13 12:50:42 [DEBUG] http: Shutting down http server (127.0.0.1:8500)2017/03/13 12:50:42 [INFO] agent: requesting shutdown2017/03/13 12:50:42 [INFO] consul: shutting down server2017/03/13 12:50:42 [WARN] serf: Shutdown without a Leave2017/03/13 12:50:42 [WARN] serf: Shutdown without a Leave2017/03/13 12:50:42 [ERR] dns: error starting tcp server: accept tcp 127.0.0.1:8600: use of closed network connection2017/03/13 12:50:42 [INFO] agent: shutdown complete

说明:

  • 该节点离开后,会通知cluster中的其他节点

注意:

  • 安装部分参考自:https://www.consul.io/intro/getting-started/install.html
  • 启动和停止服务部分参考自:https://www.consul.io/intro/getting-started/agent.html

5 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 的选项,如下:

-advertise=addr          Sets the advertise address to use-advertise-wan=addr      Sets address to advertise on wan instead of advertise addr-atlas=org/name          Sets the Atlas infrastructure name, enables SCADA.-atlas-join              Enables auto-joining the Atlas cluster-atlas-token=token       Provides the Atlas API token-atlas-endpoint=1.2.3.4  The address of the endpoint for Atlas integration.-bootstrap               Sets server to bootstrap mode-bind=0.0.0.0            Sets the bind address for cluster communication-http-port=8500          Sets the HTTP API port to listen on-bootstrap-expect=0      Sets server to expect bootstrap mode.-client=127.0.0.1        Sets the address to bind for client access.This includes RPC, DNS, HTTP and HTTPS (if configured)-config-file=foo         Path to a JSON file to read configuration from.This can be specified multiple times.-config-dir=foo          Path to a directory to read configuration filesfrom. This will read every file ending in ".json"as configuration in this directory in alphabeticalorder. This can be specified multiple times.-data-dir=path           Path to a data directory to store agent state-dev                     Starts the agent in development mode.-recursor=1.2.3.4        Address of an upstream DNS server.Can be specified multiple times.-dc=east-aws             Datacenter of the agent (deprecated: use 'datacenter' instead).-datacenter=east-aws     Datacenter of the agent.-encrypt=key             Provides the gossip encryption key-join=1.2.3.4            Address of an agent to join at start time.Can be specified multiple times.-join-wan=1.2.3.4        Address of an agent to join -wan at start time.Can be specified multiple times.-retry-join=1.2.3.4      Address of an agent to join at start time withretries enabled. Can be specified multiple times.-retry-interval=30s      Time to wait between join attempts.-retry-max=0             Maximum number of join attempts. Defaults to 0, whichwill retry indefinitely.-retry-join-wan=1.2.3.4  Address of an agent to join -wan at start time withretries enabled. Can be specified multiple times.-retry-interval-wan=30s  Time to wait between join -wan attempts.-retry-max-wan=0         Maximum number of join -wan attempts. Defaults to 0, whichwill retry indefinitely.-log-level=info          Log level of the agent.-node=hostname           Name of this node. Must be unique in the cluster-protocol=N              Sets the protocol version. Defaults to latest.-rejoin                  Ignores a previous leave and attempts to rejoin the cluster.-server                  Switches agent to server mode.-syslog                  Enables logging to syslog-ui                      Enables the built-in static web UI server-ui-dir=path             Path to directory containing the Web UI resources-pid-file=path           Path to file to store agent PID

consul agent 命令的常用选项,如下:

  • -data-dir
    • 作用:指定agent储存状态的数据目录
    • 这是所有agent都必须的
    • 对于server尤其重要,因为他们必须持久化集群的状态
  • -config-dir
    • 作用:指定service的配置文件和检查定义所在的位置
    • 通常会指定为”某一个路径/consul.d”(通常情况下,.d表示一系列配置文件存放的目录)
  • -config-file
    • 作用:指定一个要装载的配置文件
    • 该选项可以配置多次,进而配置多个配置文件(后边的会合并前边的,相同的值覆盖)
  • -dev
    • 作用:创建一个开发环境下的server节点
    • 该参数配置下,不会有任何持久化操作,即不会有任何数据写入到磁盘
    • 这种模式不能用于生产环境(因为第二条)
  • -bootstrap-expect
    • 作用:该命令通知consul server我们现在准备加入的server节点个数,该参数是为了延迟日志复制的启动直到我们指定数量的server节点成功的加入后启动。
  • -node
    • 作用:指定节点在集群中的名称
    • 该名称在集群中必须是唯一的(默认采用机器的host)
    • 推荐:直接采用机器的IP
  • -bind
    • 作用:指明节点的IP地址
    • 有时候不指定绑定IP,会报Failed to get advertise address: Multiple private IPs found. Please configure one. 的异常
  • -server
    • 作用:指定节点为server
    • 每个数据中心(DC)的server数推荐至少为1,至多为5
    • 所有的server都采用raft一致性算法来确保事务的一致性和线性化,事务修改了集群的状态,且集群的状态保存在每一台server上保证可用性
    • server也是与其他DC交互的门面(gateway)
  • -client
    • 作用:指定节点为client,指定客户端接口的绑定地址,包括:HTTP、DNS、RPC
    • 默认是127.0.0.1,只允许回环接口访问
    • 若不指定为-server,其实就是-client
  • -join
    • 作用:将节点加入到集群
  • -datacenter(老版本叫-dc,-dc已经失效)
    • 作用:指定机器加入到哪一个数据中心中

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

我们尝试一下:

consul agent -dev -client 192.168.11.143

发现果然可以使用http://192.168.11.143:8500/ui 访问了。

6 Consul 的高可用

Consul Cluster集群架构图如下:
这里写图片描述

这边准备了三台CentOS 7的虚拟机,主机规划如下,供参考:

主机名称IP作用是否允许远程访问
node0192.168.11.143consul server
node1192.168.11.144consul client
node2192.168.11.145consul client

6.1 搭建步骤:

  • 启动node0机器上的Consul(node0机器上执行):
consul agent -data-dir /tmp/node0 -node=node0 -bind=192.168.11.143 -datacenter=dc1 -ui -client=192.168.11.143 -server -bootstrap-expect 1
  • 启动node1机器上的Consul(node1机器上执行):
consul agent -data-dir /tmp/node1 -node=node1 -bind=192.168.11.144 -datacenter=dc1 -ui
  • 启动node2机器上的Consul(node2机器上执行):
consul agent -data-dir /tmp/node2 -node=node2 -bind=192.168.11.145 -datacenter=dc1 -ui -client=192.168.11.145
  • 将node1节点加入到node0上(node1机器上执行):
consul join 192.168.11.143
  • 将node2节点加入到node0上(node2机器上执行):
consul join -rpc-addr=192.168.11.145:8400  192.168.11.143
  • 这样一个简单的Consul集群就搭建完成了,在node1上查看当前集群节点:
consul members -rpc-addr=192.168.11.143:8400

结果如下:

Node   Address              Status  Type    Build  Protocol  DC
node0  192.168.11.143:8301  alive   server  0.7.0  2         dc1
node1  192.168.11.144:8301  alive   client  0.7.0  2         dc1
node2  192.168.11.145:8301  alive   client  0.7.0  2         dc1

说明集群已经搭建成功了。

我们分析一下,为什么第5步和第6步需要加-rpc-addr 选项,而第4步不需要加任何选项呢?原因是-client 指定了客户端接口的绑定地址,包括:HTTP、DNS、RPC,而consul joinconsul members 都是通过RPC与Consul交互的。

6.2 访问集群

这里写图片描述
如上,我们三个节点都加了-ui 参数启动了内建的界面。我们可以通过:http://192.168.11.143:8500/ui/ 或者http://192.168.11.145:8500/ui/进行访问,也可以在node1机器上通过http://127.0.0.1:8500/ui/ 进行访问,原因是node1没有开启远程访问 ,三种访问方式结果是一致的,如下:

consul cluster

7 参考文档:

  1. Consul官方文档:https://www.consul.io/intro/getting-started/install.html

  2. Consul 系列博文:http://www.cnblogs.com/java-zhao/archive/2016/04/13/5387105.html

  3. 使用consul实现分布式服务注册和发现:http://www.tuicool.com/articles/M3QFven


http://chatgpt.dhexx.cn/article/y4tqpdGl.shtml

相关文章

consul下载安装

linux下 sudo yum install -y yum-utils sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo sudo yum -y install consulwindow下 https://developer.hashicorp.com/consul/downloads 然后把里面的exe文件放在gopath下就行了 验…

Consul 入门教程

目录 一、什么是服务发现 二、consul 简介 三、consul的几个概念 四、安装 Consul 五、运行 Consul Agent 六、集群成员 七、停止 Agent 八、注册服务 九、Consul 集群 十、健康检查 十一、KV Data 十二、Consul Web UI 十三、Docker下安装consul 一、什么是服务发…

consul 教程

目录 1.consul安装 1.1 先安装go环境 2.2 安装consul 2.开始使用 2.1运行consul agent 2.2 查看集群成员 2.3 注册服务 2.4 发现服务 3.Consul 集群 3.1 部署集群 3.2 启动代理 3.3 加入集群 3.4 离开集群 4 健康检查 4.1 设置健康检查文件 4.2 查询健康状况 5…

consul部署

docker consul的容器服务更新与发现 服务注册与发现是微服务架构中不可或缺的重要组件。起初服务都是单节点的&#xff0c;不保证高可用性&#xff0c;也不考虑服务的压力承载&#xff0c;服务之间调用单纯的通过接口访问。直到后来出现了多个节点的分布式架构&#xff0c;起初…

【Consul教程】1.Consul简介和安装

简介 consul是HashiCorp 公司基于Go语音开发的微服务工具。基于它Http API方式可以十分方便在系统中实现服务注册于发现&#xff0c;分布式配置中心。支持集群CP模式&#xff0c;一致性算法是Raft。 常见解决方案&#xff1a; SpringBoot Consul 实现注册中心、配置中心。 …

Consul安装使用

最近在写springcloud系列文章&#xff0c;其中注册中心中使用的consul也是文章篇幅之一&#xff0c;这篇文章介绍了我对于consul的安装的一个步骤&#xff0c;可以作为大家在安装使用consul的过程中的一个参考。 1、windows安装 访问官网下载地址&#xff0c;下载windows对应版…

Consul介绍

consul服务注册与发现 了解consul consul是google开源的一个使用go语言开发的服务发现、配置管理中心服务。内置了服务注册与发现框 架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案&#xff0c;不再需要依赖其他工具&#xff08;比如ZooKeeper等&#xff0…

Consul 注册中心介绍

在 Spring Cloud 体系中&#xff0c;几乎每个角色都会有两个以上的产品提供选择&#xff0c;比如在注册中心有&#xff1a;Eureka、Consul、zookeeper、etcd 等&#xff1b;网关的产品有 Zuul、Spring Cloud Gateway 等。在注册中心产品中&#xff0c;最常使用的是 Eureka 和 C…

Consul服务注册与发现

目录 一、Consul简介 &#xff08;一&#xff09;官网 &#xff08;二&#xff09;特点 二、安装并运行Consul &#xff08;一&#xff09;官网安装说明 &#xff08;二&#xff09;下载 &#xff08;三&#xff09;使用开发模式启动 三、服务提供者 四、服务消费者 …

Consul是什么

前言 Consul是一个基于CP的轻量级分布式高可用的系统&#xff0c;提供服务发现、健康检查、K-V存储、多数据中心等功能&#xff0c;不需要再依赖其他组件(Zk、Eureka、Etcd等)。 服务发现&#xff1a;Consul可以提供一个服务&#xff0c;比如api或者MySQL之类的&#xff0c;其他…

Consul的使用

Consul可以用来进行服务发现&#xff0c;我们来测试下Consul的使用 一.Consul基本使用 先需要下载Consul的程序https://www.consul.io/downloads.html 我们这里下载Windows 64版本。 下载后直接解压就行了&#xff0c;里面会有一个consul.exe文件。通过命令可以启动consul.…

Consul 使用手册(感觉比较全了)

使用consul 介绍 Consul包含多个组件,但是作为一个整体,为你的基础设施提供服务发现和服务配置的工具.他提供以下关键特性: 服务发现 Consul的客户端可用提供一个服务,比如 api 或者mysql ,另外一些客户端可用使用Consul去发现一个指定服务的提供者.通过DNS或者HTTP应用程序…

【Consul】Consul架构-简介

Consul是一个复杂的系统&#xff0c;有许多不同的移动部件。为了帮助用户和Consul的开发人员更深入的了解consul是如何工作的&#xff0c;本文介绍consul的系统架构。 高级话题&#xff1a;本节主要讲解consul内部技术细节&#xff0c;使用consul不需要必须了解这些细节的。这…

Consul的介绍、安装与使用

1.常用服务注册与发现框架 常见服务发现框架 Consul、Eureka、 ZooKeeper以及Etcd ZooKeeper是这种类型的项目中历史最悠久的之一&#xff0c;它起源于Hadoop。它非常成熟、可靠&#xff0c;被许多大公司&#xff08;YouTube、eBay、雅虎等&#xff09;使用。其中consul并不需…

【超详细】Consul的安装的使用附多环境配置(傻瓜式教程)

一、Consul概述 Consul 是 HashiCorp 公司推出的开源工具&#xff0c;用于实现分布式系统的服务发现与配置。与其他分布式服务注册与发现的方案&#xff0c;Consul 的方案更“一站式”&#xff0c;内置了服务注册与发现框架、分布一致性协议实现、健康检查、Key/Value 存储、多…

Consul系列:什么是Consul?

引言 Consul 是 HashiCorp 公司推出的开源工具&#xff0c;用于实现分布式系统的服务发现与配置。与其他分布式服务注册与发现的方案&#xff0c; consu1 的方案更“一站式”&#xff0c;内置了服务注册 与发现框架、分布一致性协议实现、健康检查、Key/Value 存储、多数据中心…

Consul安装并运行

Consul安装并运行 一、安装 官网&#xff1a;https://www.consul.io/downloads.html 下载完成后只有一个consul.exe文件&#xff0c;硬盘路径下双击运行 查看 consul 版本信息&#xff1a; consul --version 使用开发模式启动&#xff1a; consul agent -dev 二、运行 通…

Consul

Consul 是 HashiCorp 公司推出的开源工具&#xff0c;用于实现分布式系统的服务发现与配置。与其它分布式服务注册与发现的方案&#xff0c;Consul 的方案更“一站式”&#xff0c;内置了服务注册与发现框架、分布式一致性协议实现、健康检查、Key/Value 存储&#xff08;配置中…

Consul详解

Spring Cloud 支持很多服务发现的软件&#xff0c;Eureka 只是其中之一&#xff0c;下面是 Spring Cloud 支持的服务发现软件以及特性对比&#xff1a; FeatureeuerkaConsulzookeeperetcd服务健康检查可配支持服务状态&#xff0c;内存&#xff0c;硬盘等(弱)长连接&#xff0c…

5分钟让你看懂Consul特性及搭建

一、前言 虽然说牛逼的公司都有那么几个牛逼的运维团队&#xff0c;牛逼的运维团队都有着神秘黑科技般敲代码的姿势&#xff1b;本人虽然不是一个运维工程师&#xff0c;但是有幸自己比较爱倒腾这些东西&#xff0c;也会那么一点点运维知识&#xff0c;虽然不算专业&#xff0…