tyk で API Gateway を作ってみた【パート1: コンポーネント起動編】

tykでAPI Gatewayを作りました。
本ページではDockerを用いたtykコンポーネントの起動手順を紹介します。
##実装環境
```Terminal
$ sw_vers

ProductName:	Mac OS X
ProductVersion:	10.12.6
BuildVersion:	16G1314
```
##参考文献
ほとんどの内容がtykのオフィシャルページに掲載されています。
ページが英語なので、ここでは日本語で紹介します。
また、補足説明を追加しました。

https://github.com/TykTechnologies/tyk
##前提条件
1. dockerがインストールされている
2. gitがインストールされている
3. python 2.7がインストールされている

```Terminal
$ docker version

Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   9ee9f40
 Built:        Thu Apr 26 07:13:02 2018
 OS/Arch:      darwin/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.1-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   9ee9f40
  Built:        Thu Apr 26 07:22:38 2018
  OS/Arch:      linux/amd64
  Experimental: true
```
##手順
1. Tyk Gatewayの起動
2. Tyk Dashboardの起動
3. Tyk Pumpの起動

###1. Tyk Gatewayの起動
####redisコンテナの取得

```Terminal
$ docker pull redis
```
####Tyk Gatewayコンテナの取得

```Terminal
$ docker pull tykio/tyk-gateway
```
####redisの起動
```Terminal
$ docker run -d --name tyk_redis redis
```
#### tyk_gatewayの起動
```terminal
$ docker run --name tyk_gateway -p 8080:8080 --link tyk_redis:redis -v $(pwd)/apps:/opt/tyk-gateway/apps tykio/tyk-gateway

time="May 19 08:32:57" level=error msg="No gRPC URL is set!" 
2018/05/19 08:32:57 grpc: addrConn.resetTransport failed to create client transport: connection error: desc = "transport: No gRPC URL is set!"; Reconnecting to { <nil>}
time="May 19 08:32:57" level=info msg="PIDFile location set to: /var/run/tyk-gateway.pid" 
time="May 19 08:32:57" level=info msg="Initialising Tyk REST API Endpoints" 
time="May 19 08:32:57" level=info msg="Starting Poller" 
time="May 19 08:32:57" level=info msg="Redis connection pools are ready after number of retires" currRetry=0 
time="May 19 08:32:57" level=info msg="Redis connection pools are ready" 
time="May 19 08:32:57" level=info msg="--> Standard listener (http)" port=":8080" 
time="May 19 08:32:57" level=info msg="Setting up Server" 
time="May 19 08:32:57" level=info msg="Gateway started (v2.6.1)" 
time="May 19 08:32:57" level=info msg="Initialising distributed rate limiter" 
time="May 19 08:32:57" level=info msg="--> Listening on address: (open interface)" 
time="May 19 08:32:57" level=info msg="--> Listening on port: 8080" 
time="May 19 08:32:57" level=info msg="--> PID: 9" 
time="May 19 08:32:57" level=info msg="Loading policies" 
time="May 19 08:32:57" level=info msg="Policies found (1 total):" 
time="May 19 08:32:57" level=info msg=" - default" 
time="May 19 08:32:57" level=info msg="Starting gateway rate limiter notifications..." 
time="May 19 08:32:57" level=info msg="Loading API Specification from /opt/tyk-gateway/apps/app_sample.json" 
time="May 19 08:32:57" level=info msg="Detected 1 APIs" 
time="May 19 08:32:57" level=info msg="Preparing new router" 
time="May 19 08:32:57" level=info msg="Initialising Tyk REST API Endpoints" 
time="May 19 08:32:57" level=info msg="Loading API configurations." 
time="May 19 08:32:57" level=info msg="Tracking hostname" api_name="Tyk Test API" domain="(no host)" 
time="May 19 08:32:57" level=info msg="Loading API" api_name="Tyk Test API" 
time="May 19 08:32:57" level=warning msg="The Health Checker is deprecated and we do no longer recommend its use." 
time="May 19 08:32:57" level=info msg="Checking security policy: Token" api_name="Tyk Test API" 
time="May 19 08:32:57" level=info msg="Processed and listening on: /tyk-api-test/{rest:.*}" 
time="May 19 08:32:57" level=info msg="Loading uptime tests..." 
time="May 19 08:32:57" level=info msg="Initialised API Definitions" 
time="May 19 08:32:57" level=info msg="API reload complete" 
time="May 19 08:32:58" level=error msg="No gRPC URL is set!" 
2018/05/19 08:32:58 grpc: addrConn.resetTransport failed to create client transport: connection error: desc = "transport: No gRPC URL is set!"; Reconnecting to { <nil>}
time="May 19 08:33:00" level=error msg="No gRPC URL is set!" 
2018/05/19 08:33:00 grpc: addrConn.resetTransport failed to create client transport: connection error: desc = "transport: No gRPC URL is set!"; Reconnecting to { <nil>}
time="May 19 08:33:03" level=error msg="No gRPC URL is set!" 
2018/05/19 08:33:03 grpc: addrConn.resetTransport failed to create client transport: connection error: desc = "transport: No gRPC URL is set!"; Reconnecting to { <nil>}
```
※エラーメッセージは無視して良い。
※※"/apps"はAPIのjsonファイルの格納用です。
####動作確認
```terminal
$ curl -L http://localhost:8080//tyk-api-test/get

{
    "error": "Authorization field missing"
}
```
###2. Tyk Dashboardの起動
####MongoDBコンテナの取得
```terminal
$ docker pull mongo

Using default tag: latest
latest: Pulling from library/mongo
4d0d76e05f3c: Already exists 
2da2ecd7fdbd: Pull complete 
c3a86da34d0f: Pull complete 
e2b1f447e420: Pull complete 
c9e820834b36: Pull complete 
ffa34fa64bf4: Pull complete 
63127ea58ee0: Pull complete 
ccb46836c598: Pull complete 
7b0abf374ec4: Pull complete 
0e8b13c8fd38: Pull complete 
Digest: sha256:c6d2b2f8c054210db26b492bab81ffab171ee54eb58925fa98fabb4faca3a9cb
Status: Downloaded newer image for mongo:latest
```
####MongoDBコンテナの起動
```terminal
$ docker run --name tyk_mongo -d mongo
```
####hostsファイルの編集
```terminal
$ vi /etc/hosts
```
####下記をhostsファイルにコピペ
```terminal
127.0.0.1 dashboard.tyk.docker
```
####Tyk Dashboardの起動
```terminal
$ docker run -d --name tyk_dashboard -p 3000:3000 --link tyk_redis:redis --link tyk_mongo:mongo --link tyk_gateway:tyk_gateway tykio/tyk-dashboard

Unable to find image 'tykio/tyk-dashboard:latest' locally
latest: Pulling from tykio/tyk-dashboard
b0568b191983: Already exists 
2ae2471d369e: Pull complete 
63c9c80af39f: Pull complete 
f65dd2df80ff: Pull complete 
Digest: sha256:880f5f7dc4dd74bfbbd5699bdd3c16c051bdceb2e84c02f1eb6a7cf12bed74dd
Status: Downloaded newer image for tykio/tyk-dashboard:latest
docker: Error response from daemon: could not get container for tyk_gateway: No such container: tyk_gateway.
See 'docker run --help'. ```
```
####起動確認。ブラウザからダッシュボードにアクセス
```terminal
$ open http://dashboard.tyk.docker:3000/ 
```
<img width="1440" alt="スクリーンショット 2018-05-19 18.01.09.png" src="https://qiita-image-store.s3.amazonaws.com/0/258400/833dc9ce-c905-082c-0e21-f3b1abbc4b29.png">

####tyk-dashbord の git を取得
```terminal
$ git clone https://github.com/TykTechnologies/tyk-dashboard-docker.git

