Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The fastest way to bootstrap Ruby on Rails

The fastest way to bootstrap Ruby on Rails

@ RubyKaigi 2019 ✨💎✨

Japanese Script: https://gist.github.com/udzura/9d21c4eaecf1c118268ea48c7974b62a

KONDO Uchio

April 19, 2019
Tweet

More Decks by KONDO Uchio

Other Decks in Technology

Transcript

  1. Engineer @ GMO Pepabo, Inc. Uchio Kondo / @udzura https://blog.udzura.jp/

    * Local Organizer of * Chair of CloudNative Days Fukuoka 2019 * Author of Japanese Ruby Books * Founder of Haconiwa (mruby on container) * 10 y.o. Rubyist * Container Enthusiast * System Programming Novice
  2. Living and working in Fukuoka 3 ☕ We’re here! GMO

    Pepabo Office Yakuin, Fukuoka’s Best ☕ Town
  3. I’ve sent a patch to CRIU • And I have

    read many parts of CRIU’s code (almost all in C language) 9
  4. CRIU • Using CRIU, We can: • Dump processes information

    info “image” files • Restore processes from dumped image files • CRIU stands for “Checkpoint and Restore In Userspace” • CRIU is a tool for processes, but it can be applied to containers C/R. 13
  5. CRIU Internal: • CRIU uses /proc filesystems, cgroup attributes and

    stats, TCP checkpoint feature after Kernel 3.5, and many syscalls to get process info 14
  6. Migration example • cf. KVM’s live migration 16 Dump! Copy

    Restore! Reboot Host A Container Image Same Container Another Host
  7. Faster bootstrap example • When you have a heavier app

    in bootstrap, like Jenkins, Redmine, or legacy monolithic Rails app, you will hesitate to reboot these. • e.g. Jenkins requires about 5 sec to boot
 (using docker with downloaded image). 17
  8. Ahead-of-time process dump • CRIU can skip initialization process. •

    e.g. read() scripts and code evaluation. • Using CRIU program will be loaded directory from “memory dump” 18
  9. Environments • We have a application platform using containers: 22

    Application Containers Proxy Routing by vhost Requests Deployment by users
  10. Automated host migration • When one host is crashed, the

    new container is invoked automatically in another host. But this requires RESTART of a container... 23 Crash! Proxy Requests Restart and Redirect Accesses
  11. Can we reduce restart time? • CRIU looked like a

    very good choice for this system. 24
  12. Preliminary experiment • Created instances on vagrant as below: •

    Checking response time, this includes restart time 25
  13. How to measure boot time 26 ngx_mruby Haconiwa Containers On

    the next request, restart an application container and forward access to this container in one transaction Force to stop by operation Haconiwa
  14. The response time is composed of 27 Normal Container Response

    First Time Response with Boot Middleware work Middleware work App response App response Container bootstrap process
  15. Experiment on staging • Deployed simple Welcome Rails application to

    staging • Ran apache bench and plot response times • Invoke relocation between hosts, then check reboot time. 29
  16. FYI: Scale out using CRIU • We can restore and

    spawn as many containers as you like from one CRIU's container image • This means CRIU can reduce costs of containers scale-out 35 Proxy Host
  17. But I guess you are thinking: • “Our application is

    deployed to VMs, NOT containers” 38
  18. Migration VM->container is hard • Many aspects of system characteristics

    are varying between VMs and containers • VMs has an init process • daemons such as sshd and rsyslogd • a full-stack filesystem to manage whole operation system • ... all of which containers are usually omitting 39
  19. Warning: • Following talk is about Linux container runtimes implementation

    • Requires some of Linux kernel's container feature • But I’ll show you (m)ruby codes, which I guess you can read :) 44
  20. What grenadine daemon do indeed • grenadine daemon creates a

    minimalistic container internally. 45 Grenadine daemon Application Minimalistic container described below App App wait() We’re here!
  21. How to write a container in Ruby pid = Process.fork

    do
 isolate_namespace_using_unshare(flag) assign_to_cgroup(name) if enabled chroot_or_pivot_root(to: rootfs) # Doing other options exec(“/path/to/command”, “--opt1”, “--opt2”) end status = Process.waitpid2(pid) # will block and wait() puts status • NOTE: program should be single-threaded, so mruby is better 46
  22. Grenadine unshare-s only PID Namespace and Mount Namespace • To

    avoid PID conflict: 47 12345 12347 12351 12345 12347 12351 12345 is Already taken! <- -> 1 6 4 1 6 4 No existing PID Root namespace Using isolated namespace Restore
  23. Grenadine requires no additional root filesystem • Grenadine creates ad-hoc

    root filesystem using bind-mount 50 / /run/gren/aaaa-bbbb /tmp /run/gren/aaaa-bbbb/tmp /var/log /run/gren/aaaa-bbbb/var/log /... /run/gren/aaaa-bbbb/... Bind-mount pivot_root !!
  24. 51 Generates new root For bind-mount dest Bind-mount “/“ and

    any important directories Mount namespace is isolated, so it is invisible from host
  25. To make checkpoint easily • The processes Grenadine creates look

    like a normal, VM-based application daemon • Internally some of attributes are unshared • To allow CRIU to create checkpoints easily • Grenadine is aimed that it can be introduced into historical VM-based application environments with least changes 52
  26. Tha age of Cloud Native • Now is the time

    everything becomes containerized, orchestrated and microservice-based... 54
  27. Grenadine in the future • Grenadine can be used for

    historical VMs now • I want people to use Grenadine as a checkpoint/restore tool for Kubernetes or other container-based systems • It is cloud-native middleware!! (Like ones on CNCF incubators) 55
  28. Please try Grenadine • Current Grenadine have great chemistry with

    smaller applications • But soon it can be applied with larger apps :) • I will release rpm or deb file - as soon as possible. :( 56
  29. References • Websites: • CRIU.org https://criu.org/Main_Page • Introduction to CRIU

    https://speakerdeck.com/udzura/introduction-to- criu • Grenadine on GitHub: https://github.com/udzura/grenadine • Japanese article: https://udzura.hatenablog.jp/entry/ 2019/03/07/202615 60