Skip to content

Instantly share code, notes, and snippets.

@udzura
Last active August 12, 2018 16:26
Show Gist options
  • Save udzura/08f6198e51af3d75f7224612f48c3f16 to your computer and use it in GitHub Desktop.
Save udzura/08f6198e51af3d75f7224612f48c3f16 to your computer and use it in GitHub Desktop.
ケーネイティブと私

  • @udzura Uchio Kondo ペパボ
  • コンテナで人生が狂った
  • 本業はスプラトゥーン2
  • Haconiwaというコンテナランタイムを自作していたり、それでウェブサービスをやっている
  • 最近はCRIUいじってます https://criu.org/Main_Page
  • 今日は前佛さんのご結婚をお祝いに来ました

Knative っち?

  • Kubernetesの上でサーバレスなやつを作るツールキット・規約集(と言う解釈をしている)
  • プログラミング言語でいうSDKに当たりそう
  • 3 種類のやつがある

Knative Serving

いわゆるWebサービスのアプリコンテナなどをいい感じにサーブする

Knative Events

Cloud Eventsに沿ってやっていってる、ServingがPaaSならこっちはFaaSのためのツールと仕様

Knative Build

コンテナなどのビルドを司る

buildpack を試したぞ

手順

demoをやっていきます

ビルドテンプレートを入れる

kubectl apply -f https://raw.githubusercontent.com/knative/build-templates/master/buildpack/buildpack.yaml

今回はGCP上ではなく、ローカルで試すのでexportのための手順が必要

  • docker-auth.yaml
apiVersion: v1
kind: Secret
metadata:
  name: basic-user-pass-dockerio
  annotations:
    build.knative.dev/docker-0: https://index.docker.io
type: kubernetes.io/basic-auth
data:
  username: dWR6dXJh
  password: $(echo -n your-pass | base64)
  • acl.yml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: cloudnativeman
secrets:
- name: basic-user-pass-dockerio
  • それぞれを kubeclt apply -f していく
  • build-rails.yml
apiVersion: build.knative.dev/v1alpha1
kind: Build
metadata:
  name: buildpack-build-example
spec:
  serviceAccountName: cloudnativeman
  source:
    git:
      url: https://github.com/udzura/rg-kitakyu-1stapp
      revision: master
  template:
    name: buildpack
    arguments:
    - name: IMAGE
      value: udzura/knative-buildappp-rails-example
  • apply -f するとビルドが走っているようだ...
$ kubectl get pods                                      
NAME                                                        READY     STATUS      RESTARTS   AGE
buildpack-build-example-56j72                               0/1       Completed   0          5h

