Dockerizing KONG以自动方式运行并动态设置上游URL

时间:2018-03-27 07:46:09

标签: docker spring-boot docker-compose yaml kong

我正在尝试编写一个用于停泊KONG的yaml文件。设置KONG的要求是我想要设置两个上游URL的基本要求(当相应的请求到来时,它指向这些URL)。 我创建的YAML文件是:

version: "2"

services:
  postgres:
    image: postgres:9.4
    container_name: kong-database
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=kong
      - POSTGRES_DB=kong
web:
    image: kong:0.12-alpine
    container_name: kong
    environment:
      - KONG_DATABASE=postgres
      - KONG_PG_HOST=kong-database
      - KONG_CASSANDRA_CONTACT_POINTS=kong-database
      - 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:8001
      - KONG_ADMIN_LISTEN_SSL=0.0.0.0:8444  
    restart: always
    ports:
      - "8000:8000"
      - "8443:8443"
      - "8001:8001"
      - "8444:8444"
    links:
      - kong-database:kong-database
 kong:
    admin_host: 192.168.99.100:8001
    apis:
      - name:onboarding-api
        host:
          - eso.com.onboarding
        upstream_url: http://192.168.99.100:8080
        - name:orchestrationcockpit-api
        host:
          - eso.com.orchestrationcockpit
        upstream_url: http://192.168.99.100:808

基于设置KONG泊坞窗命令:

 1) run Kong in Docker:
docker run -d --name kong-database \
-p 5432:5432 \
-e "POSTGRES_USER=kong" \
-e "POSTGRES_DB=kong" \
postgres:9.4

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

docker run -it --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:0.12-alpine

2) Run the attached docker-compose.yml files to start Onboarding and OrchestrationCockpit containers (these should be built first by runnning mvn clean install in the repositories. Then verify that the services are running o:

curl -i -X GET \
--url http://192.168.99.100:8080/onboarding/v1

curl -i -X GET \
--url http://192.168.99.100:8081/orchestrationcockpit/v1

3) Add the services to the Kong access gateway
curl -i -X POST \
--url http://192.168.99.100:8001/apis/ \
--data 'name=onboarding-api' \
--data 'hosts=eso.com.onboarding' \
--data 'upstream_url=http://192.168.99.100:8080'

curl -i -X POST \
--url http://192.168.99.100:8001/apis/ \
--data 'name=orchestrationcockpit-api' \
--data 'hosts=eso.com.orchestrationcockpit' \
--data 'upstream_url=http://192.168.99.100:8081'

curl -i http://192.168.99.100:8001/apis/

4) Verify that the services can be accessed from the gateway:
curl -i -X GET \
--url http://192.168.99.100:8000/onboarding/v1 \
--header 'Host: eso.com.onboarding'

curl -i -X GET \
--url http://192.168.99.100:8000/orchestrationcockpit/v1 \
--header 'Host: eso.com.orchestration'

我做得对吗?此外,如果我不知道上游URL我怎么能动态设置它们?即当要在生产环境中设置KONG时我需要更改IP地址,有没有更好的方法来做到这一点而无需硬编码IP地址?

错误:添加API和上游URl不正确。使用YAML为KONG添加上游URL的正确方法是什么?使用的操作系统:Linux和Windows。

1 个答案:

答案 0 :(得分:0)

没有简单的方法可以直接通过docker-compose在Kong中设置路线。

但是,您可以创建一个容器并在其中运行一个脚本,该脚本在脚本中执行curl命令。