搬瓦工 VPS 部署 APISIX API 网关搭建教程

Apache APISIX 是由 Apache 软件基金会孵化的高性能云原生 API 网关,基于 Nginx 和 etcd 构建,具备动态路由、负载均衡、熔断限流、身份认证等丰富功能。与 Kong 相比,APISIX 采用 etcd 作为配置中心,路由变更无需重启即可生效,性能更优。本教程将介绍如何在搬瓦工 VPS 上使用 Docker Compose 完整部署 APISIX,包括 Dashboard 管理面板的配置。部署前请确保已安装好 Docker 和 Docker Compose

一、APISIX 架构简介

APISIX 的核心架构由以下组件构成:

  • APISIX Core:核心代理引擎,基于 OpenResty 处理请求路由和转发。
  • etcd:分布式键值存储,作为 APISIX 的配置中心,存储路由、上游和插件配置。
  • APISIX Dashboard:可视化管理面板,提供图形化界面管理路由和插件。
  • Admin API:RESTful 管理接口,监听 9180 端口。

APISIX 的路由配置存储在 etcd 中,任何配置变更都会通过 etcd watch 机制实时推送到网关节点,无需重启服务,实现了真正的动态配置。

二、系统要求

  • 操作系统:Ubuntu 20.04+ 或 Debian 11+
  • 内存:至少 1GB,推荐 2GB 以上
  • Docker:已安装 Docker 和 Docker Compose
  • 端口:9080(代理 HTTP)、9443(代理 HTTPS)、9180(Admin API)、9000(Dashboard)

三、Docker Compose 部署

3.1 创建项目目录和配置文件

mkdir -p /opt/apisix && cd /opt/apisix

创建 APISIX 配置文件 apisix_conf/config.yaml

mkdir -p apisix_conf
cat > apisix_conf/config.yaml <<'EOF'
deployment:
  admin:
    admin_key:
      - name: admin
        key: your_admin_api_key_2026
        role: admin
    allow_admin:
      - 0.0.0.0/0
  etcd:
    host:
      - "http://etcd:2379"
    prefix: "/apisix"
    timeout: 30
plugin_attr:
  prometheus:
    export_addr:
      ip: 0.0.0.0
      port: 9091
EOF

3.2 创建 Dashboard 配置文件

mkdir -p dashboard_conf
cat > dashboard_conf/conf.yaml <<'EOF'
conf:
  listen:
    host: 0.0.0.0
    port: 9000
  etcd:
    endpoints:
      - "http://etcd:2379"
    prefix: /apisix
  log:
    error_log:
      level: warn
      file_path: /dev/stderr
authentication:
  secret: your_dashboard_secret_2026
  expire_time: 3600
  users:
    - username: admin
      password: admin_pass_2026
EOF

3.3 编写 docker-compose.yml

version: '3.8'

services:
  etcd:
    image: bitnami/etcd:3.5
    container_name: apisix-etcd
    restart: always
    environment:
      ALLOW_NONE_AUTHENTICATION: "yes"
      ETCD_ADVERTISE_CLIENT_URLS: http://etcd:2379
    volumes:
      - etcd_data:/bitnami/etcd

  apisix:
    image: apache/apisix:3.9-debian
    container_name: apisix
    restart: always
    depends_on:
      - etcd
    volumes:
      - ./apisix_conf/config.yaml:/usr/local/apisix/conf/config.yaml:ro
    ports:
      - "9080:9080"
      - "9443:9443"
      - "127.0.0.1:9180:9180"
      - "9091:9091"

  apisix-dashboard:
    image: apache/apisix-dashboard:3.0-alpine
    container_name: apisix-dashboard
    restart: always
    depends_on:
      - etcd
    volumes:
      - ./dashboard_conf/conf.yaml:/usr/local/apisix-dashboard/conf/conf.yaml:ro
    ports:
      - "127.0.0.1:9000:9000"

volumes:
  etcd_data:

3.4 启动服务

cd /opt/apisix
docker compose up -d

3.5 验证部署

# 检查所有容器状态
docker compose ps

# 测试 Admin API
curl -s http://127.0.0.1:9180/apisix/admin/routes \
  -H 'X-API-KEY: your_admin_api_key_2026' | python3 -m json.tool

四、路由配置管理

4.1 创建上游服务