$ kubectl describe pod buildpack-build-example-56j72
Name:           buildpack-build-....                                                                                                                                 [104/231743]
Namespace:      default             
Node:           minikube/192.168.64.3
Start Time:     Thu, 26 Jul 2018 14:10:20 +0900                                         
Labels:         build-name=buildpack-build-x                                                      
Annotations:    sidecar.istio.io/inject=false                                           
Status:         Succeeded                                                                                           
IP:             172.17.0.28                                                                 
Controlled By:  Build/buildpack-build-x                                                                              
Init Containers:                                                                                                                                 
  build-step-credential-initializer:                                                                                                                               
    Container ID:  docker://9e578bb7cf3ca48d5e8af3b949906a3187ba18b10d3223ff30d367d53220c669
    Image:         gcr.io/build-crd/github.com/knative/build/cmd/creds-init@sha256:cfcc14889abe29b54d17ff6cf414918d9d92ff02bb112525742ec2e30117899f
    Image ID:      docker-pullable://gcr.io/build-crd/github.com/knative/build/cmd/creds-init@sha256:cfcc14889abe29b54d17ff6cf414918d9d92ff02bb112525742ec2e30117899f
    Port:          <none>                                                 
    Host Port:     <none>                        
    Args:                                          
      -basic-docker=basic-user-pass-dockerio=https://index.docker.io
    State:          Terminated                     
      Reason:       Completed                      
      Exit Code:    0        
      Started:      Thu, 26 Jul 2018 14:10:21 +0900     
      Finished:     Thu, 26 Jul 2018 14:10:21 +0900                                     
    Ready:          True          
    Restart Count:  0      
    Environment:                                                                        
      HOME:  /builder/home          
    Mounts:                 
      /builder/home from home (rw)                                                      
      /var/build-secrets/basic-user-pass-dockerio from secret-volume-basic-user-pass-dockerio (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from cloudnativeman-token-knccg (ro)
      /workspace from workspace (rw)
  build-step-git-source:
    Container ID:  docker://6049f0c1438e49c58eb32fe68ca237288449a5e2fe92ce2e3c4d203005123b19
    Image:         gcr.io/build-crd/github.com/knative/build/cmd/git-init@sha256:d52c29a4a1d83712b048bd32dbb5f97b7d2791af9b892f68fe94e162932ac66e
    Image ID:      docker-pullable://gcr.io/build-crd/github.com/knative/build/cmd/git-init@sha256:d52c29a4a1d83712b048bd32dbb5f97b7d2791af9b892f68fe94e162932ac66e
    Port:          <none>
    Host Port:     <none>
....

$ kubectl logs buildpack-build-... -c build-step-build
-----> Ruby Buildpack version 1.7.21     
-----> Supplying Ruby                    
-----> Installing bundler 1.16.2                      
       Download [https://buildpacks.cloudfoundry.org/dependencies/bundler/bundler-1.16.2.tgz]
       **WARNING** You have not declared a Ruby version in your Gemfile.
       Defaulting to 2.4.4                                             
       See http://docs.cloudfoundry.org/buildpacks/ruby/index.html#runtime for more information.
-----> Installing ruby 2.4.4                                                        
       Download [https://buildpacks.cloudfoundry.org/dependencies/ruby/ruby-2.4.4-linux-x64-8e992e9b.tgz]
-----> Update rubygems from 2.6.14.1 to 2.7.7
-----> Installing rubygems 2.7.7                                            
       Download [https://buildpacks.cloudfoundry.org/dependencies/rubygems/rubygems-2.7.7-4cb2c9a3.tgz]
-----> Installing node 4.9.1                                                          
       Download [https://buildpacks.cloudfoundry.org/dependencies/node/node-4.9.1-linux-x64-2eccbf91.tgz]
       **WARNING** node 4.x will no longer be available in new buildpacks released after 2018-04-01.
       See: https://github.com/nodejs/LTS                                             
-----> Installing dependencies using bundler 1.16.2          
       Running: bundle install --without development:test --jobs=4 --retry=4 --path /tmp/contents487331078/deps/0/vendor_bundle --binstubs /tmp/contents487331078/deps/0/binstubs --
deployment                                               
       Fetching gem metadata from https://rubygems.org/.......
       Fetching rake 12.3.1         
       Fetching concurrent-ruby 1.0.5  
       Fetching minitest 5.11.3                        
       Installing minitest 5.11.3  
       Installing rake 12.3.1                          
       Installing concurrent-ruby 1.0.5  
       Fetching thread_safe 0.3.6        
       Fetching builder 3.2.3
       ....
## いつものアレじゃん!(感動)

$ kubectl logs buildpack-build-... -c build-step-export
2018/07/26 05:14:56 mounted blob: sha256:c2037d918bb3d124dc66561661585a8e6f15e90002b7b9285878694335a67aba
2018/07/26 05:14:56 mounted blob: sha256:d732cb0e8d66da8ec34f3ddf8194c96915165840df08b3846873db6c0b880dc4
2018/07/26 05:14:56 mounted blob: sha256:c35d6ba7a2be4a718bab4c4ca4f12bfccb794629454951a7db6403ca9d294f95
2018/07/26 05:14:57 mounted blob: sha256:d3874070f3046336e70e578cdfc09b6d9c5c21c89dfaf4a5b9e885014aec3bfc
2018/07/26 05:14:59 pushed blob sha256:66cc677f4c051e3148066ccee6c273ed6ec899b1c5a9a5b61c414385e2ad9568
2018/07/26 05:16:14 pushed blob sha256:14b3ff9f3fb082b7b890db32a85acbf114bad2d15829a567b17b388e7632b300
2018/07/26 05:16:14 index.docker.io/udzura/knative-buildappp-rails:latest: digest: sha256:4208c94effd233842e7ea928667999ad177bc60715a689c2230e39942c5bcd93 size: 1083
  • イメージができたらそのままServeできる、すごい
  • service-rails.yml
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: buildapp-rails-hello
  namespace: default
spec:
  runLatest:
    configuration:
      revisionTemplate:
        spec:
          container:
            image: docker.io/udzura/knative-buildappp-rails
            env:
            - name: DATABASE_URL
              value: "sqlite3:///tmp/prod.sqlite3"
  • アクセスする
$ curl -svH "Host: buildapp-rails-hello.default.example.com" http://$(minikube ip):$(kubectl get svc knative-ingressgateway -n istio-system -o 'jsonpath={.spec.ports[?(@.port==80)].nodePort}') | grep -i yay | grep -v data
      <h1>Yay! You&rsquo;re on Rails!</h1>
  • ブラウザで見たい。Railsのいつものアレ
  • minikube ipのIPに buildapp-rails-hello.default.example.com をhostsして、 kubectl get svc knative-ingressgateway -n istio-system で見えるポートをくっつけてアクセス
  • knative-ingressgateway が Host: http://udzura.jp:32380 と Host: http://udzura.jp を厳密に区別するから、minikube上かつブラウザで動作確認するために謎のプロクシを書いて頑張った(サーバフルな要素)
require 'webrick'
require 'webrick/httpproxy'

before_req = proc{|req, res|
  h = req.instance_variable_get(:@header)
  h['host'] = h['host'].map{|v| v.sub(/:\d+$/, '') }
  req.instance_variable_set(:@header, h)
  p req['Host']
}
config = { :BindAddress => '127.0.0.1', :Port => 8080, :ProxyAuthProc => before_req}

s = WEBrick::HTTPProxyServer.new( config )
Signal.trap('INT') do
  s.shutdown
end
s.start

これで

https://pbs.twimg.com/media/DjApcVUVsAE_Uce.png

所感

  • Knative めっちゃおもろやん
  • Kubernetes がやっていきたい世界の一端が見えた。
    • いわゆるプログラミング言語のライブラリ、パッケージはコンテナになっていく
    • プログラミング言語のレベルでしか試せなかったものが、アーキテクチャ出れるで色々試せる世界が来る
  • パブリッククラウドにガッツリ依存した「ピタゴラ装置」的な楽しさがOSSにやって来た感じ。それぞれのコンポーネントは自作できるし
  • 触って損はないでしょう

より詳しい手順

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment