スポンサーリンク
APIゲートウェイ と言うのは、複数のAPIをまとめたり、APIに特化した機能をもつソフトウェアやサービスのことです。
この手の製品で有名なのは Apigee や AWSのAPIゲートウェイ ですが、オープンソースのものもあります。
それが今回ご紹介する Kong と言うアプリケーションです。(公式ページは こちら )
私は業務ではApigeeを使うことが多いのですが、他のも知っとこうと思い今回Kongを使ってみましたのでその手順をメモします。
公式の手順 を参考にインストールを行います。
流れとしては以下のようになります。
①epelリポジトリの設定
②postgresqlのインストール・初期設定
③kongのインストール・初期設定
まずepelをインストールします。
詳細は こちらの記事 にまとめていますので参考まで。
amazon-linux-extras install epel
こちらの記事: Amazon linux2にpostgresqlをインストールする手順にまとめておりますので参照ください。
上記手順実施後、kong用の初期設定を行います。
まずはpostgresに接続します。
$ sudo su - postgres
$ psql -U postgres
postgres=#
接続できたらkongのユーザ、DBを作成します。postgresのCLIで以下コマンドを実行します。
CREATE USER kong; CREATE DATABASE kong OWNER kong;
次に、postgresqlの認証の設定を変更します。
詳細は こちらの記事 にも書いてありますが、この手順を行わないと初期設定に失敗して結構はまります。。
vim /var/lib/pgsql/data/pg_hba.conf
以下の部分(80行目付近)を修正します。内容は peer
, ident
となっている部分を trust
に変更するというものです。
変更前
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 ident
変更後
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
#####local all all peer # ←ここをコメントアウト
local all all trust # ←ここを追記
# IPv4 local connections:
######host all all 127.0.0.1/32 ident # ←ここもコメントアウト
host all all 127.0.0.1/32 trust # ←ここも追記
設定変更後、postgresを再起動します。
systemctl restart postgresql.service
# ←何も表示されなければOK
これでpostgresqlの設定は完了です。
いよいよ本題、kongのインストールです。
こちらはyumでインストールします。
yum install https://bintray.com/kong/kong-community-edition-aws/download_file?file_path=dists/kong-community-edition-1.0.2.aws.rpm --nogpgcheck
以下のような結果が得られればOKです。
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
kong-community-edition-1.0.2.aws.rpm | 16 MB 00:00:01
Examining /var/tmp/yum-root-cyCtXA/kong-community-edition-1.0.2.aws.rpm: kong-community-edition-1.0.2-1.noarch
Marking /var/tmp/yum-root-cyCtXA/kong-community-edition-1.0.2.aws.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package kong-community-edition.noarch 0:1.0.2-1 will be installed
--> Processing Dependency: openssl098e for package: kong-community-edition-1.0.2-1.noarch
amzn2-core | 2.4 kB 00:00:00
amzn2extra-docker | 1.3 kB 00:00:00
amzn2extra-epel | 1.3 kB 00:00:00
epel/x86_64/metalink | 7.4 kB 00:00:00
epel | 4.7 kB 00:00:00
157 packages excluded due to repository priority protections
--> Running transaction check
---> Package openssl098e.x86_64 0:0.9.8e-29.amzn2.3.2 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
============================================================================================================================================================================================================
Package Arch Version Repository Size
============================================================================================================================================================================================================
Installing:
kong-community-edition noarch 1.0.2-1 /kong-community-edition-1.0.2.aws 47 M
Installing for dependencies:
openssl098e x86_64 0.9.8e-29.amzn2.3.2 amzn2-core 803 k
Transaction Summary
============================================================================================================================================================================================================
Install 1 Package (+1 Dependent package)
Total size: 48 M
Total download size: 803 k
Installed size: 49 M
Is this ok [y/d/N]: y
Downloading packages:
openssl098e-0.9.8e-29.amzn2.3.2.x86_64.rpm | 803 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : openssl098e-0.9.8e-29.amzn2.3.2.x86_64 1/2
Installing : kong-community-edition-1.0.2-1.noarch 2/2
Verifying : openssl098e-0.9.8e-29.amzn2.3.2.x86_64 1/2
Verifying : kong-community-edition-1.0.2-1.noarch 2/2
Installed:
kong-community-edition.noarch 0:1.0.2-1
Dependency Installed:
openssl098e.x86_64 0:0.9.8e-29.amzn2.3.2
Complete!
インストールが完了したら、次に初期設定を行います。
以下コマンドを実行します。
まず、kongのunixユーザを作成します。(rootで実行してください)
useradd -d /home/kong -s /bin/bash -m kong
作成が完了したらsuします
su - kong
$
次に初期設定のコマンドを実行します。
kong migrations bootstrap
このコマンドでエラーが出なければ起動します。
kong start
特にエラーが出なければ起動完了です。
kongはkong自体の設定もAPIで行いますので、APIで設定を取得できます。
ですので応答が返ってくればkongのインストール成功です。
以下コマンドで確認します。
curl -X GET http://localhost:8001
以下のような結果が返ってくればOKです。
{"plugins":{"enabled_in_cluster":[],"available_on_server":{"response-transformer":true,"oauth2":true,"acl":true,"correlation-id":true,"pre-function":true,"jwt":true,"cors":true,"ip-restriction":true,"basic-auth":true,"key-auth":true,"rate-limiting":true,"request-transformer":true,"http-log":true,"file-log":true,"hmac-auth":true,"ldap-auth":true,"datadog":true,"tcp-log":true,"zipkin":t
~~~以下略~~~
以上となります。
スポンサーリンク