< Return to Video

RailsConf 2014 - Deploying Rails is Easier Than it Looks by Ben Dixon

  • 0:17 - 0:20
    BEN DIXON: OK. So, I'm Ben.
  • 0:20 - 0:22
    I'm a Rails developer and the author
  • 0:22 - 0:26
    of a book called Reliably Deploying Rails
    Applications.
  • 0:26 - 0:28
    I spend a lot of people - a lot of people?
  • 0:28 - 0:31
    A lot of time teaching people Ruby at
  • 0:31 - 0:32
    MakeItWithCode dot com,
  • 0:32 - 0:35
    which is an online mentor-driven class
  • 0:35 - 0:37
    for people who are new to Ruby
  • 0:37 - 0:39
    but want to get started making things quickly.
  • 0:39 - 0:41
    I spend the rest of my time working with
  • 0:41 - 0:44
    the awesome guys at HelloSwimio, where we
    make cool
  • 0:44 - 0:46
    tech for swimmers. So we make things like
    the
  • 0:46 - 0:49
    time tabling system for the Olympic pool in
    London,
  • 0:49 - 0:52
    the speedo swim tracking app, and the first
    automatic
  • 0:52 - 0:54
    lap tracker for the Pebble smart watch.
  • 0:54 - 0:58
    So, before we get started, could you put your
  • 0:58 - 1:00
    hands up if you've deployed a Rails app of
  • 1:00 - 1:05
    some sort to Heroku. Awesome. Now keep your
    hand
  • 1:05 - 1:07
    up if you've deployed a Rails app to a
  • 1:07 - 1:12
    server that you've set up yourself. Good.
    That's less
  • 1:12 - 1:13
    people.
  • 1:13 - 1:15
    So if someone came to me and said, I
  • 1:15 - 1:16
    want to deploy my first Rails app, how should
  • 1:16 - 1:19
    I do it? I wouldn't think about it, I'd
  • 1:19 - 1:22
    just say use Heroku. And I'd say use Heroku,
  • 1:22 - 1:26
    because Heroku makes deploying apps incredibly
    simple. You just
  • 1:26 - 1:30
    commit your code, Heroku create, git push
    Heroku, and
  • 1:30 - 1:33
    magically, a couple of minutes later, your
    application is
  • 1:33 - 1:36
    live, on the internet, for the world to see.
  • 1:36 - 1:39
    And because Heroku has made this process so
    simple
  • 1:39 - 1:41
    and so easy, it's really easy to take that
  • 1:41 - 1:42
    server stuff and kind of put it in a
  • 1:42 - 1:45
    bucket of things we don't look at. Because
    it's
  • 1:45 - 1:48
    complicated and Heroku makes it really easy,
    so why
  • 1:48 - 1:50
    would we bother?
  • 1:50 - 1:52
    And what I'd like to demonstrate in this talk
  • 1:52 - 1:54
    is that, in the same way Rails makes building
  • 1:54 - 1:57
    web applications really, really easy, there
    are lots of
  • 1:57 - 1:59
    great tools out there that make setting up
    servers
  • 1:59 - 2:02
    for them and deploying to those servers really
    easy.
  • 2:02 - 2:04
    And once you get the hang of these tools,
  • 2:04 - 2:06
    you can do some really cool stuff.
  • 2:06 - 2:10
    Before we get started, two important bits
    of terminology,
  • 2:10 - 2:13
    which probably you already know. A PaaS, a
    platform
  • 2:13 - 2:15
    as a service, and by that I mean something
  • 2:15 - 2:18
    like Heroku. So something that just abstracts
    all of
  • 2:18 - 2:19
    your infrastructure away so that you don't
    have to
  • 2:19 - 2:21
    worry about it.
  • 2:21 - 2:23
    On the other scale, you've got a VPS, a
  • 2:23 - 2:26
    Virtual Private Server, and for the purposes
    of this
  • 2:26 - 2:28
    talk, I just mean some sort of Linux server
  • 2:28 - 2:31
    running the cloud. So the type of thing you
  • 2:31 - 2:34
    get from Linode or RackSpace or DigitalOcean.
  • 2:34 - 2:36
    So, to get started, I want to take a
  • 2:36 - 2:39
    little look at what's going on behind the
    scenes
  • 2:39 - 2:42
    when you deploy a typical Rails application.
    How do
  • 2:42 - 2:45
    we go from Rails S on our development machine
  • 2:45 - 2:47
    to a Rails app on a VPS somewhere serving
  • 2:47 - 2:49
    our application to the world?
  • 2:49 - 2:52
    So, here we've got a beautiful picture that
    I've
  • 2:52 - 2:56
    drawn, because I'm new to Keynote. And the
    black
  • 2:56 - 2:58
    box around the outside is our VPS. It's a
  • 2:58 - 3:01
    single VPS. And this is the first thing that
  • 3:01 - 3:04
    seemed to surprise lots of people. For a simple
  • 3:04 - 3:08
    production configuration, you only need one
    VPS. In the
  • 3:08 - 3:10
    same way, in development, you can run your
    entire
  • 3:10 - 3:14
    Rails application on your development machine.
    For simple production
  • 3:14 - 3:17
    application, you can run everything on a single
    VPS.
  • 3:17 - 3:21
    And that VPS will have three main components
    on
  • 3:21 - 3:23
    it for almost all Rails applications. You're
    gonna have
  • 3:23 - 3:26
    a web server, you're gonna have an app server,
  • 3:26 - 3:30
    and you're gonna have a database server. So,
    when
  • 3:30 - 3:32
    someone types in the address of our website,
    a
  • 3:32 - 3:35
    request comes in. It hits our VPS, and the
  • 3:35 - 3:37
    first place that request is gonna go is our
  • 3:37 - 3:40
    web server. Now, if that request was for a
  • 3:40 - 3:43
    static file, and by a static file, I basically
  • 3:43 - 3:46
    mean, anything that goes in the Rails asset
    pipeline
  • 3:46 - 3:48
    or anything that you might find in the public
  • 3:48 - 3:50
    folder, the web server's gonna deal with that
    directly.
  • 3:50 - 3:52
    It's gonna send that file straight back to
    the
  • 3:52 - 3:54
    user for display in their browser and that's
    the
  • 3:54 - 3:55
    end of it.
  • 3:55 - 3:59
    If, however, that request is for a dynamic
    page,
  • 3:59 - 4:03
    so for a page in our Rails app, essentially,
  • 4:03 - 4:06
    that's gonna get passed back to an app server.
  • 4:06 - 4:09
    Now, when we run Rails S in development, what
  • 4:09 - 4:13
    we're starting is an instance of an app server.
  • 4:13 - 4:15
    So, the app server here is Rails S running
  • 4:15 - 4:18
    on our VPS. So the web server passes the
  • 4:18 - 4:21
    request back. The app server generates the
    response and
  • 4:21 - 4:24
    then passes it back to the web server, which
  • 4:24 - 4:26
    then returns it to the user.
  • 4:26 - 4:28
    And in development, we're probably used to
    the web,
  • 4:28 - 4:31
    the app server being something like Thin or
    WebRick.
  • 4:31 - 4:33
    In production it might also be Thin or Webrick.
  • 4:33 - 4:35
    It might also be something a bit more advanced
  • 4:35 - 4:37
    like Puma or Unicorn.
  • 4:37 - 4:41
    Finally, for pretty much all Rails applications,
    we're gonna
  • 4:41 - 4:43
    want some sort of database server. And that's
    probably
  • 4:43 - 4:47
    gonna be PostGres or mySQL or Mongo. And,
    again,
  • 4:47 - 4:50
    we don't need a separate database server.
    This all
  • 4:50 - 4:52
    runs on our single VPS.
  • 4:52 - 4:55
    So, to recap, a request comes in and it
  • 4:55 - 4:58
    hits our web server. Probably EngineX or Apache.
    If
  • 4:58 - 5:01
    it's for a static file, the web server deals
  • 5:01 - 5:03
    with it. If it's for part of our Rails
  • 5:03 - 5:05
    application, it gets passed back to the app
    server,
  • 5:05 - 5:08
    which is basically Rails S, and the app server
  • 5:08 - 5:10
    may communicate with the database server,
    read some data,
  • 5:10 - 5:13
    write some data, and then the app server constructs
  • 5:13 - 5:15
    the response, sends it back to the web server,
  • 5:15 - 5:16
    which serves it to the user.
  • 5:16 - 5:20
    So, you'd be forgiven for looking at that
    and
  • 5:20 - 5:23
    thinking, well, that already looks more complicated
    than Heroku.
  • 5:23 - 5:25
    Why on earth would you ever do that? Why
  • 5:25 - 5:26
    not just use Heroku?
  • 5:26 - 5:28
    Well, I started doing this, if I'm honest,
    because
  • 5:28 - 5:31
    of cost. I had quite a lot of side
  • 5:31 - 5:36
    projects, and as you probably already know,
    most past
  • 5:36 - 5:38
    solutions charge by the process. And a lot
    of
  • 5:38 - 5:42
    my personal projects and side projects had
    background jobs.
  • 5:42 - 5:45
    Now when you've got five or six side projects,
  • 5:45 - 5:48
    all of which would need two Heroku dynos,
    suddenly
  • 5:48 - 5:49
    that bill at the end of the month starts
  • 5:49 - 5:52
    to get quite substantial.
  • 5:52 - 5:54
    And so I wanted to save money by deploying
  • 5:54 - 5:57
    to my own servers. And I did. It's worth
  • 5:57 - 6:00
    bearing in mind, if that's also your motivation,
    that,
  • 6:00 - 6:04
    as with any new skill, you have to invert.
  • 6:04 - 6:05
    Invert? You have to put in a certain amount
  • 6:05 - 6:08
    of time to learn it. If you were to
  • 6:08 - 6:10
    account for the time I put into learning this
  • 6:10 - 6:12
    to begin with, at market rates, I'm fairly
    sure
  • 6:12 - 6:14
    there would have been no saving at all. Personally,
  • 6:14 - 6:17
    they're my own projects. I wasn't paying myself
    market
  • 6:17 - 6:19
    rates. And I was happy to treat it as
  • 6:19 - 6:21
    an investment which would pay off in the long
  • 6:21 - 6:23
    term, and it has.
  • 6:23 - 6:27
    Actually, it turned out, the biggest benefit
    of learning
  • 6:27 - 6:28
    how to do this was nothing to do with
  • 6:28 - 6:32
    money. It was the infrastructure went from
    being this
  • 6:32 - 6:34
    kind of necessary evil that I have to have
  • 6:34 - 6:36
    in order to run my Rails apps, to just
  • 6:36 - 6:39
    another tool that I could use for building
    interesting
  • 6:39 - 6:42
    stuff. And the best example of that, for me,
  • 6:42 - 6:45
    is Make It With Code. We provide our students
  • 6:45 - 6:48
    with a cloud IDE that has Ruby pre-installed.
    And
  • 6:48 - 6:50
    that means they can hit the ground running.
    They
  • 6:50 - 6:53
    can start writing Ruby code really quickly.
  • 6:53 - 6:55
    And the techniques that we use setting those
    up
  • 6:55 - 6:57
    automatically for all of our students are
    exactly the
  • 6:57 - 7:00
    same techniques that I'm gonna cover in this
    presentation
  • 7:00 - 7:02
    for setting up Rails apps. So it means that
  • 7:02 - 7:04
    our infrastructure has gone from being this
    thing that
  • 7:04 - 7:06
    we have to have in the background, as like
  • 7:06 - 7:09
    a supporting thing, to something that actually
    adds value
  • 7:09 - 7:11
    to our product in itself. It's a part of
  • 7:11 - 7:13
    our product.
  • 7:13 - 7:15
    So if you decide to give this a go,
  • 7:15 - 7:17
    I really, really urge you not to do it
  • 7:17 - 7:20
    even slightly like I did it originally. Because
    it
  • 7:20 - 7:21
    was terrible. I started off by getting a VPS.
  • 7:21 - 7:24
    That bit was good. That bit worked. I Googled,
  • 7:24 - 7:27
    how do I set up a Rails VPS. I
  • 7:27 - 7:29
    found all these great tutorials that said,
    type in
  • 7:29 - 7:31
    this command, then that command, then edit
    this config
  • 7:31 - 7:33
    file. And I found loads of things on StackOverflow
  • 7:33 - 7:35
    that said, here's what to do when that goes
  • 7:35 - 7:37
    wrong and that goes wrong. And eventually
    I had
  • 7:37 - 7:38
    this working server. And it was great.
  • 7:38 - 7:41
    And it broke a few times in the first
  • 7:41 - 7:43
    few weeks. But we fixed that when it happened.
  • 7:43 - 7:48
    And, by and large, it worked for quite awhile.
  • 7:48 - 7:50
    And it worked until it didn't work, and when
  • 7:50 - 7:52
    it didn't work, I was in France, on a
  • 7:52 - 7:54
    holiday with my family, and it was a fairly
  • 7:54 - 7:57
    rural bit of France, and we had, we had
  • 7:57 - 7:58
    a wifi connection. But it was only one wifi
  • 7:58 - 8:03
    connection for, basically, the village. And
    to access that,
  • 8:03 - 8:05
    we, we could access it by sitting half in
  • 8:05 - 8:08
    a wardrobe on the top floor of the house,
  • 8:08 - 8:10
    which was nice.
  • 8:10 - 8:12
    And, so one morning I woke up and I
  • 8:12 - 8:15
    was sitting in the wardrobe with a coffee
    and
  • 8:15 - 8:17
    checking my email, and I had a wonderful,
    a
  • 8:17 - 8:19
    lovely email from my host to say, Dear Valued
  • 8:19 - 8:21
    Customer, your server is dead, you need to
    make
  • 8:21 - 8:24
    a new one, Lots of love, from your hosts.
  • 8:24 - 8:26
    And I thought, good. Right. So I set up
  • 8:26 - 8:28
    a little desk in this wardrobe and for the
  • 8:28 - 8:30
    rest of the day, my family sat by a
  • 8:30 - 8:35
    river. They drank wine, ate cheese, mocked
    me slightly,
  • 8:35 - 8:38
    and I sat in a roof, in a wardrobe,
  • 8:38 - 8:40
    in a heat wave, and pretty much followed the
  • 8:40 - 8:43
    same tutorials I employed the first time.
    Made the
  • 8:43 - 8:45
    same mistakes that I'd made the first time,
    and
  • 8:45 - 8:47
    I got the server back up, but it really
  • 8:47 - 8:50
    wasn't any easier the second time. And that
    seemed
  • 8:50 - 8:52
    kind of wrong to me. That it seemed really
  • 8:52 - 8:52
    frustrating.
  • 8:52 - 8:54
    So I figured there must be a better way
  • 8:54 - 8:56
    of doing this. If I'm honest, actually what
    I
  • 8:56 - 8:58
    thought was, I can't believe there isn't already
    a
  • 8:58 - 9:01
    better way of doing this, I should invent
    it
  • 9:01 - 9:02
    and then I'll be rich and famous and everyone
  • 9:02 - 9:06
    will love me. Luckily I took a step back
  • 9:06 - 9:08
    and thought, OK, this probably already exists.
  • 9:08 - 9:11
    And, of course, it did. And what I wanted
  • 9:11 - 9:15
    was a configuration management tool. And a
    configuration management
  • 9:15 - 9:17
    tool is really simple. It allows you to define
  • 9:17 - 9:19
    and automate the commands that it takes to
    set
  • 9:19 - 9:22
    up the server to do a particular thing. And
  • 9:22 - 9:25
    what's amazing about a configuration management
    tool is once
  • 9:25 - 9:27
    you've done it once, doing it again and again
  • 9:27 - 9:30
    and again is completely trivial.
  • 9:30 - 9:34
    So, that time you put in to start with,
  • 9:34 - 9:35
    it will probably only be one command to set
  • 9:35 - 9:38
    up an identical server the next time. And
    to
  • 9:38 - 9:39
    give you an idea of just how simple it
  • 9:39 - 9:41
    can be, I've got a brief demonstration of
    the
  • 9:41 - 9:43
    process that I actually move, at the moment,
    on
  • 9:43 - 9:45
    a pretty much weekly basis for setting up
    new
  • 9:45 - 9:46
    servers.
  • 9:46 - 9:49
    At the end of this presentation, I'll link
    to
  • 9:49 - 9:51
    tutorials of how to do this yourself and the
  • 9:51 - 9:53
    sample code. So please don't worry about following
    it
  • 9:54 - 9:55
    step by step, I just want to give an
  • 9:55 - 9:57
    idea about how simple it can be.
  • 9:57 - 10:02
    So, here you can see, on the left, your
  • 10:02 - 10:04
    left, there is a list of servers that I've
  • 10:04 - 10:08
    deployed in the past. And I'm using my configuration
  • 10:08 - 10:11
    management tool of choice, Chef Solo, which
    uses JSON
  • 10:11 - 10:15
    files for defining what should go onto a particular
  • 10:15 - 10:16
    type of server.
  • 10:16 - 10:17
    And on the bottom you can see I've got
  • 10:17 - 10:18
    a couple of web servers for Make It With
  • 10:18 - 10:21
    Code, which at the moment is running a selection
  • 10:21 - 10:23
    of Rails and Sinatra apps. And I want to
  • 10:23 - 10:26
    write a new one. And to do this, I
  • 10:26 - 10:28
    just create a new JSON file, web3 dot makeitwithcode
  • 10:28 - 10:32
    dot com. I save it, and then this server,
  • 10:32 - 10:33
    it's gonna be pretty much the same as web2.
  • 10:33 - 10:35
    It's just running Rails apps.
  • 10:35 - 10:36
    So I just copy all the JSON from my
  • 10:36 - 10:40
    previous server, but it into the new one,
    and
  • 10:40 - 10:41
    the bit I want to draw your attention to
  • 10:41 - 10:43
    is, if you look at the bottom of that,
  • 10:43 - 10:45
    you've got this run list with a list of
  • 10:45 - 10:47
    things that are being called roles. And pretty
    much
  • 10:47 - 10:51
    all configuration management systems have
    some sort of analogous
  • 10:51 - 10:56
    concept to this, which, kind of individual
    components that
  • 10:56 - 10:58
    you can mix and match to define what should
  • 10:58 - 11:00
    go onto this server that you're setting up.
  • 11:00 - 11:03
    So I've defined in the past, what goes on
  • 11:03 - 11:05
    an internet server. What goes on a PostGres
    server.
  • 11:05 - 11:07
    And now if I want that, I can just
  • 11:07 - 11:08
    drop that role in, and I know that it
  • 11:08 - 11:10
    will be set up in exactly the way that
  • 11:10 - 11:12
    I want it. So on this server I want
  • 11:12 - 11:14
    Redis. I didn't on the previous one. So I
  • 11:14 - 11:18
    can just uncomment it. Uncomment it, yeah.
    And I'll
  • 11:18 - 11:21
    get exactly the Redis configuration I want.
  • 11:21 - 11:23
    The good bit, the fun bit, is once I've
  • 11:23 - 11:25
    done this, I can type on command - knife
  • 11:25 - 11:27
    solo bootstrap - and then the address of the,
  • 11:27 - 11:31
    the VPS I've created. Presenter. And then
    leave it
  • 11:31 - 11:34
    for about twenty minutes, go and get a coffee,
  • 11:34 - 11:36
    and the output that will scroll through, it
    will
  • 11:36 - 11:39
    tell me what commands it's applying. It will
    show
  • 11:39 - 11:42
    me diffs of any configuration files that it's
    changing.
  • 11:42 - 11:44
    And if it goes wrong, which it doesn't very
  • 11:44 - 11:45
    often, it will tell me exactly what it was
  • 11:45 - 11:47
    doing when it went wrong, so I can fix
  • 11:47 - 11:48
    it.
  • 11:48 - 11:50
    And at the end of this process, I will
  • 11:50 - 11:53
    have a Rails server which is identical the
    last
  • 11:53 - 11:55
    one I set up, and the one before that,
  • 11:55 - 11:56
    and I can check that this works. I can
  • 11:56 - 11:59
    fire up a web browser. I can go to
  • 11:59 - 12:01
    web3 and the address that I gave it, and
  • 12:01 - 12:03
    I'll get this wonderful Welcome to EngineX
    page, which
  • 12:03 - 12:06
    says, that server is set up and it's ready
  • 12:06 - 12:07
    to use.
  • 12:07 - 12:10
    So, what I'm trying to demonstrate with that
    figure
  • 12:10 - 12:15
    is that configuration management is basically,
    don't repeat yourself
  • 12:15 - 12:17
    for setting up servers. If you think back
    to
  • 12:17 - 12:20
    the first time you used, say, Devise, for
    example,
  • 12:20 - 12:22
    the first time I used Devise, it took me
  • 12:22 - 12:24
    ages to get all_auth and things like that
    working.
  • 12:24 - 12:27
    And then, six months later, I came back to
  • 12:27 - 12:28
    it, and didn't really remember any of the
    stuff
  • 12:28 - 12:31
    that I'd done to get it to work. But
  • 12:31 - 12:32
    I could go back to the code I wrote
  • 12:32 - 12:34
    the first time, I could look at that code,
  • 12:34 - 12:37
    copy bits of it, and it was much quicker.
  • 12:37 - 12:41
    Now, the problem with just copying commands
    or entering
  • 12:41 - 12:44
    commands manually when setting up the server
    is there
  • 12:44 - 12:47
    is no natural audit trail of what did I
  • 12:47 - 12:49
    do to make this work the first time. So
  • 12:49 - 12:51
    there's a good chance, the second time you're
    gonna
  • 12:51 - 12:53
    do it, you'll make the same mistakes as the
  • 12:53 - 12:54
    first time.
  • 12:54 - 12:57
    With a configuration management utility, you
    naturally create this
  • 12:57 - 13:00
    audit trail as you go along. So that, in
  • 13:00 - 13:02
    time, that time you invest up front, it goes
  • 13:02 - 13:05
    much, much further in the long run.
  • 13:05 - 13:09
    So, hopefully that's shown that the bit where
    people
  • 13:09 - 13:11
    often trip up, getting a server setup, it
    doesn't
  • 13:11 - 13:13
    need to be that difficult, and I'll give you
  • 13:13 - 13:15
    the exact sample code at the end, which you
  • 13:15 - 13:17
    can use if you want a head start.
  • 13:17 - 13:20
    But something like Heroku, it doesn't just
    make setting
  • 13:20 - 13:23
    up a server really easy. It makes deploying
    to
  • 13:23 - 13:26
    one incredibly easy. Git push heroku. It pretty
    much
  • 13:26 - 13:28
    couldn't be easier. And I think it's fair
    to
  • 13:28 - 13:30
    say that the first time I used Heroku, it
  • 13:30 - 13:32
    was completely revolutionary.
  • 13:32 - 13:34
    So I came to Rails from Python/Jengo, and
    before
  • 13:34 - 13:37
    that, PHP, and I'd used a whole range of,
  • 13:37 - 13:41
    kind of, home grown deployment systems, which
    ranged from
  • 13:41 - 13:43
    trying to remember what files you've changed
    and FTP
  • 13:43 - 13:47
    them across to all sync things and custom
    shell
  • 13:47 - 13:49
    scripts. And the one thing they all had in
  • 13:49 - 13:52
    common was they were flaky and deployment
    was something
  • 13:52 - 13:54
    you didn't want to do, because it would probably
  • 13:54 - 13:57
    break. So you maybe deployed a couple of times
  • 13:57 - 13:58
    a week.
  • 13:58 - 13:59
    So I used Heroku for the first time and
  • 13:59 - 14:02
    it was amazing. Suddenly I could apploy. Apploy?
    Deploy
  • 14:02 - 14:06
    ten times a day. And it was just fitted
  • 14:06 - 14:07
    into my normal workflow.
  • 14:07 - 14:09
    So I was really keen that if I was
  • 14:09 - 14:11
    deploying to my own servers, it had to be
  • 14:11 - 14:13
    as simple as that. It had to be one
  • 14:13 - 14:16
    command to deploy. And luckily, as is often
    the
  • 14:16 - 14:18
    case in the Rails community, loads of very
    clever
  • 14:18 - 14:20
    people wanted exactly the same thing, and
    so have
  • 14:20 - 14:23
    developed some awesome tools, some awesome
    gems, to make
  • 14:23 - 14:26
    it really simple to set that up.
  • 14:26 - 14:28
    In the Rails community, I think the best known
  • 14:28 - 14:30
    of these is probably Capistrano, which I'll
    talk about
  • 14:30 - 14:35
    a bit more towards the end of the presentation.
  • 14:35 - 14:37
    But, first, another brief demo of how simple
    it
  • 14:37 - 14:40
    is to add Capistrano into an existing, in
    this
  • 14:40 - 14:44
    case, Rails 4.1 application. Again, at the
    end, I'll
  • 14:44 - 14:47
    give details of step by, a step by step
  • 14:47 - 14:49
    tutorial and the ensemble code for doing this.
    So
  • 14:49 - 14:51
    please don't worry about the individual steps.
    I just
  • 14:51 - 14:55
    want to give an idea of what's involved.
  • 14:55 - 15:01
    So, to get started, Rails 4.1 application.
    I just
  • 15:01 - 15:05
    drop in the Capistrano gem and a few supporting
  • 15:05 - 15:08
    helper gems. Save that, and then add it to
  • 15:08 - 15:12
    git and, sorry, bundle and add it to Git.
  • 15:12 - 15:14
    Under the hood we're still using Git to deploy
  • 15:14 - 15:16
    in the same way Heroku does, so it's important
  • 15:16 - 15:18
    to make sure changes like that are committed.
  • 15:18 - 15:20
    And then running cap install, which just generates
    some
  • 15:20 - 15:23
    basic configuration files. The first one of
    these is
  • 15:23 - 15:26
    a cap file. This is just a rake file
  • 15:26 - 15:29
    for capistrano. And here, I'm just telling
    it that
  • 15:29 - 15:31
    I want to use those helper gems that I
  • 15:31 - 15:32
    included in the first step. So I'm telling
    it
  • 15:32 - 15:34
    that I want to use rbenv to manage what
  • 15:34 - 15:36
    Ruby version I'm using. I want it to install
  • 15:36 - 15:40
    gems for me, compile assets for me, and apply
  • 15:40 - 15:42
    migrations. All the normal stuff you do when
    setting
  • 15:42 - 15:43
    up a Rails app.
  • 15:43 - 15:46
    We've then got our main config file, just
    deploy
  • 15:46 - 15:48
    dot rb. And here I'm just giving the app
  • 15:48 - 15:51
    a name, telling it which Git repository to
    deploy
  • 15:51 - 15:53
    from, and getting rid of the load of the
  • 15:53 - 15:57
    boilerplate for the simple application. I'm
    not gonna use
  • 15:57 - 15:59
    it. Uncommenting out a few other bits of boilerplate.
  • 15:59 - 16:02
    And then finally I'm gonna add an rbenv specific
  • 16:02 - 16:05
    block that tells it to always use a particular
  • 16:05 - 16:08
    Ruby version. I think, in this case, 2 point
  • 16:08 - 16:10
    1 point 1, when deploying this app.
  • 16:10 - 16:13
    Finally, we have stage configuration files,
    so if you
  • 16:13 - 16:16
    have a production server and a staging server,
    and
  • 16:16 - 16:18
    here I'm just telling it that the production
    server
  • 16:18 - 16:21
    is going to be web3 dot makeitwithcode dot
    com,
  • 16:21 - 16:22
    which is the server we set up in the
  • 16:22 - 16:24
    first demo. And then that should be made available
  • 16:24 - 16:28
    to the public at, imaginatively, web3 dot
    makeitwithcode dot
  • 16:28 - 16:29
    com.
  • 16:29 - 16:33
    Then, firing up a console, running a command
    called
  • 16:33 - 16:36
    setup config, and you can think of this like
  • 16:36 - 16:38
    heroku create. This is just telling that server,
    or
  • 16:38 - 16:41
    preparing that server for a new application.
    It's generating
  • 16:41 - 16:44
    a few config files, it's creating a new database
  • 16:44 - 16:48
    yml and telling EngineX that it needs to serve
  • 16:48 - 16:51
    a new site. Then running another helper task
    that
  • 16:51 - 16:56
    I added this time. Create database. This is
    just,
  • 16:56 - 16:58
    create a new database on that server. Making
    sure
  • 16:58 - 17:00
    there's a user that has permission to access
    that
  • 17:00 - 17:01
    server.
  • 17:01 - 17:04
    And this is the important command. Cap production
    deploy.
  • 17:04 - 17:07
    And this, if we're using capistrano, is our
    equivalent
  • 17:07 - 17:10
    to git push heroku. And if you look at
  • 17:10 - 17:13
    the output, it's doing some pretty standard
    stuff. It's
  • 17:13 - 17:17
    applying migrations, compiling assets and
    restarting the app server.
  • 17:17 - 17:19
    And once that completes, we can fire up the
  • 17:19 - 17:21
    web browser, visit it, and you can see we've
  • 17:21 - 17:23
    got a simple, if utterly useless Rails application.
  • 17:23 - 17:26
    And it has access to the database, it can
  • 17:26 - 17:28
    read and write, and now we can iterate on
  • 17:28 - 17:30
    that, and every time we want to make a
  • 17:30 - 17:34
    change, we can just make that change, commit
    it,
  • 17:34 - 17:38
    cap production deploy, and it really is as
    simple
  • 17:38 - 17:41
    as deploying it to heroku.
  • 17:41 - 17:43
    So one thing that we haven't looked at yet
  • 17:43 - 17:46
    is, it's very easy to set this stuff up,
  • 17:46 - 17:48
    but the other great thing about using a path
  • 17:48 - 17:51
    like heroku is that when it breaks at 2
  • 17:51 - 17:53
    AM, it kind of isn't our problem. They have
  • 17:53 - 17:55
    engineers and they get woken up at 2 AM.
  • 17:55 - 17:58
    And they go and fix it. And our application
  • 17:58 - 18:00
    just comes back up online again.
  • 18:00 - 18:04
    And we can't get around that completely. It's
    now
  • 18:04 - 18:06
    our problem when it breaks at 2 AM. But
  • 18:06 - 18:10
    luckily there are lots of tools out there
    which
  • 18:10 - 18:12
    mean we can minimize the number of times this
  • 18:12 - 18:15
    will happen. If you think in a typical production
  • 18:15 - 18:20
    Rails application, you probably got some exception
    handling. You
  • 18:20 - 18:22
    know some exceptions will happen from time
    to time,
  • 18:22 - 18:24
    and so you've got to have your begin and
  • 18:24 - 18:26
    rescues in that. And then hopefully you've
    also got
  • 18:26 - 18:29
    some sort of exception notification. And that
    means that
  • 18:29 - 18:32
    when an exception that you haven't planned
    for happens,
  • 18:32 - 18:34
    you get an email and you know that you
  • 18:34 - 18:35
    can go in, you should go in and fix
  • 18:35 - 18:36
    it.
  • 18:36 - 18:39
    So, there are an analogous set of tools when
  • 18:39 - 18:42
    you're setting up your own servers. And these
    are
  • 18:42 - 18:45
    called monitoring tools, and they're really,
    really simple. They're
  • 18:45 - 18:47
    essentially just a list of checks that say,
    here's
  • 18:47 - 18:50
    how something ought to behave. If it's not
    behaving,
  • 18:50 - 18:52
    here's a command that you can run to try
  • 18:52 - 18:56
    and fix it. If it's still not working, here's
  • 18:56 - 18:58
    the person to notify and how to notify them
  • 18:58 - 18:59
    there's a problem.
  • 18:59 - 19:02
    And that might sound a little bit daunting.
    So
  • 19:02 - 19:03
    every time you set up an application you're
    gonna
  • 19:04 - 19:06
    have to think about all these checks. In practice,
  • 19:06 - 19:09
    this is where configuration management comes
    in. So, if
  • 19:09 - 19:11
    you remember in that first demonstration,
    when I uncommented
  • 19:11 - 19:14
    that Redis role, as part of that Redis role,
  • 19:14 - 19:17
    when I originally wrote it, probably a year
    or
  • 19:17 - 19:19
    a couple of years ago, I included the monitoring
  • 19:19 - 19:23
    configuration for it, which basically says,
    is Redis working?
  • 19:23 - 19:25
    If no, restart it. If it's still not working,
  • 19:25 - 19:26
    email me.
  • 19:26 - 19:29
    And now that will get automatically setup
    every time
  • 19:29 - 19:31
    I include that Redis role. So there really
    isn't
  • 19:31 - 19:34
    that much duplication. Configuration management
    just does all of
  • 19:34 - 19:36
    this for us.
  • 19:36 - 19:39
    So to get a bit more practical, if you
  • 19:39 - 19:42
    decide to, to go ahead and try this. This
  • 19:42 - 19:44
    is the stack that I end up using, probably
  • 19:44 - 19:49
    99% of the time. For a web server, EngineX,
  • 19:49 - 19:51
    really popular in the Rails community. There's
    lots of
  • 19:51 - 19:54
    really great Rails-specific documentation
    out there.
  • 19:54 - 19:57
    For an app server, Unicorn. This probably
    isn't what
  • 19:57 - 19:58
    you're using in development at the moment.
    That's more
  • 19:58 - 20:01
    likely to be Thin or Webrick. Unicorn's a
    little
  • 20:01 - 20:04
    bit more complicated. But, for that little
    bit of
  • 20:04 - 20:08
    extra complexity, you get some really nice
    features. In
  • 20:08 - 20:10
    particular, it's very easy to set up what's
    called
  • 20:10 - 20:14
    zero-downtime deployment, which you may already
    be familiar with.
  • 20:14 - 20:17
    Essentially what this means is, the simplest
    way to
  • 20:17 - 20:21
    deploy an application is to deploy your new
    code,
  • 20:21 - 20:24
    stop the existing application, and then start
    it again
  • 20:24 - 20:25
    with the new code. The problem is, if you've
  • 20:25 - 20:27
    got a big Rails application, it can take two
  • 20:27 - 20:29
    or three minutes to start up. And that means
  • 20:29 - 20:31
    that, for two or three minutes, every time
    you
  • 20:31 - 20:34
    deploy, your app's offline, which is pretty
    bad for
  • 20:34 - 20:37
    your customers if you're doing this five,
    six times
  • 20:37 - 20:38
    a day.
  • 20:38 - 20:39
    With Unicorn, you can set it up so that
  • 20:39 - 20:42
    when you deploy new code, it will start up
  • 20:42 - 20:44
    your app in the background, and only once
    it's
  • 20:44 - 20:46
    ready will it switch it over and stop the
  • 20:46 - 20:51
    previous version, so your customers don't
    notice an interruption.
  • 20:51 - 20:54
    For a database, I typ, by default go with
  • 20:54 - 20:56
    PostGres. Again, you've probably all heard
    of it. Very
  • 20:56 - 20:59
    popular in the Rails community. I recommend
    it to
  • 20:59 - 21:01
    people, because if you're on the Heroku free
    plan,
  • 21:01 - 21:06
    it's probably what you're using already, because
    it's their
  • 21:06 - 21:07
    default database.
  • 21:07 - 21:09
    The only controversial item on this list,
    because all
  • 21:09 - 21:11
    lists should have at least one controversial
    item, is
  • 21:11 - 21:14
    probably Monit. And it's a bit controversial
    because there
  • 21:14 - 21:16
    are some amazing monitoring tools out there
    which are
  • 21:16 - 21:21
    written using Ruby syntax for defining rules.
    I keep
  • 21:21 - 21:25
    coming back to Monit because it's tiny. Its
    syntax
  • 21:25 - 21:26
    isn't quite as nice to work with as some
  • 21:26 - 21:28
    of the others, some of the others, but it
  • 21:28 - 21:30
    uses absolutely no memory, and once you set
    it
  • 21:30 - 21:32
    up, it just stays out of the way and
  • 21:32 - 21:36
    works, which is pretty much what I look for
  • 21:36 - 21:37
    in a monitoring tool.
  • 21:37 - 21:39
    For setting up your server, so your server
    provisioning
  • 21:39 - 21:43
    tool, I recommend getting started with Chef
    Solo. Chef
  • 21:43 - 21:45
    Solo is the tool you saw me using in
  • 21:45 - 21:50
    the first demonstration. And it's designed
    for deploying individual
  • 21:50 - 21:53
    servers from your development machine. What's
    really nice about
  • 21:53 - 21:56
    Chef Solo is it's built on top of a
  • 21:56 - 21:59
    much bigger piece of software called Chef
    Server. And
  • 21:59 - 22:01
    Chef Server is designed for when you need
    to
  • 22:01 - 22:04
    deploy lots of servers in complicated configurations.
  • 22:04 - 22:06
    So, if you need, eventually, to move on from
  • 22:06 - 22:11
    your simple single-box configuration to much
    more complicated ones,
  • 22:11 - 22:14
    almost everything you've learnt using Chef
    Solo will be
  • 22:14 - 22:15
    applicable to Chef Server.
  • 22:15 - 22:21
    I mentioned, previously, there were lots of
    deployment tools,
  • 22:21 - 22:24
    automation tools out there for Rails. Probably
    the best-known
  • 22:24 - 22:28
    one is Capistrano. At the moment, there are
    two
  • 22:28 - 22:31
    stable versions of Capistrano out in the wild.
    There
  • 22:31 - 22:36
    is Capistrano 2 and Capistrano 3. Most of
    the
  • 22:36 - 22:40
    documentation and tutorials that you find
    will probably be
  • 22:40 - 22:43
    for Capistrano 2.
  • 22:43 - 22:46
    Despite that, I recommend starting off with
    Capistrano 3,
  • 22:46 - 22:48
    if you are starting from a blank slate. The
  • 22:48 - 22:51
    reason for this is that version 3 was a
  • 22:51 - 22:55
    complete ground-up rewrite of version 2. In
    particular, it's
  • 22:55 - 22:59
    just a rake application. So Capistrano 3 is
    just
  • 22:59 - 23:04
    a, a layer on top of rake that provides
  • 23:04 - 23:07
    deployment-specific functionality. And that
    means that any knowledge you've
  • 23:07 - 23:09
    already got of how to write rake tasks and
  • 23:09 - 23:11
    how to interact with rake tasks and how to
  • 23:11 - 23:14
    debug them is directly applicable to Capistrano
    3. And
  • 23:14 - 23:16
    that just makes the learning curve that little
    bit
  • 23:16 - 23:19
    shallower.
  • 23:19 - 23:21
    So next steps, if you're gonna give it a
  • 23:21 - 23:24
    go. I suggest you go and get a VPS.
  • 23:24 - 23:27
    I suggest starting off with a one gig one.
  • 23:27 - 23:29
    You can run a Rails app on a 512
  • 23:29 - 23:31
    meg VPS. You can also spend a huge amount
  • 23:31 - 23:33
    of time troubleshooting out of memory hours,
    which I
  • 23:33 - 23:37
    think is the least enjoyable thing possible.
    You can
  • 23:37 - 23:39
    get a one gig VPS for about ten dollars
  • 23:39 - 23:43
    a month at DigitalOcean, I think. They're
    also a
  • 23:43 - 23:46
    sponsor, and they're really generous with
    free credit. So
  • 23:46 - 23:47
    if you corner them in a booth, I imagine
  • 23:47 - 23:49
    you can get at least a few months of
  • 23:49 - 23:50
    experimentation for free.
  • 23:50 - 23:54
    So, the url at the top there, talkingquickly
    dot
  • 23:54 - 23:59
    co dot uk slash deploying_rails. That's a
    page specific
  • 23:59 - 24:02
    to this presentation, and it contains links
    to two
  • 24:02 - 24:06
    key tutorials. The first one goes through
    step-by-step how
  • 24:06 - 24:08
    to set up a Rails server in the way
  • 24:08 - 24:11
    I did in the first video, and the second
  • 24:11 - 24:13
    one is how to set up Capistrano with a
  • 24:13 - 24:16
    new or existing Rails application, as per
    the second
  • 24:16 - 24:18
    video.
  • 24:18 - 24:21
    They also include the complete sample code
    that I'm
  • 24:21 - 24:23
    actually using in production, pretty much
    every day, for
  • 24:23 - 24:28
    deploying new Rails apps. So, hopefully, the
    fairly large
  • 24:28 - 24:29
    amount of time I put in originally trying
    to
  • 24:29 - 24:31
    work out how to get a working configuration
    management
  • 24:31 - 24:33
    set up, you can take that as a starting
  • 24:33 - 24:36
    point and get it started very quickly. It
    should
  • 24:36 - 24:38
    take maybe an hour and a half to get
  • 24:38 - 24:40
    your first server set up and an app deployed
  • 24:40 - 24:41
    to it.
  • 24:41 - 24:43
    I mentioned at start, I've also written a
    book
  • 24:43 - 24:46
    about this. Reliably Deploying Rails Applications.
  • 24:46 - 24:47
    This was basically
  • 24:47 - 24:49
    the reference that I wish I'd had when I
  • 24:49 - 24:52
    started doing this. It uses exactly the same
    sample
  • 24:52 - 24:56
    code as those tutorials. So if you start off
  • 24:56 - 24:58
    using those and find you want to do more
  • 24:58 - 25:00
    complicated things,
  • 25:00 - 25:01
    hopefully that will be a useful reference.
  • 25:01 - 25:04
    And there's a discount code, et cetera, for
    RailsConf
  • 25:04 - 25:07
    attendees on that link.
  • 25:07 - 25:11
    Finally, I'll be in the AirPad Pit at 4:30
  • 25:11 - 25:12
    today. So if anyone wants to give this a
  • 25:12 - 25:15
    go for real and wants a hand setting stuff
  • 25:15 - 25:17
    up, I'd be very happy to help out. Yeah.
  • 25:17 - 25:20
    Thank you so much for coming to the talk.
  • 25:20 - 25:23
    I've really enjoyed doing this. Let's have
    some questions.
Title:
RailsConf 2014 - Deploying Rails is Easier Than it Looks by Ben Dixon
Description:

more » « less
Video Language:
English
Duration:
25:51

English subtitles

Revisions