跳到主要内容
版本:3.10.x

使用 Docker Compose 部署

Docker Compose 是快速原型验证、本地开发和小规模测试 API7 网关的便捷方式。在当前工作流中,Docker Compose 会在单台主机上启动控制面组件,而 Dashboard 会生成用于引导数据面网关的 Docker 命令,并包含所需的 mTLS 证书。

备注

Docker Compose 仅适用于开发和测试。生产环境请使用 Kubernetes高可用部署。

前提条件

第 1 步:创建目录结构

创建工作目录,并为每个组件的配置创建子目录:

mkdir -p api7-ee/{dashboard_conf,dp_manager_conf}
cd api7-ee

第 2 步:编写控制面配置文件

API7 网关控制面组件会从 YAML 文件读取配置。创建以下文件,并在第 4 步将其挂载到容器中。

dashboard_conf/conf.yaml

该文件配置集成 Dashboard 和 Admin API。database.dsn 的值必须与 Compose 文件中设置的 PostgreSQL凭据一致。

dashboard_conf/conf.yaml
server:
listen:
disable: true
host: "0.0.0.0"
port: 7080
tls:
disable: false
host: "0.0.0.0"
port: 7443
key_file: ""
cert_file: ""
status:
disable: false
host: "127.0.0.1"
port: 7081
pprof:
enable: true
host: "127.0.0.1"
port: 6060

log:
level: warn
output: stderr
access_log: stdout

database:
dsn: "postgres://api7ee:YOUR_DB_PASSWORD@postgresql:5432/api7ee"
max_open_conns: 30
max_idle_time: 30s
# max_lifetime: 60s
timeout: 5s

session_options_config:
same_site: "lax"
secure: false
max_age: 86400

prometheus:
addr: "http://prometheus:9090"
query_path_prefix: ""
whitelist:
- "/api/v1/query_range"
- "/api/v1/query"
- "/api/v1/format_query"
- "/api/v1/series"
- "/api/v1/labels"
- "/api/v1/labels/.*/values"

jaeger:
addr: "http://jaeger:16686"
timeout: 30s

audit:
retention_days: 60

consumer_proxy:
enable: false
cache_success_count: 512
cache_success_ttl: 60
cache_failure_count: 512
cache_failure_ttl: 60

developer_proxy:
cache_success_count: 256
cache_success_ttl: 15
cache_failure_count: 256
cache_failure_ttl: 15

security:
trusted_proxies: ["0.0.0.0/0", "::/0"]
ip_restriction:
allow_list: []
deny_list: []
message: "Access denied"
response_code: 403

dp_manager_conf/conf.yaml

该文件配置 DP Manager。DP Manager 会在 7943 端口暴露兼容 etcd 的 API,数据面会连接到该端口。

dp_manager_conf/conf.yaml
server:
listen:
host: "0.0.0.0"
port: 7900
tls:
host: "0.0.0.0"
port: 7943
status:
disable: false
host: "127.0.0.1"
port: 7901
pprof:
enable: true
host: "127.0.0.1"
port: 6060

log:
level: warn
output: stderr
access_log: stdout

database:
dsn: "postgres://api7ee:YOUR_DB_PASSWORD@postgresql:5432/api7ee"
max_open_conns: 30
max_idle_time: 30s
timeout: 5s

prometheus:
addr: "http://prometheus:9090"
remote_write_path: "/api/v1/write"

jaeger:
collector_addr: "http://jaeger:4318"
timeout: 30s

consumer_cache:
size: 50000
max_ttl: 2h
evict_interval: 5s

developer_cache:
size: 50000
max_ttl: 2h
evict_interval: 5s

rate_limit:
enable: false
time_window: 1
count: 1000

第 3 步:创建 PostgreSQL 数据库

启动 stack 之前,请确保上述 DSN 中引用的数据库用户和数据库已经存在。内置 PostgreSQL 镜像会在首次启动时根据环境变量创建它们,因此只需确保 conf.yaml 文件中的值与环境变量一致。

第 4 步:创建 Docker Compose 文件

在同一目录中创建 docker-compose.yaml

docker-compose.yaml
services:
prometheus:
image: api7/prometheus:2.48.1-debian-11-r0
hostname: prometheus
user: root
command:
- --config.file=/opt/bitnami/prometheus/conf/prometheus.yml
- --web.enable-remote-write-receiver
healthcheck:
test: ["CMD", "/opt/bitnami/prometheus/bin/promtool", "check", "healthy"]
interval: 10s
timeout: 10s
retries: 3
start_period: 10s
networks:
- api7

