介绍

官方文档:https://www.consul.io/docs/intro

 

Consul是一个分布式,高可用的系统。是一种服务网格解决方案,提供具有服务发现,配置和分段功能的全功能控制平面。

 

主要特点如下:

服务发现:Consul的客户端可以注册服务,例如 api或mysql,其他客户端可以使用Consul来发现给定服务的提供者。使用DNS或HTTP,应用程序可以轻松找到它们依赖的服务。

健康检查: Consul客户端可用提供任意数量的健康检查,指定一个服务(比如:webserver是否返回了200 OK 状态码)或者使用本地节点(比如:内存使用是否大于90%).。这个信息可由operator用来监视集群的健康。被服务发现组件用来避免将流量发送到不健康的主机。

Key/Value存储:应用程序可用根据自己的需要使用Consul的层级的Key/Value存储。比如动态配置,功能标记,协调,领袖选举等等,简单的HTTP API让他更易于使用。

多数据中心: Consul支持开箱即用的多数据中心。这意味着用户不需要担心需要建立额外的抽象层让业务扩展到多个区域。Consul面向DevOps和应用开发者友好.是他适合现代的弹性的基础设施。

安全的服务通讯:Consul可以为服务生成和分发TLS证书以建立相互TLS连接。 Intentions可用于定义允许哪些服务进行通信。使用Intentions可以非常方便地管理服务划分(Service segmentation),并且可以实时改变通讯策略,而不必使用复杂的网络拓扑和静态防火墙规则。(用于service mesh)

 

Agent介绍

Consul的Agent是Consul的核心进程,其作用是 维护成员信息,服务注册,运行检查,响应用户请求等。

一个Agent可以运行为Client或Server模式。

Consul Server:通过Raft协议保证强一致性和可用性,每个数据中心至少需要运行一个Server。由于Server会使用较多的系统资源,所以集群中Consul Server的数量一般不会太多。

Consul Client:Client非常轻量,通常服务通过Client进行服务注册和发现。

 

Consul Agent启动参数

版本:Consul v1.8.4

官方地址:https://www.consul.io/docs/agent/options

 

consul agent命名用于启动Agent,consul agent启动时可以通过命令行参数或配置文件指定配置选项

配置的优先级 命令行参数 > 配置文件

常用的命令行参数:
-advertise:用于更改我们通知给集群中其他节点的地址,如果未指定默认使用-bind地址作为通知地址。
-bind:该地址用来在集群内部的通讯,集群内的所有节点到地址都必须是可达的,默认是0.0.0.0

-bootstrap-expect :在一个datacenter中期望提供的server节点数目,当该值提供的时候,consul一直等到达到指定sever数目的时候才会引导整个集群,该标记不能和bootstrap共用

-client:  Consul将绑定客户端接口的地址,包括HTTP、DNS、RPC等服务,默认是127.0.0.1,所以不对外提供服务,如果你要对外提供服务改成0.0.0.0

-config-file: 要加载的配置文件。
-config-dir: 要加载的配置文件目录,里面所有以.json结尾的文件都会被加载
-datacenter:指定该agent使用的数据中心,如果未提供,则默认为“ dc1”
-disable-host-node-id: 将其设置为true将阻止Consul使用主机上的信息来生成确定性节点ID,而是生成一个随机节点ID,该ID将保留在数据目录中。在同一主机上运行多个Consul代理进行测试时,这很有用。
-domain: 设置DNS查询响应的域名,这将会覆盖默认的域名”consul.”
-dns-port:监听的DNS端口。这将覆盖默认端口8600
-grpc-port:监听的gRPC API端口。默认值-1(禁用gRPC)
-http-port:监听的HTTP API端口。这将覆盖默认端口8500
-https-port:用于侦听的HTTPS API端口。默认值-1
-log-file:将所有Consul代理日志消息写入文件。
-join:启动时要加入的另一个Agent的地址。可以多次指定,以指定要加入的多个代理。
-rejoin:类似join,但是允许重新连接,直到成功为止。一旦成功加入成员列表中,它将不再尝试加入。 
# Using a DNS entry
$ consul agent -retry-join "consul.domain.internal"
# Using IPv4
$ consul agent -retry-join "10.0.4.67"
# Using IPv6
$ consul agent -retry-join "[::1]:8301"

-node:节点在集群中的名称,在一个集群中必须是唯一的,默认是该节点的主机名

-server : 定义agent运行在server模式,如果不指定该参数则以client模式运行。
-server-port:要侦听的服务器RPC端口。这将覆盖默认的服务器RPC端口8300
-ui: 启用内置的Web UI服务器和所需的HTTP路由。
-ui-dir: 提供存放web ui资源的路径,该目录必须是可读的

 

 

Consul命令行

版本:Consul v1.8.4

#consul --help
Usage: consul [--version] [--help] <command> [<args>]

Available commands are:
    acl            Interact with Consul's ACLs 与agent交互的ACL
    agent          Runs a Consul agent  运行一个consul agent
   catalog        Interact with the catalog  用于列出条目信息
   【 
     子命令:
     consul catalog datacenters  列出所有的数据中心
     consul catalog nodes  列出所有的节点信息(包括client和server)
     consul catalog  services  列出已注册的服务列表
   】
   config  Interact with Consul's Centralized Configurations 用于与consul的配置进行交互
  【
    子命令:
    Write a config:
    $ consul config write web.serviceconf.hcl

    Read a config:
    $ consul config read -kind service-defaults -name web

    List all configs for a type:
    $ consul config list -kind service-defaults

    Delete a config:
    $ consul config delete -kind service-defaults -name web
   】

    connect        Interact with Consul Connect 用于与consul连接相关的交互
    debug          Records a debugging archive for operators 记录调试信息
    event          Fire a new event  发起新的event
    exec           Executes a command on Consul nodes  在Consul节点上执行一个命令
    force-leave    Forces a member of the cluster to enter the "left" state  强制节点成员在集群中的状态转换到left状态
    info           Provides debugging information for operators.  提供操作的debug级别的信息
    intention      Interact with Connect service intentions  与连接服务的intentions交互
    join           Tell Consul agent to join cluster  加入consul节点到集群中
    keygen         Generates a new encryption key  生成一个新的加密key
    keyring        Manages gossip layer encryption keys
    kv  Interact with the key-value store  与key-value存储进行交互
    【
	  子命令:
      delete    Removes data from the KV store
      export    Exports a tree from the KV store as JSON
      get       Retrieves or lists data from the KV store
      import    Imports a tree stored as JSON to the KV store
      put       Sets or updates data in the KV store
    】

    leave          Gracefully leaves the Consul cluster and shuts down  优雅的退出consul集群
    lock           Execute a command holding a lock  执行一个加锁的命令
    login          Login to Consul using an auth method  使用auth方法登录Consul
    logout         Destroy a Consul token created with login  销毁通过登录创建的Consul token
    maint          Controls node or service maintenance mode 控制节点或服务维护模式
    members        Lists the members of a Consul cluster 列出consul集群的成员信息
    monitor        Stream logs from a Consul agent  来自Consul代理的流日志
    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    与services交互
    snapshot       Saves, restores and inspects snapshots of Consul server state  保存,还原和检查Consul服务器状态的快照
    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的变化