Consul CLI
HashiCorp Consul 命令行工具参考指南。
Quick Start
CODEBLOCK0
Global Options
CODEBLOCK1
常用全局环境变量:
- -
CONSUL_HTTP_ADDR - Agent 地址 (默认 http://127.0.0.1:8500) - INLINECODE2 - ACL Token
- INLINECODE3 - Token 文件路径
- INLINECODE4 - 启用 HTTPS
- INLINECODE5 - SSL 证书验证 (默认 true)
- INLINECODE6 - CA 证书路径
- INLINECODE7 - 命名空间 (Enterprise)
Agent Commands
启动 Agent
CODEBLOCK2
Agent 常用参数:
| 参数 | 说明 |
|---|
| INLINECODE8 | 开发模式 |
| INLINECODE9 |
服务器模式 |
|
-bootstrap-expect=N | 期望服务器数量 |
|
-data-dir=<path> | 数据目录 |
|
-bind=<IP> | 绑定地址 |
|
-client=<IP> | 客户端接口地址 |
|
-ui | 启用 Web UI |
|
-join=<addr> | 加入集群 (已弃用,用 -retry-join) |
|
-retry-join=<addr> | 重试加入集群 |
|
-datacenter=<name> | 数据中心名称 |
|
-node=<name> | 节点名称 |
|
-config-dir=<path> | 配置目录 |
|
-config-file=<path> | 配置文件 |
|
-encrypt=<key> | 加密密钥 |
管理操作
CODEBLOCK3
KV Commands
CODEBLOCK4
子命令
CODEBLOCK5
Catalog Commands
CODEBLOCK6
子命令
CODEBLOCK7
Services Commands
CODEBLOCK8
子命令
CODEBLOCK9
ACL Commands
CODEBLOCK10
Bootstrap
CODEBLOCK11
Policy 管理
CODEBLOCK12
策略规则示例:
CODEBLOCK13
Token 管理
CODEBLOCK14
Role 管理
CODEBLOCK15
Operator Commands
CODEBLOCK16
Raft 管理
CODEBLOCK17
区域管理
CODEBLOCK18
Snapshot Commands
CODEBLOCK19
Connect / Service Mesh Commands
CODEBLOCK20
Intention 管理
CODEBLOCK21
代理配置
CODEBLOCK22
Debug & Monitoring Commands
Monitor
CODEBLOCK23
Debug
CODEBLOCK24
Info
CODEBLOCK25
RTT
CODEBLOCK26
Event & Exec Commands
Event
CODEBLOCK27
Exec
CODEBLOCK28
Lock Command
CODEBLOCK29
Watch Command
CODEBLOCK30
TLS Commands
CODEBLOCK31
生成证书
CODEBLOCK32
Validate Command
CODEBLOCK33
Common Patterns
健康检查脚本
CODEBLOCK34
服务发现
CODEBLOCK35
集群启动脚本
CODEBLOCK36
Exit Codes
错误 |
| 2 | 连接错误 |
| 3 | 速率限制 |
References
For detailed command reference, see:
Consul CLI
HashiCorp Consul 命令行工具参考指南。
快速开始
bash
查看所有命令
consul
查看命令帮助
consul
-h
consul -h
启用自动补全
consul -autocomplete-install
全局选项
bash
consul [--version] [--help] []
常用全局环境变量:
- - CONSULHTTPADDR - Agent 地址 (默认 http://127.0.0.1:8500)
- CONSULHTTPTOKEN - ACL Token
- CONSULHTTPTOKENFILE - Token 文件路径
- CONSULHTTPSSL - 启用 HTTPS
- CONSULHTTPSSLVERIFY - SSL 证书验证 (默认 true)
- CONSULCACERT - CA 证书路径
- CONSULNAMESPACE - 命名空间 (企业版)
Agent 命令
启动 Agent
bash
开发模式 (快速测试)
consul agent -dev
开发模式 + 允许外部访问
consul agent -dev -client=0.0.0.0
服务器模式
consul agent -server \
-bootstrap-expect=3 \
-data-dir=/opt/consul/data \
-bind=192.168.1.1 \
-client=0.0.0.0 \
-ui
加入集群
consul agent -config-dir=/etc/consul.d \
-retry-join=provider=aws tagkey=consul tagvalue=server
Agent 常用参数:
服务器模式 |
| -bootstrap-expect=N | 期望服务器数量 |
| -data-dir= | 数据目录 |
| -bind= | 绑定地址 |
| -client= | 客户端接口地址 |
| -ui | 启用 Web UI |
| -join= | 加入集群 (已弃用,用 -retry-join) |
| -retry-join= | 重试加入集群 |
| -datacenter= | 数据中心名称 |
| -node= | 节点名称 |
| -config-dir= | 配置目录 |
| -config-file= | 配置文件 |
| -encrypt= | 加密密钥 |
管理操作
bash
重载配置
consul reload
优雅停止
consul leave
强制节点离开
consul force-leave
查看成员
consul members
consul members -detailed
consul members -wan
consul members -status=alive
查看信息
consul info
KV 命令
bash
consul kv [options] [args]
子命令
bash
写入
consul kv put
consul kv put redis/config/connections 5
consul kv put -flags=123 mykey myvalue # 附加 flags
读取
consul kv get
consul kv get redis/config/connections
consul kv get -detailed # 详细信息
递归读取
consul kv get -recurse
consul kv get -recurse redis/
删除
consul kv delete
consul kv delete -recurse
导出/导入
consul kv export > backup.json
consul kv import @backup.json
锁操作
consul kv put -acquire=
consul kv put -release=
Catalog 命令
bash
consul catalog [options]
子命令
bash
列出数据中心
consul catalog datacenters
列出节点
consul catalog nodes
consul catalog nodes -service=redis
consul catalog nodes -detailed
列出服务
consul catalog services
consul catalog services -node=
Services 命令
bash
consul services [options]
子命令
bash
注册服务 (命令行)
consul services register -name=web
consul services register -name=redis -tag=primary -port=6379
注册服务 (配置文件)
cat > web.json << EOF
{
Service: {
Name: web,
Tags: [v1, production],
Port: 8080,
Check: {
HTTP: http://localhost:8080/health,
Interval: 10s
}
}
}
EOF
consul services register web.json
注销服务
consul services deregister -id=web
consul services deregister web.json
ACL 命令
bash
consul acl [options]
Bootstrap
bash
初始化 ACL 系统
consul acl bootstrap
返回: AccessorID, SecretID (root token)
Policy 管理
bash
创建策略
consul acl policy create \
-name service-read \
-description Read access to services \
-rules @policy.hcl
列出策略
consul acl policy list
读取策略
consul acl policy read -name service-read
更新策略
consul acl policy update -name service-read -rules @new-policy.hcl
删除策略
consul acl policy delete -name service-read
策略规则示例:
hcl
policy.hcl
service_prefix {
policy = read
}
key_prefix config/ {
policy = write
}
node {
policy = read
}
Token 管理
bash
创建 Token
consul acl token create \
-description My token \
-policy-name service-read
列出 Token
consul acl token list
读取 Token
consul acl token read -accessor-id=
更新 Token
consul acl token update -accessor-id= -description Updated
删除 Token
consul acl token delete -accessor-id=
设置 Agent Token
consul acl set-agent-token default
Role 管理
bash
consul acl role create -name=my-role -policy-name=service-read
consul acl role list
consul acl role read -name=my-role
consul acl role delete -name=my-role
Operator 命令
bash
consul operator [options]
Raft 管理
bash
列出 Raft 节点
consul operator raft list-peers
移除 Raft 节点
consul operator raft remove-peer
集群状态
consul operator autopilot get-config
区域管理
bash
列出区域
consul operator area list
创建区域
consul operator area create -name=us-west
Snapshot 命令
bash
创建快照
consul snapshot save backup.snap
从 Agent 保存
consul snapshot save -http-addr=http://192.168.1.1:8500 backup.snap
恢复快照
consul snapshot restore backup.snap
检查快照
consul snapshot inspect backup.snap
Connect / Service Mesh 命令
bash
consul connect [options]
Intention 管理
bash
列出意图
consul intention list
创建意图
consul intention create web db
consul intention create -deny web malicious
删除意图
consul intention delete web db
检查意图
consul intention check web db
代理配置
bash
生成代理配置
consul connect proxy-config -service=web
调试与监控命令
Monitor
bash
实时日志
consul monitor
指定日志级别
consul monitor -log-level=debug
Debug
bash
收集调试信息
consul debug -duration=30s -output=/tmp/consul-debug
Info
bash
运行时信息
consul info
RTT
bash
网络延迟估算
consul rtt
consul rtt dc1.node1 dc2.node2