2.1 服务发现 - 2.1.5 Consul常用命令
优质
小牛编辑
132浏览
2023-12-01
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
的选项,如下:
-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(老版本叫-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
访问了。