Consul的安装与启动

37 篇文章 0 订阅
17 篇文章 0 订阅

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

Consul的安装与启动

cd /usr/local/bin/
wget https://releases.hashicorp.com/consul/1.0.0/consul_1.0.0_linux_amd64.zip?_ga=2.86515800.1546778999.1510551854-179199314.1510551854
unzip consul_1.0.0_linux_amd64.zip

得到consul 文件,这样就完成了安装。

  • 启动consul

    ./consul agent -dev           # -dev表示开发模式运行,另外还有-server表示服务模式运行
    
输入http://127.0.0.1:8500/ui/ 访问Consul,可查看到默认的ui界面:
  • 尝试访问http://192.168.11.143/ui/ ,会发现无法访问。说明Consul还不能被远程访问.

Consul常用命令

Consul常用命令

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

输入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 files
                           from. This will read every file ending in ".json"
                           as configuration in this directory in alphabetical
                           order. 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 with
                           retries 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, which
                           will retry indefinitely.
  -retry-join-wan=1.2.3.4  Address of an agent to join -wan at start time with
                           retries 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, which
                           will 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

    • 作用:指定机器加入到哪一个数据中心

尝试一下:

consul agent -dev -client 10.128.223.231

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

参考文档

官方文档:https://www.consul.io/docs/agent/options.html

Consul系列博客:http://www.cnblogs.com/java-zhao/p/5378876.html

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值