跳转至

第二章:Kong 部署与配置

Kong 是最流行的开源 API 网关之一,基于 Nginx 和 Lua 构建。

架构

┌─────────────────────────────────────────────────────────────┐
│                     Kong 架构                                │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  ┌─────────────────────────────────────────────────────┐   │
│  │                   Kong Gateway                       │   │
│  │  ┌─────────────────────────────────────────────┐   │   │
│  │  │              Nginx + LuaJIT                 │   │   │
│  │  │  ┌─────────┐  ┌─────────┐  ┌─────────┐     │   │   │
│  │  │  │ Router  │  │ Plugins │  │ Balancer│     │   │   │
│  │  │  └─────────┘  └─────────┘  └─────────┘     │   │   │
│  │  └─────────────────────────────────────────────┘   │   │
│  └─────────────────────────────────────────────────────┘   │
│                           │                                 │
│                           ▼                                 │
│  ┌─────────────────────────────────────────────────────┐   │
│  │              Data Store (PostgreSQL/Cassandra)      │   │
│  │  - 配置存储                                          │   │
│  │  - 插件状态                                          │   │
│  └─────────────────────────────────────────────────────┘   │
│                                                             │
└─────────────────────────────────────────────────────────────┘

部署方式

Docker 部署

# 创建网络
docker network create kong-net

# 部署 PostgreSQL
docker run -d --name kong-database \
  --network=kong-net \
  -p 5432:5432 \
  -e "POSTGRES_USER=kong" \
  -e "POSTGRES_DB=kong" \
  -e "POSTGRES_PASSWORD=kong" \
  postgres:13

# 初始化数据库
docker run --rm --network=kong-net \
  -e "KONG_DATABASE=postgres" \
  -e "KONG_PG_HOST=kong-database" \
  -e "KONG_PG_PASSWORD=kong" \
  kong:3.4 kong migrations bootstrap

# 启动 Kong
docker run -d --name kong \
  --network=kong-net \
  -e "KONG_DATABASE=postgres" \
  -e "KONG_PG_HOST=kong-database" \
  -e "KONG_PG_PASSWORD=kong" \
  -e "KONG_PROXY_LISTEN=0.0.0.0:8000,0.0.0.0:8443 ssl" \
  -e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \
  -p 8000:8000 \
  -p 8443:8443 \
  -p 8001:8001 \
  kong:3.4

Kubernetes 部署

# 使用 Helm
helm repo add kong https://charts.konghq.com
helm repo update

helm install kong kong/kong \
  --namespace kong \
  --create-namespace \
  --set ingressController.installCRDs=false \
  --set proxy.type=LoadBalancer

Kong Operator

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kong
  namespace: kong
spec:
  replicas: 3
  selector:
    matchLabels:
      app: kong
  template:
    spec:
      containers:
      - name: kong
        image: kong:3.4
        env:
        - name: KONG_DATABASE
          value: "off"  # DB-less 模式
        - name: KONG_PROXY_LISTEN
          value: "0.0.0.0:8000,0.0.0.0:8443 ssl"
        - name: KONG_ADMIN_LISTEN
          value: "0.0.0.0:8001"
        ports:
        - containerPort: 8000
        - containerPort: 8443
        - containerPort: 8001

核心概念

Service

代表后端服务:

# 创建 Service
curl -i -X POST http://localhost:8001/services \
  -d "name=user-service" \
  -d "url=http://user-service:8080"

Route

定义请求路由规则:

# 创建 Route
curl -i -X POST http://localhost:8001/services/user-service/routes \
  -d "name=user-route" \
  -d "paths[]=/api/users"

Consumer

代表 API 消费者:

# 创建 Consumer
curl -i -X POST http://localhost:8001/consumers \
  -d "username=client-app"

Plugin

扩展功能:

# 添加插件
curl -i -X POST http://localhost:8001/services/user-service/plugins \
  -d "name=rate-limiting" \
  -d "config.minute=100"

配置管理

声明式配置

# kong.yml
_format_version: "3.0"

services:
- name: user-service
  url: http://user-service:8080
  routes:
  - name: user-route
    paths:
    - /api/users
  plugins:
  - name: rate-limiting
    config:
      minute: 100

- name: order-service
  url: http://order-service:8080
  routes:
  - name: order-route
    paths:
    - /api/orders
  plugins:
  - name: jwt
    config:
      secret_is_base64: false

加载配置

# 启动时加载配置
kong start --nginx-conf=kong.yml

# 或通过 Admin API
curl -i -X POST http://localhost:8001/config \
  -F "config=@kong.yml"

常用插件

认证插件

# JWT 认证
curl -i -X POST http://localhost:8001/services/user-service/plugins \
  -d "name=jwt"

# API Key 认证
curl -i -X POST http://localhost:8001/services/user-service/plugins \
  -d "name=key-auth"

# OAuth2 认证
curl -i -X POST http://localhost:8001/services/user-service/plugins \
  -d "name=oauth2"

安全插件

# IP 限制
curl -i -X POST http://localhost:8001/services/user-service/plugins \
  -d "name=ip-restriction" \
  -d "config.allow=192.168.1.0/24"

# CORS
curl -i -X POST http://localhost:8001/services/user-service/plugins \
  -d "name=cors" \
  -d "config.origins=*"

# WAF
curl -i -X POST http://localhost:8001/services/user-service/plugins \
  -d "name=waf" \
  -d "config.ruleset=OWASP"

流量控制插件

# 限流
curl -i -X POST http://localhost:8001/services/user-service/plugins \
  -d "name=rate-limiting" \
  -d "config.minute=100" \
  -d "config.policy=local"

# 熔断
curl -i -X POST http://localhost:8001/services/user-service/plugins \
  -d "name=request-termination" \
  -d "config.status_code=503" \
  -d "config.message=Service unavailable"

管理界面

Konga

apiVersion: apps/v1
kind: Deployment
metadata:
  name: konga
  namespace: kong
spec:
  replicas: 1
  selector:
    matchLabels:
      app: konga
  template:
    spec:
      containers:
      - name: konga
        image: pantsel/konga:latest
        env:
        - name: NODE_ENV
          value: production
        - name: TOKEN_SECRET
          value: "your-secret"
        ports:
        - containerPort: 1337

Kong Manager

Kong Enterprise 自带管理界面。

小结

Kong 部署要点:

  • 部署方式:Docker、Kubernetes、DB-less
  • 核心概念:Service、Route、Consumer、Plugin
  • 配置管理:声明式配置
  • 常用插件:认证、安全、流量控制

下一章我们将学习 APISIX 的部署与配置。