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

2cf373725ded741824c50fd571eda6e1?s=128

KONDO Uchio

April 19, 2019
Tweet

Transcript

  1. 2.

    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. 3.

    Living and working in Fukuoka 3 ☕ We’re here! GMO

    Pepabo Office Yakuin, Fukuoka’s Best ☕ Town
  3. 9.

    I’ve sent a patch to CRIU • And I have

    read many parts of CRIU’s code (almost all in C language) 9
  4. 12.
  5. 13.

    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
  6. 14.

    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
  7. 16.

    Migration example • cf. KVM’s live migration 16 Dump! Copy

    Restore! Reboot Host A Container Image Same Container Another Host
  8. 17.

    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
  9. 18.

    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
  10. 22.

    Environments • We have a application platform using containers: 22

    Application Containers Proxy Routing by vhost Requests Deployment by users
  11. 23.

    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
  12. 24.

    Can we reduce restart time? • CRIU looked like a

    very good choice for this system. 24
  13. 25.

    Preliminary experiment • Created instances on vagrant as below: •

    Checking response time, this includes restart time 25
  14. 26.

    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
  15. 27.

    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
  16. 29.

    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
  17. 33.
  18. 35.

    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
  19. 38.

    But I guess you are thinking: • “Our application is

    deployed to VMs, NOT containers” 38
  20. 39.

    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
  21. 44.

    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
  22. 45.

    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!
  23. 46.

    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
  24. 47.

    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
  25. 50.

    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 !!
  26. 51.

    51 Generates new root For bind-mount dest Bind-mount “/“ and

    any important directories Mount namespace is isolated, so it is invisible from host
  27. 52.

    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
  28. 54.

    Tha age of Cloud Native • Now is the time

    everything becomes containerized, orchestrated and microservice-based... 54
  29. 55.

    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
  30. 56.

    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
  31. 58.
  32. 60.

    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