2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

KONG v1.3.0 環境構築とルート設定まで

Posted at

環境

  • vagrant (coreos)
  • docker (docker-compose)
  • postgresql
  • kong (1.3.0)

参考にしたページ

環境構築

vagrant

coreosのVagrantfileをコピーして終了
docker-composeは好きな方法でインストールしてください。

ブラウザとかでもアクセスできるように、8000と8001をフォワード。

docker設定

kongのdockerhubのページにpostgresから設定する方法が全部書いてある。
環境変数とポートの設定をそのまま記述してdocker-compose upする

docker-compose.yml
version: "3"
volumes:
  kong-vol:

services:
  kong-db:
    image: "postgres:9.6"
    container_name: "kong-db"
    environment:
      POSTGRES_USER: "kong"
      POSTGRES_PASSWORD: "kong"
      POSTGRES_DB: "kong"

  kong-migrate:
    image: "kong:latest"
    container_name: "kong-migrate"
    depends_on:
      - "kong-db"
    environment:
      KONG_DATABASE: "postgres"
      KONG_PG_HOST: "kong-db"
      KONG_PG_USER: "kong"
      KONG_PG_PASSWORD: "kong"
      KONG_CASSANDRA_CONTACT_POINTS: "kong-db"
    command: "kong migrations bootstrap"
    restart: "on-failure:3"

  kong:
    image: "kong:latest"
    container_name: "kong"
    environment:
      KONG_DATABASE: "postgres"
      KONG_PG_HOST: "kong-db"
      KONG_PG_USER: "kong"
      KONG_PG_PASSWORD: "kong"
      KONG_CASSANDRA_CONTACT_POINTS: "kong-db"
      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, 0.0.0.0:8444 ssl"
    ports:
      - "8000:8000"
      - "8443:8443"
      - "8001:8001"
      - "8444:8444"
    depends_on:
      - "kong-migrate"
    restart: "on-failure:3"

DBの初期化タイミングの問題でkong-migrateとkongの起動が失敗するためrestartを設定してる。
他にいい方法があったら教えてクレメンス。

service登録

Kong - GettingStarted

管理API(:8001)の /services を叩くとサービス登録できる

$ curl -X POST http://localhost:8001/services --data 'name=example-service' --data 'url=http://mockbin.org' | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                                                                                                                                   
                                 Dload  Upload   Total   Spent    Left  Speed                                                                                                                                                     
100   339  100   296  100    43    977    142 --:--:-- --:--:-- --:--:--   980                                                                                                                                                    
{                                                                                                                                                                                                                                 
  "host": "mockbin.org",                                                                                                                                                                                                          
  "created_at": 1570950317,                                                                                                                                                                                                       
  "connect_timeout": 60000,                                                                                                                                                                                                       
  "id": "18e5e341-14bf-4998-849e-31d7c10194ef",                                                                                                                                                                                   
  "protocol": "http",                                                                                                                                                                                                             
  "name": "example-service",                                                                                                                                                                                                      
  "read_timeout": 60000,                                                                                                                                                                                                          
  "port": 80,                                                                                                                                                                                                                     
  "path": null,                                                                                                                                                                                                                   
  "updated_at": 1570950317,                                                                                                                                                                                                       
  "retries": 5,                                                                                                                                                                                                                   
  "write_timeout": 60000,                                                                                                                                                                                                         
  "tags": null,                                                                                                                                                                                                                   
  "client_certificate": null                                                                                                                                                                                                      
}               

example-serviceというサービスを登録
渡せるパラメータの説明は以下が優しい
Kongの使い方を知る① ~Service & Route編~

route設定

/services/<service name> を叩くとルート設定できる

$ curl -X POST --url http://localhost:8001/services/example-service/routes --data 'hosts[]=localhost' --data 'paths[]=/test' | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   443  100   412  100    31  14531   1093 --:--:-- --:--:-- --:--:-- 14714
{                                                       
  "id": "44f2c7b0-f263-4faa-af5e-681093067d21",
  "tags": null,                                         
  "updated_at": 1570950317,                             
  "destinations": null,                                 
  "headers": null,                                      
  "protocols": [                                        
    "http",                                             
    "https"                                             
  ],                                                    
  "created_at": 1570950317,                             
  "snis": null,                                         
  "service": {                                          
    "id": "18e5e341-14bf-4998-849e-31d7c10194ef"
  },                                                    
  "name": null,                                         
  "preserve_host": false,                               
  "regex_priority": 0,                                  
  "strip_path": true,                                   
  "sources": null,                                      
  "paths": [                                            
    "/test"                                             
  ],                                                    
  "https_redirect_status_code": 426,
  "hosts": [                                            
    "localhost"                                         
  ],                                                    
  "methods": null                                       
}

上記の場合は、localhost:8000/test にアクセスがきた時にさっき登録したexample-serviceのurlにフォワードする設定ができる

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?