使用Docker安装Kong Gateway

434 阅读2分钟

Kong Gateway是一个云原生、与平台无关、可扩展的 API 网关,以其高性能和通过插件的可扩展性而闻名。

通过提供代理、路由、负载平衡、健康检查、身份验证等功能,Kong 可以作为轻松编排微服务或传统 API 流量的中央层。

接下来带领大家使用Docker安装Kong Gateway。

安装

首先像下面这样创建目录。

|-config/kong.yaml
|-output/
|-docker-compose.yaml
|-POSTGRES_PASSWORD

在中config/kong.yaml,添加以下内容:

# a very minimal declarative config file
_format_version: "2.1"
_transform: true

POSTGRES_PASSWORD中,只需添加您想要的任何密码。例如:

password

然后,将以下代码片段复制到docker-compose.yaml.

version: '3.9'

x-kong-config: &kong-env
  KONG_DATABASE: postgres
  KONG_PG_DATABASE: ${KONG_PG_DATABASE:-kong}
  KONG_PG_HOST: kong-database
  KONG_PG_USER: ${POSTGRES_USER:-kong}
  KONG_PG_PASSWORD_FILE: /run/secrets/kong_postgres_password

services:
  kong-migrations:
    image: kong/kong-gateway:3.3.1.0
    command: kong migrations bootstrap
    depends_on:
      - kong-database
    environment:
      <<: *kong-env
    secrets:
      - kong_postgres_password
    networks:
      - kong-net
    restart: on-failure

  kong-migrations-up:
    image: kong/kong-gateway:3.3.1.0
    command: kong migrations up && kong migrations finish
    depends_on:
      - kong-database
    environment:
      <<: *kong-env
    secrets:
      - kong_postgres_password
    networks:
      - kong-net
    restart: on-failure

  kong-gateway:
    image: kong/kong-gateway:3.3.1.0
    user: "${KONG_USER:-kong}"
    networks:
      - kong-net
    environment:
      <<: *kong-env
      KONG_PROXY_ACCESS_LOG: /dev/stdout
      KONG_ADMIN_ACCESS_LOG: /dev/stdout
      KONG_PROXY_ERROR_LOG: /dev/stderr
      KONG_ADMIN_ERROR_LOG: /dev/stderr
      KONG_ADMIN_LISTEN: 0.0.0.0:${KONG_ADMIN_PORT:-8001}
      KONG_PROXY_LISTEN: 0.0.0.0:${KONG_PROXY_PORT:-8000}
    secrets:
      - kong_postgres_password
    ports:
      - '${KONG_PROXY_PORT:-8000}:8000'
      - '${KONG_ADMIN_PORT:-8001}:8001'
    depends_on:
      - kong-database
    healthcheck:
      test: ["CMD", "kong", "health"]
      interval: 10s
      timeout: 10s
      retries: 10
    restart: on-failure:5
  kong-database:
    image: postgres:13
    environment:
      - POSTGRES_DB=${KONG_PG_DATABASE:-kong}
      - POSTGRES_USER=${POSTGRES_USER:-kong}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-kongpass}
    networks:
      - kong-net
networks:
  kong-net:
    external: true

secrets:
  kong_postgres_password:
    file: ./POSTGRES_PASSWORD

完成上述所有操作后,从终端运行以下命令。

docker compose up -d

此命令将为 Kong Gateway 创建一个 docker 容器。

点击浏览器 - http:/127.0.0.1:8000。在浏览器中看到一些 JSON 输出。

配置

在这一部分中,我们将配置任何 Kong Gateway 安装中最常见的设置。

下面的 CURL 执行的所有输出都将存储在output/目录中。

在此配置结束时,会配置完成以下内容:

  • 使用 API 密钥保护管理 API

    • 创建管理员消费者
    • 为管理 API 创建服务
    • 为管理 API 创建路由
  • 启用每分钟 60 个请求的全局速率限制

  • 启用全局代理缓存 30 秒

导出默认配置

curl -s localhost:8001 | jq '.configuration' > output/configuration-default.json

将全局速率限制设置为每分钟 60 个请求。

curl -X POST http://localhost:8001/plugins \
  --data name=rate-limiting \
  --data config.minute=60 \
  --data config.policy=local -o output/admin-api-rate-limit-global.json

将默认代理缓存设置为 30 秒

curl -X POST http://localhost:8001/plugins \
  --data "name=proxy-cache" \
  --data "config.request_method=GET" \
  --data "config.response_code=200" \
  --data "config.content_type=application/json; charset=utf-8" \
  --data "config.cache_ttl=30" \
  --data "config.strategy=memory" -o output/admin-api-proxy-cache-global.json

创建管理 API 服务

curl --request POST \
  --url http://localhost:8001/services \
  --data name=admin-api-service \
  --data url='http://localhost:8001' -o output/admin-api-service.json

创建管理 API 路由

curl --request POST \
  --url http://localhost:8001/services/admin-api-service/routes \
  --data 'paths[]=/admin-api' \
  --data name=admin-api-route -o output/admin-api-route.json

在管理 API 服务上启用密钥身份验证

curl --request POST \
    --url http://localhost:8001/services/admin-api-service/plugins \
    --header 'Content-Type: application/json' \
    --header 'accept: application/json' \
    --data '{"name":"key-auth","config":{"key_names":["api-key"],"key_in_query":false}}' -o output/admin-api-key.json

创建管理 API 消费者

curl --request POST \
  --url http://localhost:8001/consumers \
  --header 'Content-Type: application/json' \
  --header 'accept: application/json' \
  --data '{"username":"administrator","custom_id":"administrator"}' -o output/admin-api-consumer.json

创建管理员 API 密钥

curl -X POST http://localhost:8001/consumers/administrator/key-auth -o output/admin-api-consumer-key.json

小结

完成后,您可以开始在本地使用 Kong Gateway - 或者如果您想在生产中使用 - 您可以根据您首选的操作系统安装 Kong Gateway,然后只需按照上面给出的步骤进行 Kong Gateway 的最小设置即可。