Docker 部署 Consul
使用 Docker Compose 快速创建本地 Consul 服务是最推荐的方式。
Docker Compose 配置
# docker-compose.yml
services:
consul:
image: hashicorp/consul:latest
container_name: consul
ports:
- "8500:8500" # HTTP API + Web UI
- "8600:8600/udp" # DNS
volumes:
- ./consul/data:/consul/data
- ./consul/config:/consul/config
command: agent -server -bootstrap-expect=1 -ui -client=0.0.0.0
yaml
关键配置说明:
| 配置项 | 说明 |
|---|---|
8500:8500 | HTTP API 端口,Web UI 也通过此端口访问 |
8600:8600/udp | DNS 服务端口 |
agent -server | 以 Server 模式运行 |
-bootstrap-expect=1 | 单节点模式(开发环境适用) |
-ui | 启用 Web 管理界面 |
-client=0.0.0.0 | 允许所有网络访问 |
启动服务
docker compose up -d
bash
验证
浏览器访问 http://localhost:8500 即可打开 Consul 的 Web 管理界面。
镜像选择
Docker Hub 上有两个常用的 Consul 镜像:
| 镜像 | 说明 |
|---|---|
hashicorp/consul | 官方镜像,推荐使用 |
bitnami/consul | Bitnami 出品,提供更多环境变量配置选项 |
学习阶段使用官方镜像即可。生产环境可以考虑 Bitnami 镜像,它提供了更丰富的 Docker Compose 配置示例。
生产环境注意事项
单节点模式仅适用于开发和学习。生产环境需要:
- 多节点集群:至少 3 个 Server 节点保证高可用
- 数据持久化:将
data和config目录映射到宿主机 - ACL 配置:启用访问控制列表,限制客户端权限
- TLS 加密:节点间通信启用 TLS
# 生产环境示例(3 节点集群)
services:
consul-server-1:
image: hashicorp/consul:latest
command: agent -server -bootstrap-expect=3 -node=consul-server-1
# ...
consul-server-2:
image: hashicorp/consul:latest
command: agent -server -bootstrap-expect=3 -node=consul-server-2 -join=consul-server-1
# ...
consul-server-3:
image: hashicorp/consul:latest
command: agent -server -bootstrap-expect=3 -node=consul-server-3 -join=consul-server-1
# ...
yaml
↑