tjtjtjのメモ

自分のためのメモです

kong 普通に試す

何もわからないがとりあえず進める

network 作って, cassandra 立ち上げて, マイグレーションして、kong 起動

$ docker network create kong-net
$ docker run -d --name kong-database \
               --network=kong-net \
               -p 9042:9042 \
               cassandra:3

docker run --rm \
     --network=kong-net \
     -e "KONG_DATABASE=cassandra" \
     -e "KONG_PG_HOST=kong-database" \
     -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
     kong:latest kong migrations bootstrap

docker run -d --name kong \
     --network=kong-net \
     -e "KONG_DATABASE=cassandra" \
     -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, 0.0.0.0:8444 ssl" \
     -p 8000:8000 \
     -p 8443:8443 \
     -p 8001:8001 \
     -p 8444:8444 \
     kong:latest

kong を確認

$ curl -sS http://localhost:8001/ | jq .
{
  "plugins": {
    "enabled_in_cluster": [],
    "available_on_server": {
      "correlation-id": true,
      "pre-function": true,
      "cors": true,
      "ldap-auth": true,
      "loggly": true,
:
}

configuring-a-service

ここをみながら

https://docs.konghq.com/1.3.x/getting-started/configuring-a-service

1. Add your Service using the Admin API

サービス作成

$ curl -i -X POST \
  --url http://localhost:8001/services/ \
  --data 'name=example-service' \
  --data 'url=http://mockbin.org'

HTTP/1.1 201 Created
Date: Wed, 18 Sep 2019 11:46:54 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/1.3.0
Content-Length: 296
{
  "host":"mockbin.org",
  "created_at":1568807214,
  "connect_timeout":60000,
  "id":"ae3c5ee9-3d44-49bc-8869-1f7f0f58afc1",
  "protocol":"http",
  "name":"example-service",
  "read_timeout":60000,
  "port":80,
  "path":null,
  "updated_at":1568807214,
  "retries":5,
  "write_timeout":60000,
  "tags":null,
  "client_certificate":null
}

サービス確認

$ curl -s http://localhost:8001/services | jq .
{
  "next": null,
  "data": [
    {
      "host": "mockbin.org",
      "created_at": 1568807214,
      "connect_timeout": 60000,
      "id": "ae3c5ee9-3d44-49bc-8869-1f7f0f58afc1",
      "protocol": "http",
      "name": "example-service",
      "read_timeout": 60000,
      "port": 80,
      "path": null,
      "updated_at": 1568807214,
      "retries": 5,
      "write_timeout": 60000,
      "tags": null,
      "client_certificate": null
    }
  ]
}

2. Add a Route for the Service

サービスにルート追加

$ curl -i -X POST \
  --url http://localhost:8001/services/example-service/routes \
  --data 'hosts[]=example.com'

HTTP/1.1 201 Created
Date: Wed, 18 Sep 2019 11:55:08 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/1.3.0
Content-Length: 408

{
  "id":"72a9d29f-6571-43c3-beef-c02e6ccbf699",
  "tags":null,
  "updated_at":1568807708,
  "destinations":null,
  "headers":null,
  "protocols":["http","https"],
  "created_at":1568807708,
  "snis":null,
  "service":{"id":"ae3c5ee9-3d44-49bc-8869-1f7f0f58afc1"},
  "name":null,
  "preserve_host":false,
  "regex_priority":0,
  "strip_path":true,
  "sources":null,
  "paths":null,
  "https_redirect_status_code":426,
  "hosts":["example.com"],
  "methods":null
}

サービス確認。前と同じ

$ curl -s http://localhost:8001/services | jq .
{
  "next": null,
  "data": [
    {
      "host": "mockbin.org",
      "created_at": 1568807214,
      "connect_timeout": 60000,
      "id": "ae3c5ee9-3d44-49bc-8869-1f7f0f58afc1",
      "protocol": "http",
      "name": "example-service",
      "read_timeout": 60000,
      "port": 80,
      "path": null,
      "updated_at": 1568807214,
      "retries": 5,
      "write_timeout": 60000,
      "tags": null,
      "client_certificate": null
    }
  ]
}

こうすると1サービスだけ得られた

$ curl -s http://localhost:8001/services/example-service | jq .
{
  "host": "mockbin.org",
  "created_at": 1568807214,
  "connect_timeout": 60000,
  "id": "ae3c5ee9-3d44-49bc-8869-1f7f0f58afc1",
  "protocol": "http",
  "name": "example-service",
  "read_timeout": 60000,
  "port": 80,
  "path": null,
  "updated_at": 1568807214,
  "retries": 5,
  "write_timeout": 60000,
  "tags": null,
  "client_certificate": null
}

3. Forward your requests through Kong

curl -i -X GET \
  --url http://localhost:8000/ \
  --header 'Host: example.com'

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 10695
Connection: keep-alive
Server: openresty/1.15.8.2
Date: Wed, 18 Sep 2019 11:59:18 GMT
Etag: W/"29c7-XG+PICJmz/J+UYWt5gkKqqAUXjc"
Vary: Accept-Encoding
Via: kong/1.3.0
X-Kong-Upstream-Status: 200
X-Kong-Upstream-Latency: 444
X-Kong-Proxy-Latency: 541
Kong-Cloud-Request-ID: 42851e60164d317e266bc17f39123263

<!DOCTYPE html><html><head><meta charset="utf-8"><title>Mockbin by Kong</title>...