Consul安装部署
2026/1/15大约 3 分钟Consul安装部署
Consul 安装部署
一、下载安装
1.1 官方下载
访问 Consul 下载页面 下载对应系统的版本。
1.2 Linux 安装
# 下载
wget https://releases.hashicorp.com/consul/1.17.0/consul_1.17.0_linux_amd64.zip
# 解压
unzip consul_1.17.0_linux_amd64.zip
# 移动到 PATH
sudo mv consul /usr/local/bin/
# 验证安装
consul version1.3 macOS 安装
# 使用 Homebrew
brew install consul
# 验证
consul version1.4 Windows 安装
- 下载 Windows 版本 zip 文件
- 解压到指定目录(如
C:\consul) - 添加到系统 PATH 环境变量
- 打开 CMD 验证:
consul version
1.5 Docker 安装
# 拉取镜像
docker pull hashicorp/consul:1.17.0
# 运行开发模式
docker run -d --name consul \
-p 8500:8500 \
-p 8600:8600/udp \
hashicorp/consul:1.17.0 agent -dev -client=0.0.0.0二、开发模式启动
2.1 快速启动
# 开发模式(单节点,数据存内存)
consul agent -dev
# 允许外部访问
consul agent -dev -client=0.0.0.02.2 访问 Web UI
启动后访问 http://localhost:8500 查看 Consul UI。
2.3 常用端口
| 端口 | 协议 | 说明 |
|---|---|---|
| 8500 | HTTP | HTTP API 和 Web UI |
| 8600 | DNS | DNS 接口 |
| 8300 | TCP | Server RPC |
| 8301 | TCP/UDP | Serf LAN(局域网 Gossip) |
| 8302 | TCP/UDP | Serf WAN(广域网 Gossip) |
三、生产模式部署
3.1 配置文件
// /etc/consul.d/consul.json
{
"datacenter": "dc1",
"data_dir": "/opt/consul/data",
"log_level": "INFO",
"node_name": "consul-server-1",
"server": true,
"bootstrap_expect": 3,
"bind_addr": "192.168.1.101",
"client_addr": "0.0.0.0",
"ui_config": {
"enabled": true
},
"retry_join": [
"192.168.1.101",
"192.168.1.102",
"192.168.1.103"
]
}3.2 配置说明
| 配置项 | 说明 |
|---|---|
datacenter | 数据中心名称 |
data_dir | 数据存储目录 |
node_name | 节点名称 |
server | 是否为 Server 模式 |
bootstrap_expect | 期望的 Server 数量 |
bind_addr | 绑定地址 |
client_addr | 客户端访问地址 |
retry_join | 集群节点地址 |
3.3 启动服务
# 创建数据目录
sudo mkdir -p /opt/consul/data
# 启动 Consul
consul agent -config-dir=/etc/consul.d/
# 或使用 systemd
sudo systemctl start consul3.4 Systemd 服务配置
# /etc/systemd/system/consul.service
[Unit]
Description=Consul Service Discovery Agent
Documentation=https://www.consul.io/
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=consul
Group=consul
ExecStart=/usr/local/bin/consul agent -config-dir=/etc/consul.d/
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target# 启用并启动服务
sudo systemctl daemon-reload
sudo systemctl enable consul
sudo systemctl start consul四、集群部署
4.1 集群架构
4.2 Server 1 配置
// /etc/consul.d/consul.json
{
"datacenter": "dc1",
"data_dir": "/opt/consul/data",
"node_name": "consul-server-1",
"server": true,
"bootstrap_expect": 3,
"bind_addr": "192.168.1.101",
"client_addr": "0.0.0.0",
"ui_config": {
"enabled": true
},
"retry_join": [
"192.168.1.102",
"192.168.1.103"
]
}4.3 Server 2 配置
{
"datacenter": "dc1",
"data_dir": "/opt/consul/data",
"node_name": "consul-server-2",
"server": true,
"bootstrap_expect": 3,
"bind_addr": "192.168.1.102",
"client_addr": "0.0.0.0",
"retry_join": [
"192.168.1.101",
"192.168.1.103"
]
}4.4 Server 3 配置
{
"datacenter": "dc1",
"data_dir": "/opt/consul/data",
"node_name": "consul-server-3",
"server": true,
"bootstrap_expect": 3,
"bind_addr": "192.168.1.103",
"client_addr": "0.0.0.0",
"retry_join": [
"192.168.1.101",
"192.168.1.102"
]
}4.5 Client 配置
{
"datacenter": "dc1",
"data_dir": "/opt/consul/data",
"node_name": "consul-client-1",
"server": false,
"bind_addr": "192.168.1.201",
"retry_join": [
"192.168.1.101",
"192.168.1.102",
"192.168.1.103"
]
}4.6 验证集群
# 查看集群成员
consul members
# 查看集群状态
consul operator raft list-peers
# 查看 Leader
consul info | grep leader五、Docker Compose 部署
5.1 docker-compose.yml
version: '3'
services:
consul-server-1:
image: hashicorp/consul:1.17.0
container_name: consul-server-1
command: agent -server -bootstrap-expect=3 -node=server-1 -bind=0.0.0.0 -client=0.0.0.0 -ui -retry-join=consul-server-2 -retry-join=consul-server-3
ports:
- "8500:8500"
- "8600:8600/udp"
networks:
- consul-net
consul-server-2:
image: hashicorp/consul:1.17.0
container_name: consul-server-2
command: agent -server -bootstrap-expect=3 -node=server-2 -bind=0.0.0.0 -client=0.0.0.0 -retry-join=consul-server-1 -retry-join=consul-server-3
networks:
- consul-net
consul-server-3:
image: hashicorp/consul:1.17.0
container_name: consul-server-3
command: agent -server -bootstrap-expect=3 -node=server-3 -bind=0.0.0.0 -client=0.0.0.0 -retry-join=consul-server-1 -retry-join=consul-server-2
networks:
- consul-net
consul-client:
image: hashicorp/consul:1.17.0
container_name: consul-client
command: agent -node=client-1 -bind=0.0.0.0 -retry-join=consul-server-1 -retry-join=consul-server-2 -retry-join=consul-server-3
networks:
- consul-net
networks:
consul-net:
driver: bridge5.2 启动集群
docker-compose up -d六、常用命令
6.1 Agent 命令
# 启动 Agent
consul agent -config-dir=/etc/consul.d/
# 重新加载配置
consul reload
# 优雅退出
consul leave6.2 成员管理
# 查看成员
consul members
# 查看详细信息
consul members -detailed
# 加入集群
consul join 192.168.1.1016.3 服务管理
# 注册服务
consul services register service.json
# 注销服务
consul services deregister service-id
# 查看服务
consul catalog services6.4 KV 操作
# 写入
consul kv put key value
# 读取
consul kv get key
# 删除
consul kv delete key
# 列出
consul kv get -recurse prefix/七、总结
部署要点:
- 开发环境:使用
-dev模式快速启动 - 生产环境:至少 3 个 Server 节点
- 配置文件:使用 JSON 或 HCL 格式
- 服务管理:使用 systemd 管理进程
- 集群验证:使用
consul members检查状态
