目標
CentOSへKongをインストールする。
マイクロサービスをAPI Gatewayパターンで実装するため、API Gatewayの構築を考えている。
KongがマイクロサービスのAPI Gatewayとして必要な機能を備えているように見えるため、検証のため今回インストールまで行った。
Kongとは
getkong.org
環境
$ cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
公式のInstallationへ行くと、DockerやVagrantなど色々なインストール方法がでてくる。
今回はCentOSの項目に従ってインストールを進めていく。
getkong.org
1. Install the Package
パッケージからインストールを行う。
$ wget https://github.com/Mashape/kong/releases/download/0.10.3/kong-0.10.3.el7.noarch.rpm
$ yum install kong-0.10.3.el7.noarch.rpm
2.Configure your database
DB設定を行う。
Kong supports both PostgreSQL 9.4+ and Cassandra 3.x.x as its datastore.
ということなので、今回はPostgreのv9.6をインストールする。
Postgresql downloadページ
PostgreSQL: Linux downloads (Red Hat family)
Postgresqlのインストールから起動
$ yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
# Install the client packages
$ yum install postgresql96
# Optionally install the server packages:
$ yum install postgresql96-server
# Optionally initialize the database and enable automatic start:
$ /usr/pgsql-9.6/bin/postgresql96-setup initdb
$ systemctl enable postgresql-9.6
$ systemctl start postgresql-9.6
インストール、起動が完了したら、Kong用のDBとDB Userの設定を行う。
$ su - postgres
bash$ psql
$ CREATE USER kong; CREATE DATABASE kong OWNER kong;
$ \q
bash$ psql -l
データベース一覧
名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権
-----------+----------+------------------+-------------+-------------------+-----------------------
kong | kong | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 |
postgres | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 |
template0 | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 行)
3.Start Kong
$ kong start
Error: /usr/local/share/lua/5.1/kong/cmd/start.lua:21: [postgres error] could not get current migrations: [postgres error] FATAL: ユーザ"kong"のIdent認証に失敗しました
上記のエラーが出たので解決する。
/var/lib/pgsql/9.6/data/pg_hba.conf の 82行目を以下のように書き換えた
$vi /var/lib/pgsql/9.6/data/pg_hba.conf
81 # IPv4 local connections:
82 - host all all 127.0.0.1/32 ident
81 # IPv4 local connections:
82 + host all all 127.0.0.1/32 trust
Postgreを再起動して再度Kongを起動する
$ systemctl restart postgresql-9.6
$ kong start
2017/07/24 17:14:17 [info] Kong started
Kongが動いたので、確認のためcurlで8001ポートへリクエストを投げる。
# Kong is running
$ curl 127.0.0.1:8001
{"timers":{"running":0,"pending":5},"configuration":{"admin_error_log":"logs\/....
正常に動作していれば、上記のようなjsonレスポンスが返ってくる。
実際にAPIを登録してみる
以下3つのキーを指定する
http://httpbin.org/ip を登録してみる。
$ curl -i -X POST \
> --url http://localhost:8001/apis \
> --data 'name=example-api' \
> --data 'upstream_url=http://httpbin.org/ip' \
> --data 'uris=/httpbin/ip' \
HTTP/1.1 201 Created
Date: Mon, 24 Jul 2017 10:25:00 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.10.3
{"http_if_terminated":true,"id":"9f15037b-c303-4da6-ae12-465d48998f79","retries":5,"preserve_host":false,"created_at":1500891900000,"upstream_connect_timeout":60000,"upstream_url":"http:\/\/httpbin.org\/ip","upstream_read_timeout":60000,"https_only":false,"upstream_send_timeout":60000,"strip_uri":true,"name":"example-api","uris":["\/httpbin\/ip"]}
以下のリクエストを送信する。
$ curl -i -X GET \
> --url http://localhost:8000/httpbin/ip \
>
HTTP/1.1 200 OK
Date: Mon, 24 Jul 2017 10:27:25 GMT
Content-Type: application/json
Content-Length: 44
Connection: keep-alive
Server: meinheld/0.6.1
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-Powered-By: Flask
X-Processed-Time: 0.000926971435547
Via: kong/0.10.3
X-Kong-Upstream-Latency: 335
X-Kong-Proxy-Latency: 264
{
"origin": "127.0.0.1, xxx.xxx.xxx.xxx"
}
すると、localhost:8000へのリクエストがhttpbin.org/ipへ転送され、その結果が返ってくる。
削除する場合はDELETEメソッドを使う
$ curl -i -X DELETE \ --url http://localhost:8001/apis/example-api
まとめ
検証はこれから。