Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

10 分で書ける Cloud Foundry Route Service

912 views

Published on

第 33 回 PaaS 勉強会の発表資料です。

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

10 分で書ける Cloud Foundry Route Service

  1. 1. 10 分で書ける Cloud Foundry Route Service @第33回PaaS勉強会 2016/08/02 Hiroaki Ukaji
  2. 2. 今回のテーマ Cloud Foundry Route Service
  3. 3. Route Service って何? ※知ってる!って方は聞き流して大丈夫です
  4. 4. Route Service @ 公式 Introduction  Cloud Foundry applicaAon developers may wish to apply transformaAon or processing to requests before they reach an applicaAon. ex.) authenAcaAon / rate limiAng / caching services App への Request 到達前に何か処理加えたくない?
  5. 5. Route Service って何 Cloud Foundry Route Service
  6. 6. Route Service って何 Cloud Foundry Route Service PaaS ∈
  7. 7. Route Service って何 Cloud Foundry Route Service PaaS   ∈ ≒ App のソースを投げ込むと 上手いこと動かしてくれる君
  8. 8. Route Service って何 Cloud Foundry Route Service ≒ (Cloud Foundry において)  App にくっついて      お仕事してくれる君
  9. 9. Route Service って何 Cloud Foundry Route Service ≒ (Cloud Foundry において)  App にくっついて      お仕事してくれる君 App MySQL Service RMQ Service applicaAon service
  10. 10. Route Service って何 Cloud Foundry Route Service ≒ (Cloud Foundry において)  App にくっついて      お仕事してくれる君 App MySQL Service RMQ Service bind applicaAon service
  11. 11. Route Service って何 Cloud Foundry Route Service ≒ (Cloud Foundry において)  App にくっついて      お仕事してくれる君
  12. 12. Route Service って何 Cloud Foundry Route Service ≒ Cloud Foundry で     ApplicaAon と一緒になって         Route 関係の仕事をしてくれる君
  13. 13. アプリへの Access Route Client Load Balancer CF Router App  CF アプリを使う人 ロードバランサ あってもなくても CF アプリ CF の中で アクセス割り振る人 ( ) ① ② ③
  14. 14. Cloud Foundry Route Service Client Load Balancer CF Router App  CF Route Service ① ② ③ ④ ⑤ ⑥
  15. 15. Cloud Foundry Route Service Client Load Balancer CF Router App  CF Route Service ① ② ③ ④ ⑤ ⑥ アプリへの Route 到達前に一旦自分を経由させる
  16. 16. Cloud Foundry Route Service Client Load Balancer CF Router App  CF Route Service ① ② ③ ④ ⑤ ⑥ 基本的に全て HTTP なので・・・
  17. 17. Cloud Foundry Route Service Client Load Balancer CF Router App  CF Route Service ① ② ③ ④ ⑤ ⑥ Route Service の正体 =  HTTP サーバ
  18. 18. デモ
  19. 19. hiroaki@HMP:~/demo$ tree . !"" hello #   !"" Staticfile #   !"" index.html #   %"" manifest.yml %"" s-rs !"" Godeps #   !"" Godeps.json #   !"" Readme #   %"" _workspace !"" Procfile !"" README.md !"" main.go %"" manifest.yml 4 directories, 9 files Client Load Balancer CF Router App  CF Route Service hello/ 配下はこの後 Backend の App に、 s-rs/ 配下は Route-Service になる予定です。 ソースコードは下記 Repository でも見ることができます h[ps://github.com/hiroakiukaji/simple-route-service-handson
  20. 20. hiroaki@HMP:~/demo$ cd hello/ hiroaki@HMP:~/demo/hello$ ls Staticfile index.html manifest.yml hiroaki@HMP:~/demo/hello$ cf push Using manifest file /Users/hiroaki/demo/hello/manifest.yml Creating app staticapp in org ukaji_org / space ukaji_space as ukaji... OK : : : : : App started OK App staticapp was started using this command `sh boot.sh` Showing health and status for app staticapp in org ukaji_org / space ukaji_space as ukaji... OK requested state: started instances: 1/1 usage: 256M x 1 instances urls: staticapp.mcf.nttlabs.info last uploaded: Mon Aug 8 02:36:03 UTC 2016 stack: unknown buildpack: staticfile 1.3.9 state since cpu memory disk details #0 running 2016-08-08 11:36:17 AM 0.0% 0 of 256M 0 of 1G hiroaki@HMP:~/demo/hello$ curl staticapp.mcf.nttlabs.info (´・_・`) Client Load Balancer CF Router App  CF まずは Backend のアプリをデプロイ
  21. 21. hiroaki@HMP:~/demo/hello$ cd ../s-rs/ hiroaki@HMP:~/demo/s-rs$ ls Godeps Procfile README.md main.go manifest.yml hiroaki@HMP:~/demo/s-rs$ cf push : : : : : requested state: started instances: 1/1 usage: 256M x 1 instances urls: simple-rs.mcf.nttlabs.info last uploaded: Mon Aug 8 04:19:13 UTC 2016 stack: unknown buildpack: go_buildpack state since cpu memory disk details #0 running 2016-08-08 01:19:32 PM 0.0% 0 of 256M 0 of 1G hiroaki@HMP:~/demo/s-rs$ cf create-user-provided-service simplerouteservice -r https://simple-rs.mcf.nttlabs.info Creating user provided service simplerouteservice in org ukaji_org / space ukaji_space as ukaji... OK hiroaki@HMP:~/demo/s-rs$ cf services Getting services in org ukaji_org / space ukaji_space as ukaji... OK name service plan bound apps last operation simplerouteservice user-provided Route Service として使う HTTP サーバをデプロイ。(※) その後 HTTP サーバを Service として認識させる。 ※ HTTPS で到達できる場所ならばどこでも構いませんが、 今回はたまたま Cloud Foundry 上に同居させています。 Client Load Balancer CF Router App  CF Route Service  
  22. 22. hiroaki@HMP:~/demo/s-rs$ cf bind-route-service mcf.nttlabs.info simplerouteservice -n staticapp Binding route staticapp.mcf.nttlabs.info to service instance simplerouteservice in org ukaji_org / space ukaji_space as ukaji... OK hiroaki@HMP:~/demo/s-rs$ curl staticapp.mcf.nttlabs.info (´・_・`) Route Service としてバインドすれば完成。 Client Load Balancer CF Router App  CF Route Service   hiroaki@HMP:~$ cf logs simple-rs Connected, tailing logs for app simple-rs in org ukaji_org / space ukaji_space as ukaji... 2016-08-08T13:36:58.24+0900 [RTR/0] OUT simple-rs.mcf.nttlabs.info - [08/08/2016:04:36:58.238 +0000] "GET / HTTP/1.0" 200 0 13 "-" "curl/7.43.0" 192.168.12.34:35667 x_forwarded_for:"192.168.10.40, 192.168.12.34, 192.168.13.51" x_forwarded_proto:"https" vcap_request_id:ab5cfd36-1548-4f95-7fb2-8c9fc7dc3262 response_time:0.008521804 app_id:2b11f3ec-d733-48e2- b141-5ac98020eee3 何も処理をしていないので振る舞いは変わりませんが、 Route Service のアプリのログを見てみると何らかの HTTP アクセスが飛んでいることが分かるはず。
  23. 23. Route Service の実装
  24. 24. Route Service by Golang の実装 最小構成で 60 行ぐらい package main import ( : ) type SimpleRoundTripper struct { transport http.RoundTripper } func newSimpleRoundTripper() *SimpleRoundTripper { transport := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: skipSslValidation()}, } return &SimpleRoundTripper{ transport: transport, } } func (s *SimpleRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) { var response *http.Response var err error response, err = s.transport.RoundTrip(request) if err != nil { return nil, err } return response, err } func main() { http.Handle("/", newProxy()) log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), nil)) } func newProxy() http.Handler { proxy := &httputil.ReverseProxy{ Director: func(r *http.Request) { url, err := url.Parse(r.Header.Get("X-Cf-Forwarded-Url")) if err != nil { log.Fatalln(err.Error()) } r.URL = url r.Host = url.Host }, Transport: newSimpleRoundTripper(), } return proxy } func skipSslValidation() bool { var skipSslValidation bool var err error if skipSslValidation, err = strconv.ParseBool(os.Getenv("SKIP_SSL_VALIDATION")); err != nil { skipSslValidation = true } return skipSslValidation }
  25. 25. Route Service by Golang の実装 Entrypoint func main() { http.Handle("/", newProxy()) log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), nil)) } GET / を待ち受けるただの HTTP サーバ
  26. 26. Route Service by Golang の実装 Route Service の本体を担うメソッド func (s *SimpleRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) { var response *http.Response var err error response, err = s.transport.RoundTrip(request) if err != nil { return nil, err } return response, err } あからさまに Request を受けて Response を返している人がいます ※この例は本当に素通しで何もしていません
  27. 27. Route Service by Golang の実装 Route Service の本体を担うメソッドを書いてみる func (s *SimpleRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) { var response *http.Response var err error response, err = s.transport.RoundTrip(request) if err != nil { return nil, err } return response, err } 適当な処理を書いて Response を投げ返してみましょう こ の へ ん
  28. 28. デモ
  29. 29. hiroaki@HMP:~/demo/s-rs$ ls Godeps Procfile README.md main.go manifest.yml hiroaki@HMP:~/demo/s-rs$ vi main.go : func (s *SimpleRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) { var response *http.Response var err error str := "_人人人人人_n> (´・_・`) <n ̄Y^Y^Y^Y^Y ̄n" dummyResponse := &http.Response{ StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString(str)), } return dummyResponse, err response, err = s.transport.RoundTrip(request) if err != nil { return nil, err } return response, err } : ダミーの HTTP レスポンスを生成して 返してしまうだけのコードを追加
  30. 30. hiroaki@HMP:~/demo/s-rs$ cf push : : : : : requested state: started instances: 1/1 usage: 256M x 1 instances urls: simple-rs.mcf.nttlabs.info last uploaded: Mon Aug 8 04:54:37 UTC 2016 stack: unknown buildpack: go_buildpack state since cpu memory disk details #0 running 2016-08-08 01:55:10 PM 0.0% 3.2M of 256M 7.7M of 1G hiroaki@HMP:~/demo/s-rs$ curl staticapp.mcf.nttlabs.info _人人人人人_ > (´・_・`) <  ̄Y^Y^Y^Y^Y ̄ コードを書き換えたので再デプロイ。 HTTP サーバの中身が入れ替わるだけなので、 Service 登録のやり直し等は特に必要ありません。 Client Load Balancer CF Router App  CF Route Service   hiroaki@HMP:~/demo/s-rs$ cf unbind-route-service mcf.nttlabs.info simplerouteservice -n staticapp -f Unbinding route staticapp.mcf.nttlabs.info from service instance simplerouteservice in org ukaji_org / space ukaji_space as ukaji... OK hiroaki@HMP:~/demo/s-rs$ curl staticapp.mcf.nttlabs.info (´・_・`) hiroaki@HMP:~/demo/s-rs$ cf bind-route-service mcf.nttlabs.info simplerouteservice -n staticapp Binding route staticapp.mcf.nttlabs.info to service instance simplerouteservice in org ukaji_org / space ukaji_space as ukaji... OK hiroaki@HMP:~/demo/s-rs$ curl staticapp.mcf.nttlabs.info _人人人人人_ > (´・_・`) <  ̄Y^Y^Y^Y^Y ̄ 今度は Route Service の bind / unbind 毎に HTTP レスポンスの差を視認できるはず。
  31. 31. Client Load Balancer CF Router App  CF Route Service ① ② ③ ④ ⑤ ⑥ Q. 何か余計なアクセスぐるぐるしてるけど 性能的にどうなんですか?
  32. 32. A. 落ちます。 こ の へ ん Route Service 無し 素通し Route Service 1000REQ / 1par 124.11 34.04 1000REQ / 5par 512.31 118.04 1000REQ / 10par 793.65 174.64 1000REQ / 20par 1377.21 207.25 VS Route Service 無し 素通し Route Service ただし・・・
  33. 33. A. 落ちます。ただし、 今回の実験では Route Service も Cloud Foundry 上に同居
  34. 34. A. 落ちます。ただし、 諸事情により採用したアーキテクチャが network hop 嵩む系 Fully Brokered StaAc Brokered User Provided 今回はこれ 使う側が超楽 ◯ × 経路増加大 経路に無駄がない ◯ × インフラ構成要変更 お手軽 ◯ × 経路増加大 公式 Route Service Architecture 3 パターン
  35. 35. まとめ •  Cloud Foundry Route Service  = CF アプリへのリクエストに処理を加える Service –  実体はほぼただの HTTP サーバ –  ソースコードは平易なので一読がおすすめ –  性能は当然落ちるので導入要否は各自検証
  36. 36. 参考 Route Services | Cloud Foundry Docs h[ps://github.com/cloudfoundry-samples/logging-route-service h[ps://github.com/cloudfoundry-samples/ratelimit-service h[ps://docs.cloudfoundry.org/services/route-services.html Github: cloudfoundry-samples/ratelimit-service Github: cloudfoundry-samples/logging-route-service h[p://www.slideshare.net/gwennetourneau/cloud-foundry-meetup-tokyo-1-route-service Cloud Foundry Meetup Tokyo #1 Route Service

×