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

Ruby and Docker on Rails

Muriel Salvan
February 04, 2014

Ruby and Docker on Rails

Quick overview of Docker and its usage.
Illustrated with a cluster of Rails applications and a Ruby load balancing proxy on top of them.
This presentation was made during the rivierarb meetup in Sophia-Antipolis on 2014 Feb 04th by Muriel Salvan.

Muriel Salvan

February 04, 2014
Tweet

Other Decks in Technology

Transcript

  1. Ruby and on Rails
    Muriel Salvan
    X-Aeon Solutions
    Feb 04th 2014
    Riviera.rb

    View full-size slide

  2. Muriel Salvan
    Freelance Developer
    Founder of X-Aeon Solutions
    Open Source advocate
    Ruby / Rails expert
    Co-founder of RivieraRB

    View full-size slide

  3. @MurielSalvan
    Github
    SourceForge
    My tech blog
    [email protected]

    View full-size slide

  4. Let's talk about cargo transport
    (that's why you came here, right?)

    View full-size slide

  5. "I want my piano to be delivered
    from Nice to London"

    View full-size slide

  6. "I also want my barrel of wine
    delivered from Nice to London."

    View full-size slide

  7. "Hey! Don't forget my bags of rice
    too!"

    View full-size slide

  8. "By train, plane, boat, truck, bus,
    mule, or whatever!"

    View full-size slide

  9. Do I worry about how goods interact
    (e.g. coffee beans next to spices)
    Can I transport quickly and smoothly
    (e.g. from boat to train to truck)

    View full-size slide

  10. A standard container that is loaded with virtually any
    goods, and stays sealed until it reaches final delivery.
    …in between, can be loaded and unloaded, stacked,
    transported efficiently over long distances, and
    transferred from one mode of transport to another

    View full-size slide

  11. Static website
    Web frontend
    User DB
    Queue Analytics DB
    Background workers
    API endpoint
    Development VM
    QA server
    Public Cloud
    Disaster recovery Contributor’s laptop
    Production Servers
    Production Cluster
    Customer Data Center
    Do services and apps
    interact appropriately?
    Can I migrate smoothly and
    quickly?

    View full-size slide

  12. Static website
    Web frontend
    User DB
    Queue
    Analytics DB
    Background workers
    API endpoint
    Development VM
    QA server
    Public Cloud
    Disaster recovery
    Contributor’s laptop
    Production Servers
    Production Cluster
    Customer Data Center

    View full-size slide

  13. So what does Docker bring?

    View full-size slide

  14. 1- File systems images

    Structured in layers (storing diffs only)

    Layers are reused between images
    Clever!

    View full-size slide

  15. 2- Processes run in a contained
    environment (containers)

    Using images' file systems
    (isolated like in chroot)

    In a separate process space
    (using Linux containers)

    With a specific network interface

    Can run as root
    It's chroot on steroids!

    View full-size slide

  16. 3- A public repository of images

    Pull and push images

    Build them from Github projects

    Be part of a great community!

    View full-size slide

  17. "Is it a yet-another-virtualization-
    solution?"

    View full-size slide


  18. Processes run by Docker
    share the host Kernel

    No device emulation

    No boot sequence

    Architectures and platforms
    between Host and containers
    must match

    View full-size slide

  19. "So where is the portability?"

    View full-size slide


  20. Containers can be run on any Kernel
    (>= 3.8, Linux 64b only for now)

    Whatever the Linux distro

    If it runs on the Host, it can run from
    a container

    Physical, virtual, cloud...

    View full-size slide

  21. It's blazingly fast!!!
    A few ms to load the container and run the
    command

    View full-size slide

  22. Get running clusters on a single host
    Awesome to test them!

    View full-size slide

  23. Ensure the environment your process
    runs in is the same in dev, test and
    prod
    Bring your environment along with your process.
    Sandboxes everywhere!
    You'll love that!

    View full-size slide

  24. Deployments become sooo easy
    Like "push"-and-"pull"-easy!

    View full-size slide

  25. Lightweight virtualization too

    View full-size slide

  26. "How do we create images?"

    View full-size slide

  27. Docker files
    Describe commands to be run to create an image

    View full-size slide

  28. # Nginx
    #
    # VERSION 0.0.1
    FROM ubuntu
    MAINTAINER Guillaume J. Charmes

    # make sure the package repository is up to date
    RUN echo "deb http://archive.ubuntu.com/ubuntu precise
    main universe" > /etc/apt/sources.list
    RUN apt-get update
    RUN apt-get install -y inotify-tools nginx apache2
    openssh-server

    View full-size slide

  29. docker build .
    Docker file
    Image

    View full-size slide

  30. "And how do we run a process in a
    container?"

    View full-size slide

  31. docker run
    Image
    Container
    running

    View full-size slide

  32. "I want to create a modified image"

    View full-size slide

  33. docker commit
    Image
    Container

    View full-size slide

  34. "Hey! I want to deploy my image for
    the world to see!"

    View full-size slide

  35. docker push
    Image
    Registry
    Defaults to
    http://index.docker.io

    View full-size slide

  36. "And now my friend wants to get my
    wonderful image to play with!"
    Yeah, sure... whatever

    View full-size slide

  37. docker search
    docker pull
    Image
    Registry

    View full-size slide

  38. "Wasn't that supposed to be a Ruby
    meetup?"

    View full-size slide

  39. Let's simulate a Rails cluster with a
    balancing Ruby proxy in front!

    View full-size slide

  40. Docker container
    B
    Docker container
    B
    Docker container
    B
    Docker container
    B
    Docker container
    B
    Docker container
    A
    3000
    3000 3000
    3000 3000
    5000 5001 5002 5003 5004
    3000

    View full-size slide

  41. 1. Create a Ruby image:
    murielsalvan/ruby

    From ubuntu base image

    Using a Dockerfile

    Bonus: Upload it to index.docker.io via Github

    View full-size slide

  42. # Ruby environment
    #
    # VERSION 0.2
    FROM ubuntu
    MAINTAINER Muriel Salvan
    RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
    RUN apt-get update
    RUN apt-get upgrade -y
    RUN apt-get install -y build-essential zlib1g-dev libssl-dev libreadline6-dev libyaml-dev
    ADD http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.0.tar.gz /tmp/
    RUN cd /tmp && \
    tar -xzf ruby-2.1.0.tar.gz && \
    cd ruby-2.1.0 && \
    ./configure && \
    make && \
    make install && \
    cd .. && \
    rm -rf ruby-2.1.0 && \
    rm -f ruby-2.1.0.tar.gz

    View full-size slide

  43. 2. Create a Rails server image:
    murielsalvan/server

    From murielsalvan/ruby base image

    Using an interactive bash in a container to install and
    configure the Rails application

    Add a startup command: rails s

    Open default port 3000

    View full-size slide

  44. 3. Launch n docker containers from
    murielsalvan/server

    Map each container port 3000 to host ports 5000+i

    View full-size slide

  45. 4. Create a new image for the Ruby
    proxy: murielsalvan/proxy

    Based on murielsalvan/ruby

    Using an interactive bash to install and use em-proxy,
    targetting servers on ports 5000+i

    Add a startup command: ruby -w balancing.rb

    Open port 3000

    View full-size slide

  46. 5. Launch 1 docker container from
    murielsalvan/proxy

    Map its guest port 3000 to host port 3000

    View full-size slide

  47. 6. Target localhost:3000

    View full-size slide

  48. Without Docker With Docker
    0
    5
    10
    15
    20
    25
    30
    35
    40
    45
    50
    Elapsed time (s) on Fibonacci computation
    Launch time
    Execution time
    Sponsored by the
    WTF effect!

    View full-size slide

  49. RSS VSZ
    0
    20000
    40000
    60000
    80000
    100000
    120000
    140000
    160000
    180000
    Memory consumption per container (kB)
    Rails
    Linux container
    The Linux container memory is always the same whatever the process in the container:
    1,5MB RSS and 32MB VSZ

    View full-size slide

  50. Links
    Homepage
    Index of Docker images
    Dockerfile reference
    Getting started

    View full-size slide

  51. Source: xkcd
    That's all, folks!
    Thanks for attending!

    View full-size slide