curl -s http://127.0.0.1:9180/apisix/admin/upstreams/1 \
  -H 'X-API-KEY: your_admin_api_key_2026' \
  -X PUT -d '{
  "type": "roundrobin",
  "nodes": {
    "192.168.1.100:3000": 1,
    "192.168.1.101:3000": 1
  }
}'

4.2 创建路由规则

curl -s http://127.0.0.1:9180/apisix/admin/routes/1 \
  -H 'X-API-KEY: your_admin_api_key_2026' \
  -X PUT -d '{
  "uri": "/api/v1/*",
  "name": "my-api-route",
  "methods": ["GET", "POST", "PUT", "DELETE"],
  "upstream_id": "1"
}'

4.3 测试路由

curl -i http://localhost:9080/api/v1/users

五、常用插件配置

5.1 限流插件 limit-count

curl -s http://127.0.0.1:9180/apisix/admin/routes/1 \
  -H 'X-API-KEY: your_admin_api_key_2026' \
  -X PATCH -d '{
  "plugins": {
    "limit-count": {
      "count": 200,
      "time_window": 60,
      "rejected_code": 429,
      "key_type": "var",
      "key": "remote_addr"
    }
  }
}'

5.2 JWT 认证插件

# 创建消费者并配置 JWT
curl -s http://127.0.0.1:9180/apisix/admin/consumers \
  -H 'X-API-KEY: your_admin_api_key_2026' \
  -X PUT -d '{
  "username": "app_client",
  "plugins": {
    "jwt-auth": {
      "key": "app-key-001",
      "secret": "my_jwt_secret_2026",
      "algorithm": "HS256",
      "exp": 86400
    }
  }
}'

# 在路由上启用 JWT 认证
curl -s http://127.0.0.1:9180/apisix/admin/routes/1 \
  -H 'X-API-KEY: your_admin_api_key_2026' \
  -X PATCH -d '{
  "plugins": {
    "jwt-auth": {}
  }
}'

5.3 Prometheus 监控

# 启用全局 Prometheus 插件
curl -s http://127.0.0.1:9180/apisix/admin/global_rules/1 \
  -H 'X-API-KEY: your_admin_api_key_2026' \
  -X PUT -d '{
  "plugins": {
    "prometheus": {}
  }
}'

# 查看 Prometheus 指标
curl http://localhost:9091/apisix/prometheus/metrics

六、Dashboard 使用

APISIX Dashboard 提供了可视化的管理界面。通过 SSH 隧道访问:

ssh -L 9000:127.0.0.1:9000 root@your-server-ip

在浏览器中打开 http://localhost:9000,使用配置文件中设置的用户名和密码登录。Dashboard 支持图形化管理路由、上游、消费者和插件配置,对不熟悉命令行操作的用户非常友好。

七、生产环境优化

7.1 启用 SSL/TLS

# 通过 Admin API 上传 SSL 证书
curl -s http://127.0.0.1:9180/apisix/admin/ssls/1 \
  -H 'X-API-KEY: your_admin_api_key_2026' \
  -X PUT -d "{
  \"cert\": \"$(cat /etc/letsencrypt/live/api.example.com/fullchain.pem)\",
  \"key\": \"$(cat /etc/letsencrypt/live/api.example.com/privkey.pem)\",
  \"snis\": [\"api.example.com\"]
}"

7.2 日志管理

# 查看 APISIX 日志
docker logs -f apisix

# 查看 etcd 状态
docker exec apisix-etcd etcdctl endpoint health

八、常见问题

etcd 连接失败

如果 APISIX 启动后报 etcd 连接错误,检查 etcd 容器是否正常运行:

docker logs apisix-etcd
docker exec apisix-etcd etcdctl endpoint status

Admin API 返回 401

确保请求头中的 X-API-KEYconfig.yaml 中配置的 admin_key 一致。

总结

Apache APISIX 是高性能的云原生 API 网关,动态配置能力和丰富的插件生态使其成为微服务架构的优秀选择。与 Kong 相比,APISIX 的 etcd 配置中心方案更适合频繁变更路由的场景。如果你需要服务网格能力,可以继续参考 Istio 服务网格教程。选购搬瓦工 VPS 请参考 全部方案,购买时使用优惠码 NODESEEK2026 可享受 6.77% 的折扣,购买链接:bwh81.net

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。