Cloning into 'tyk-dashboard-docker'...
remote: Counting objects: 211, done.
remote: Total 211 (delta 0), reused 0 (delta 0), pack-reused 211
Receiving objects: 100% (211/211), 30.05 KiB | 119.00 KiB/s, done.
Resolving deltas: 100% (118/118), done.
```

####bootstrap の実行
```terminal
$ cd tyk-dashboard-docker/
$ ./bootstrap.sh dashboard.tyk.docker

Creating Organisation
ORGID: 5b01186bcd4a35000169be3c
Adding new user
USER AUTH: c2c7fe9ff4024be14536afce7c976d3c
NEW ID: 5b01186b1f6c8556b28b47d8
Setting password

DONE
====
Login at http://dashboard.tyk.docker:3000/
User: bfkzo935wx@default.com
Pass: test123
```
注意：python 3.0 以降では動作しません。
bootstrap.sh 内の "RANDOM_USER", "PASS" 変数でユーザ名、パスワードを指定できます。

###3. Tyk Pump containerの起動
####Tyk Pumpコンテナの取得

```terminal
$ docker pull tykio/tyk-pump-docker-pub

Using default tag: latest
latest: Pulling from tykio/tyk-pump-docker-pub
b0568b191983: Already exists 
99f635d1dd79: Pull complete 
93b85fd833fa: Pull complete 
50ba1616afbf: Pull complete 
Digest: sha256:95e6e13d6289fd9c16e49d2d3f686162f554776aa7f1675ca51e2252b940364b
Status: Downloaded newer image for tykio/tyk-pump-docker-pub:latest
```
####Tyk Pumpの起動
```terminal
$ docker run -d --name tyk_pump --link tyk_redis:redis --link tyk_mongo:mongo tykio/tyk-pump-docker-pub
```