2.1.5 Consul常用命令

Consul常用命令

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

consul agent 命令详解

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

  1. -advertise=addr Sets the advertise address to use
  2. -advertise-wan=addr Sets address to advertise on wan instead of advertise addr
  3. -atlas=org/name Sets the Atlas infrastructure name, enables SCADA.
  4. -atlas-join Enables auto-joining the Atlas cluster
  5. -atlas-token=token Provides the Atlas API token
  6. -atlas-endpoint=1.2.3.4 The address of the endpoint for Atlas integration.
  7. -bootstrap Sets server to bootstrap mode
  8. -bind=0.0.0.0 Sets the bind address for cluster communication
  9. -http-port=8500 Sets the HTTP API port to listen on
  10. -bootstrap-expect=0 Sets server to expect bootstrap mode.
  11. -client=127.0.0.1 Sets the address to bind for client access.
  12. This includes RPC, DNS, HTTP and HTTPS (if configured)
  13. -config-file=foo Path to a JSON file to read configuration from.
  14. This can be specified multiple times.
  15. -config-dir=foo Path to a directory to read configuration files
  16. from. This will read every file ending in ".json"
  17. as configuration in this directory in alphabetical
  18. order. This can be specified multiple times.
  19. -data-dir=path Path to a data directory to store agent state
  20. -dev Starts the agent in development mode.
  21. -recursor=1.2.3.4 Address of an upstream DNS server.
  22. Can be specified multiple times.
  23. -dc=east-aws Datacenter of the agent (deprecated: use 'datacenter' instead).
  24. -datacenter=east-aws Datacenter of the agent.
  25. -encrypt=key Provides the gossip encryption key
  26. -join=1.2.3.4 Address of an agent to join at start time.
  27. Can be specified multiple times.
  28. -join-wan=1.2.3.4 Address of an agent to join -wan at start time.
  29. Can be specified multiple times.
  30. -retry-join=1.2.3.4 Address of an agent to join at start time with
  31. retries enabled. Can be specified multiple times.
  32. -retry-interval=30s Time to wait between join attempts.
  33. -retry-max=0 Maximum number of join attempts. Defaults to 0, which
  34. will retry indefinitely.
  35. -retry-join-wan=1.2.3.4 Address of an agent to join -wan at start time with
  36. retries enabled. Can be specified multiple times.
  37. -retry-interval-wan=30s Time to wait between join -wan attempts.
  38. -retry-max-wan=0 Maximum number of join -wan attempts. Defaults to 0, which
  39. will retry indefinitely.
  40. -log-level=info Log level of the agent.
  41. -node=hostname Name of this node. Must be unique in the cluster
  42. -protocol=N Sets the protocol version. Defaults to latest.
  43. -rejoin Ignores a previous leave and attempts to rejoin the cluster.
  44. -server Switches agent to server mode.
  45. -syslog Enables logging to syslog
  46. -ui Enables the built-in static web UI server
  47. -ui-dir=path Path to directory containing the Web UI resources
  48. -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 去访问。

我们尝试一下:

  1. consul agent -dev -client 192.168.11.143

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

参考文档

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

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