如何使用docker容器运行Kong API Gateway?

时间:2017-02-13 11:26:11

标签: docker kong

我对Kong API Gateway非常陌生,目前正在尝试使用PostgreSQL作为我的数据库容器来运行Kong容器。

我怎样才能实现这个目标?

6 个答案:

答案 0 :(得分:1)

<强> 1。启动数据库:

 $ docker run -d --name kong-database \
                  -p 5432:5432 \
                  -e "POSTGRES_USER=kong" \
                  -e "POSTGRES_DB=kong" \
                  postgres:9.4

<强> 2。启动孔:

启动Kong容器并将其链接到数据库容器,使用postgres配置KONG_DATABASE环境变量。

$ docker run -d --name kong \
              --link kong-database:kong-database \
              -e "KONG_DATABASE=postgres" \
              -e "KONG_PG_HOST=kong-database" \
              -p 8000:8000 \
              -p 8443:8443 \
              -p 8001:8001 \
              -p 7946:7946 \
              -p 7946:7946/udp \
              kong

3.Verify Kong正在运行:

$ curl http://127.0.0.1:8001

答案 1 :(得分:1)

回答@StefanWalther的问题,这是一个如何使其与docker-compose一起使用的示例:

version: "2.1"
services:
  kong:
    image: kong:latest
    depends_on:
      kong-database:
        condition: service_healthy
    healthcheck:
      test:
        - CMD
        - nc
        - -z
        - localhost
        - "8443"
      retries: 10
    links:
      - kong-database:kong-database
    command: 
      - "kong"
      - "start"
      - "--vv"
    environment:
      - KONG_DATABASE=cassandra
      - KONG_CASSANDRA_CONTACT_POINTS=kong-database
      - KONG_ADMIN_LISTEN=0.0.0.0:8001
      - KONG_ADMIN_LISTEN_SSL=0.0.0.0:8444
      - KONG_NGINX_DAEMON=off
    ports:
      - "443:8443"
      - "8001:8001"
    restart: always
    network_mode: "bridge"
  kong-database:
    image: cassandra:3
    healthcheck:
      test:
        - "CMD-SHELL"
        - "[ $$(nodetool statusgossip) = running ]"
    volumes:
      - ~/kong-database/cassandra:/var/lib/cassandra
    expose:
      - "9042"
    restart: always
    network_mode: "bridge"

另外,您可以添加kongfig来重新配置实例:

  kong-configurer:
    image: mashupmill/kongfig
    depends_on:
      kong:
        condition: service_healthy
    links:
      - kong:kong
    volumes:
      - ~/config.yml:/config.yml:ro
    command: --path /config.yml --host kong:8001
    network_mode: "bridge"

您可以使用以下命令转储要在最后一个容器中使用的配置:

kongfig dump --host localhost:8001 > ~/config.yml

有关Kongfig的更多信息,here

干杯。

答案 2 :(得分:0)

你检查了下面的回购?

https://github.com/Mashape/docker-kong

答案 3 :(得分:0)

您可以关注Kong installation guide。它按预期对我有用。

第1步:启动Postgres容器

docker run -d --name kong-database \
          -p 5432:5432 \
          -e "POSTGRES_USER=kong" \
          -e "POSTGRES_DB=kong" \
          postgres:9.5

第2步:迁移数据库

docker run --rm \
--link kong-database:kong-database \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
kong:latest kong migrations up

第3步:启动孔

docker run -d --name kong \
--link kong-database:kong-database \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=kong-database" \
-e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
-e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
-e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \
-e "KONG_ADMIN_LISTEN_SSL=0.0.0.0:8444" \
-p 8000:8000 \
-p 8443:8443 \
-p 8001:8001 \
-p 8444:8444 \
kong:latest

第4步:验证

curl -i http://localhost:8001/

答案 4 :(得分:0)

这是我自己的docker compose,它工作正常(来自github上kong的docker项目,我使用kong-oidc,您可以选择喜欢的任何版本)。

kong:
    image: kong:1.3.0-alpine-oidc
    container_name: kong
    depends_on:
      - kong-db
    healthcheck:
      test: ["CMD", "kong", "health"]
      interval: 10s
      timeout: 10s
      retries: 10
    restart: on-failure
    ports:
      - "8000:8000" # Listener
      - "8001:8001" # Admin API
      - "8443:8443" # Listener  (SSL)
      - "8444:8444" # Admin API (SSL)
    environment:
      KONG_DATABASE:         postgres
      KONG_PG_HOST:          kong-db
      KONG_PG_PORT:          5432
      KONG_PG_DATABASE:      api-gw
      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_PROXY_LISTEN:     0.0.0.0:8000, 0.0.0.0:8443 ssl
      KONG_ADMIN_LISTEN:     0.0.0.0:8001, 0.0.0.0:8444 ssl
      KONG_PLUGINS:          bundled,oidc
      KONG_LOG_LEVEL:        debug

  kong-migrations:
    image: kong:1.3.0-alpine-oidc
    command: kong migrations bootstrap
    container_name: kong-migrations
    depends_on:
      - kong-db
    environment:
      KONG_DATABASE: postgres
      KONG_PG_DATABASE: api-gw
      KONG_PG_HOST: kong-db
      KONG_PG_PASSWORD:
      KONG_PG_USER: kong
    links:
      - kong-db:kong-db
    restart: on-failure

  kong-migrations-up:
    image: kong:1.3.0-alpine-oidc
    container_name: kong-migrations-up
    command: kong migrations up && kong migrations finish
    depends_on:
      - kong-db
    environment:
      KONG_DATABASE: postgres
      KONG_PG_DATABASE: api-gw
      KONG_PG_HOST: kong-db
      KONG_PG_PASSWORD:
      KONG_PG_USER: kong
    links:
      - kong-db:kong-db
    restart: on-failure

答案 5 :(得分:0)

更新2020

  1. 为容器创建可以相互访问的桥接网络
docker network create my-net
  1. 启动kong-database容器
docker run -d --name kong-database --network my-net  -p 5432:5432  -e "POSTGRES_USER=kong" -e "POSTGRES_HOST_AUTH_METHOD=trust" -e "POSTGRES_DB=kong"  postgres:alpine
  1. 运行临时容器以初始化kong-database的数据
docker run --rm \
    --network my-net \
    -e "KONG_DATABASE=postgres" \
    -e "KONG_PG_HOST=kong-database" \
    kong:latest kong migrations bootstrap
  1. 运行空容器
docker run -d --name kong \
    --network my-net \
    -e "KONG_DATABASE=postgres" \
    -e "KONG_PG_HOST=kong-database" \
    -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
    -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
    -e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
    -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
    -e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \
    -e "KONG_ADMIN_LISTEN_SSL=0.0.0.0:8444" \
    -p 8000:8000 \
    -p 8443:8443 \
    -p 8001:8001 \
    -p 8444:8444 \
    kong:latest
  1. 验证
curl http://127.0.0.1:8001