postgresql:
image: api7/postgresql:15.4.0-debian-11-r45
hostname: postgresql
user: root
environment:
POSTGRES_USER: api7ee
POSTGRES_PASSWORD: YOUR_DB_PASSWORD
POSTGRES_DB: api7ee
healthcheck:
test: ["CMD", "pg_isready", "-U", "api7ee"]
interval: 10s
timeout: 10s
retries: 3
start_period: 10s
networks:
- api7

jaeger:
image: cr.jaegertracing.io/jaegertracing/jaeger:2.14.1
hostname: jaeger
restart: always
networks:
- api7

dashboard:
image: api7/api7-ee-3-integrated:${API7_VERSION}
hostname: dashboard
restart: always
volumes:
- ./dashboard_conf/conf.yaml:/usr/local/api7/conf/conf.yaml:ro
command:
- -c
- /nodejs/bin/node /app/server.js & /usr/local/api7/api7-ee-dashboard -c /usr/local/api7/conf/conf.yaml
ports:
- "7080:7080"
- "7443:7443"
healthcheck:
test:
- CMD
- /nodejs/bin/node
- -e
- "const net=require('net');const socket=net.connect(7443,'127.0.0.1');socket.on('connect',()=>{socket.end();process.exit(0)});socket.on('error',()=>process.exit(1));setTimeout(()=>process.exit(1),3000);"
interval: 10s
timeout: 5s
retries: 12
depends_on:
prometheus:
condition: service_healthy
postgresql:
condition: service_healthy
networks:
- api7

dp-manager:
image: api7/api7-ee-dp-manager:${API7_VERSION}
hostname: dp-manager
restart: always
volumes:
- ./dp_manager_conf/conf.yaml:/usr/local/api7/conf/conf.yaml:ro
command:
- /usr/local/api7/api7-ee-dp-manager
- -c
- /usr/local/api7/conf/conf.yaml
ports:
- "7900:7900"
- "7943:7943"
depends_on:
dashboard:
condition: service_healthy
networks:
- api7

networks:
api7:
driver: bridge

第 5 步:设置镜像版本和密钥

替换 docker-compose.yamlconf.yaml 文件中的以下占位符:

占位符描述
${API7_VERSION}API7 网关控制面发布标签,例如 3.9.10。在生产环境中始终固定为明确版本,而不要使用 latest
YOUR_DB_PASSWORD强 PostgreSQL 密码。它必须在 docker-compose.yaml 和两个 conf.yaml DSN 中保持一致。

运行 Compose 前,可以导出控制面版本:

export API7_VERSION=3.9.10

第 6 步:启动服务

docker compose up -d

检查容器状态:

docker compose ps

所有控制面服务应在一分钟内进入 Up(或 healthy)状态。

第 7 步:应用 license

打开浏览器并访问 https://localhost:7443。使用默认凭证(admin / admin)登录。首次登录时,Dashboard 会提示你重置密码。随后,Dashboard 会打开 Activate License 页面,你可以在该页面上传并激活 license。

第 8 步:验证控制面

确认 Dashboard 和 DP Manager 可访问:

curl -k -I https://localhost:7443
curl http://localhost:7900/version

第 9 步:添加数据面网关

在 API7 Dashboard 中,选择一个网关组(例如 default),进入 Gateway Instances,然后点击 Add Gateway Instance。选择 Docker 并生成部署命令。

生成的命令包含数据面所需的环境变量,包括:

  1. API7_CONTROL_PLANE_ENDPOINTS
  2. API7_GATEWAY_GROUP_SHORT_ID
  3. API7_CONTROL_PLANE_CERT
  4. API7_CONTROL_PLANE_KEY
  5. API7_CONTROL_PLANE_CA

运行生成的 docker run 命令。如果控制面在同一台机器上通过 Docker Compose 运行,请确保生成命令中的 DP Manager 地址指向网关容器可访问的地址,例如 Docker Desktop 上的 https://host.docker.internal:7943

第 10 步:验证数据面

在 API7 Dashboard 中,进入 Gateway Groups > default > Gateway Instances,确认实例已连接且健康。你也应能直接访问数据面:

curl -i "http://localhost:9080/"

此时预期会返回 404 Not Found,这表明数据面正在运行,并已准备好在你创建第一个路由后接收流量。

清理

要停止控制面容器并删除 PostgreSQL volume,请运行:

docker compose down -v

如果你使用生成的 docker run 命令启动了网关实例,请单独删除它:

docker rm -f <gateway-container-name>

后续步骤