Skip to content

Instantly share code, notes, and snippets.

@ronakbanka
Forked from matiasinsaurralde/notes.md
Created January 24, 2018 08:30
Show Gist options
  • Save ronakbanka/e2927981db34a1d724fd996f1125a7dd to your computer and use it in GitHub Desktop.
Save ronakbanka/e2927981db34a1d724fd996f1125a7dd to your computer and use it in GitHub Desktop.
Tyk + Cloud Foundry + CF Router

Initial setup

Note: Target the right organization.

cf create-space tyk-deployment
cf target -o 'matias-org' -s 'tyk-deployment'

Sample application

cd ~
git clone https://github.com/matiasinsaurralde/tyk-sample-app
cd tyk-sample-app
cf push

Tyk Gateway

This build is a custom one and includes a patch that makes Tyk compatible with the CF router:

cd ~
git clone https://github.com/matiasinsaurralde/tyk-buildpack
cd tyk-buildpack
cf push

We expose the gateway as a service called tyk-proxy:

cf create-user-provided-service tyk-proxy -r https://tyk-gateway.cfapps.io

Redis

We create and link the Redis Cloud service:

cf create-service rediscloud 30mb tyk-redis
cf bind-service tyk-gateway tyk-redis

And restage the application, after this the gateway will be connected to Redis:

cf restage tyk-gateway

Routes

We create two routes for our tyk-sample-app:tyk-sample-app.cfapps.io and tyk-sample-app-internal.cfapps.io:

cf map-route tyk-sample-app cfapps.io --hostname tyk-sample-app
cf map-route tyk-sample-app cfapps.io --hostname tyk-sample-app-internal

All the requests that target tyk-sample-app.cfapps.io will be proxied by Tyk, on the other side, requests targeting tyk-sample-app-internal.cfapps.io will be received directly by the sample application.

The Tyk buildpack is configured with a test API definition that looks like this:

    "proxy": {
        "listen_path": "/quickstart/",
        "target_url": "http://tyk-sample-app-internal.cfapps.io",
        "strip_listen_path": true
    },
    "domain": "tyk-sample-app.cfapps.io"

When a request arrives to http://tyk-sample-app.cfapps.io/quickstart/, it will be proxied to Tyk, and then to http://tyk-sample-app-internal.cfapps.io/, you may also try extra/random paths. The sample app will reply to any path, and display information about the request.

You may find the sample API definition here.

Finally we bind the tyk-proxy service to our tyk-sample-app, this is the most important step:

cf bind-route-service cfapps.io tyk-proxy --hostname tyk-sample-app

Additional information

Test request/response:

$ curl https://tyk-sample-app.cfapps.io/quickstart/

{
	"target_url": "/",
	"target_host": "tyk-sample-app-internal.cfapps.io",
	"request_headers": {
		"Accept": "*/*",
		"Accept-Encoding": "gzip",
		"Connection": "close",
		"User-Agent": "curl/7.43.0",
		"X-B3-Parentspanid": "1f474d45bf514672",
		"X-B3-Spanid": "3dce55945e2b72ab",
		"X-B3-Traceid": "9caebd13b78c971f",
		"X-Cf-Applicationid": "1dd6df2e-1c4d-4c66-bb36-52612fe61668",
		"X-Cf-Forwarded-Url": "https://tyk-sample-app.cfapps.io/quickstart/",
		"X-Cf-Instanceid": "221bf40d-5b66-42e7-7b73-5832c2f2918f",
		"X-Cf-Proxy-Metadata": "eyJub25jZSI6InZEV3A0L2pzd1pJV2svT3QifQ==",
		"X-Cf-Proxy-Signature": "NKPVsS_vIChER_OE-On5KHm8P_IMQeqMS02yJAg4jr8vh5w98vLx9-zdLv7TsSZYvYttV30fcCa4r0lQq2w1bQ6ZWBh4coPtrrqh4Yk7GyYQrEg6w0_gbX2MB4sd5tkdG5uygnXJjyqaZVgq1boSVpl1Wk5dO283p7TzdJBK5BqG3Q==",
		"X-Forwarded-For": "77.110.230.158, 10.10.2.141, 52.7.25.136, 10.10.2.141, 10.10.81.9, 54.210.138.89, 10.10.2.141",
		"X-Forwarded-Port": "80",
		"X-Forwarded-Proto": "http",
		"X-Request-Start": "1488387016516",
		"X-Vcap-Request-Id": "fbb98a8b-ccad-4fd4-4da0-264c354aacbb"
	}
}

Routes

$ cf routes
Getting routes for org matias-org / space tyk-deployment as matias@insaurral.de ...

space            host                      domain      port   path   type   apps             service
tyk-deployment   tyk-gateway               cfapps.io                        tyk-gateway
tyk-deployment   tyk-sample-app            cfapps.io                        tyk-sample-app   tyk-proxy
tyk-deployment   tyk-sample-app-internal   cfapps.io                        tyk-sample-app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment