The Heroku Hacker’s Guide - Cano Kayak Dcines ??5.1 Best Practices and You ... a CLI tool for creating and managing your ... The Heroku Hacker’s Guide, ...

  • Published on
    05-May-2018

  • View
    215

  • Download
    2

Embed Size (px)

Transcript

<ul><li><p>The Heroku Hackers GuideRelease 1.0</p><p>Randall Degges</p><p>September 08, 2012</p></li><li><p>CONTENTS</p><p>1 Preface 1</p><p>2 Why Heroku? 32.1 Simplicity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.2 Age . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.3 Popularity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.4 Polyglot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.5 Solid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.6 Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4</p><p>3 Getting Started with Heroku 73.1 Create an Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.2 Install the Toolbelt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.3 Install heroku-accounts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.4 Project Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93.5 Create an Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93.6 Follow the Required Quickstart Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103.7 Push Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103.8 Check it Out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10</p><p>4 Dynosaurs 114.1 What Are Dynos? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114.2 Understanding Dynos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114.3 Dynos Run Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114.4 One Procfile to Rule Them All . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124.5 Testing Your Procfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124.6 Scaling Up and Scaling Down Dynos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134.7 Handling Failure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144.8 Calculating Dyno Costs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14</p><p>5 The Environment 155.1 Best Practices and You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155.2 The Config Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155.3 Benefits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16</p><p>6 Take the Pain Away (with Addons) 176.1 What are Addons? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176.2 How do Addons Work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176.3 The Addon Catalogue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176.4 Adding, Removing, Upgrading, and Downgrading Addons . . . . . . . . . . . . . . . . . . . . . . . 18</p><p>i</p></li><li><p>6.5 Addon Cost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19</p><p>7 PostgreSQL Patterns 217.1 Why Heroku PostgreSQL? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217.2 Bootstrapping a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217.3 Connecting to Your Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227.4 Destroying a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237.5 Creating Read Slaves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237.6 Creating a Duplicate Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247.7 Promoting a Slave Database to a Master Database . . . . . . . . . . . . . . . . . . . . . . . . . . . 247.8 View Slow Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257.9 Backing Up Your Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257.10 Downloading Your Backups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267.11 Restoring From a Backup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277.12 Final Thoughts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27</p><p>8 Caching with Memcached 298.1 Why Cache? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298.2 Using Memcached . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298.3 Memcached on Heroku . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30</p><p>9 Scheduling Tasks with Cron 319.1 The Scheduler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319.2 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329.3 Cost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32</p><p>10 Logging 3310.1 Log Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3310.2 Viewing Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3310.3 Viewing Select Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3410.4 When to Check Your Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3410.5 Log Storage Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34</p><p>11 Monitoring with New Relic 3511.1 Why New Relic? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3511.2 Installing New Relic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3611.3 The Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3611.4 A Visual Map of Your Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3711.5 Web Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3811.6 Database Monitoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3811.7 External Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3911.8 Dyno Monitoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4011.9 Background Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4111.10 Final Thoughts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41</p><p>12 Talking to the World 4312.1 Using Custom Domains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4312.2 Updating Your DNS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4312.3 Encrypt All the Things! (with SSL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44</p><p>13 Managing Releases 4513.1 Herokus Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4513.2 Viewing Your Releases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4513.3 Rolling Back . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4613.4 Final Thoughts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46</p><p>ii</p></li><li><p>14 Working with Others 4714.1 Who is the Application Owner? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4714.2 Sharing Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4714.3 Sharing Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4714.4 Managing Collaborators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4814.5 Transferring Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48</p><p>15 Do Great Things 4915.1 Build Services, Not Apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4915.2 Building Service Oriented Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5015.3 Heroku and Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5015.4 Be Dynamic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5015.5 Final Thoughts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51</p><p>16 References and Further Reading 53</p><p>17 Special Thanks 55</p><p>iii</p></li><li><p>iv</p></li><li><p>CHAPTER</p><p>ONE</p><p>PREFACE</p><p>In the past several years, the cloud has really taken off. More and more companies (startups, side projects, and evenlarge enterprises) have decided to move their infrastructure to the cloud in order to reduce cost and complexity whilesimultaneously gaining availability, redundancy, and speed.</p><p>Unfortunately, while amazing cloud platforms like Amazon Web Services (http://aws.amazon.com/) have maderunning your company in the cloud affordable, they havent exactly made it simple. This is where Heroku(http://www.heroku.com/) comes in.</p><p>Heroku is a new type of cloud platform. Instead of renting virtual servers and installing your own software on them,Heroku handles the heavy lifting for youall you do is push your code (using the Git (http://git-scm.com/) versioncontrol system) to Heroku, and they handle the hard stuff:</p><p> Running your application across a specified number of virtual servers.</p><p> Handling releases: rolling back, rolling forward, storing environment variables, etc.</p><p> Ensuring your application automatically recovers from server failures.</p><p> Handling load balancing across many application instances allowing you to instantly scale your application tosupport millions of users.</p><p> Allowing you to instantly add and remove infrastructure components (databases, caching servers, etc.).</p><p> And lots more.</p><p>In my six years of writing, deploying, and managing high performance web applications, Ive spent thousands of hoursbuilding and maintaining the stuff that Heroku handles instantly out of the box.</p><p>Whenever I launch code, whether it be a hobby project, work project, or startupI instantly turn to Heroku.</p><p>My goal with this book is to teach you how to use Heroku successfully. Using a new platform is always tricky at first,but as you read through this book youll learn all you need to know about Herokus platform.</p><p>This book is for you, the hackers.</p><p>Thanks for buying The Heroku Hackers Guide (http://www.theherokuhackersguide.com/), and I hope you enjoy it!</p><p>1</p><p>http://aws.amazon.com/http://www.heroku.com/http://git-scm.com/http://www.theherokuhackersguide.com/</p></li><li><p>The Heroku Hackers Guide, Release 1.0</p><p>2 Chapter 1. Preface</p></li><li><p>CHAPTER</p><p>TWO</p><p>WHY HEROKU?</p><p>Before we get into the fun stuff, I thought Id take a moment to explain why you should be using Heroku in the firstplace. After allthere are a ton of cloud hosting platforms available, why use Heroku?</p><p>2.1 Simplicity</p><p>Out of all the cloud hosting options, Heroku is the simplest. Heroku is built around the Unix philosophy(http://en.wikipedia.org/wiki/Unix_philosophy), which states that you should do one thing, and do it well.</p><p>To deploy your application on Heroku, all you need to do is push your Git repository to their servers and bamyourcode is instantly deployed across as many virtual instances as you want.</p><p>Need specific infrastructure components? Databases? Caching servers? Key value stores? No problem, you caninstantly provision them and attach them to your application.</p><p>Because Heroku is a simple, modular platformyoull spend less time handling infrastructure, redundancy, deploy-ment, etc.and more time hacking code.</p><p>2.2 Age</p><p>Heroku has been around since 2007. They were one of the first cloud platforms ever built.</p><p>Since cloud computing (as we now know it) is relatively new (Amazon only launched its extremely popular webservices product in 2006), this means that Heroku has been in the business for a long timemuch longer than most ofthe other guys.</p><p>Furthermore, Heroku is owned by Salesforce.com (http://www.salesforce.com/), one of the oldest and largest cloudenterprise companies. Heroku wont be going away anytime soon, and you can feel safe putting your business criticalapplications on Heroku, knowing they wont be disappearing overnight.</p><p>2.3 Popularity</p><p>In addition to being around a long timeHeroku is also one of the most popular choices for cloud hosting. Chancesare that if youre a web programmer, youve either heard of or used Herkou in the past.</p><p>Herokus popularity amongst programmers means that you can easily find help from experienced developers who arefamiliar with Herokus platform. This makes hiring help a lot easier, and ensures that regardless of whether youre juststarting out, or growing to support millions of usersyoull be able to find an abundance of quality talent.</p><p>3</p><p>http://en.wikipedia.org/wiki/Unix_philosophyhttp://www.salesforce.com/</p></li><li><p>The Heroku Hackers Guide, Release 1.0</p><p>2.4 Polyglot</p><p>One of the best reasons to use Heroku is that they probably support your programming language (and web framework)of choice. Heroku touts itself as a polyglot platformand they truly are.</p><p>At the time of writing, Herkou currently supports the following programming languages:</p><p> Clojure</p><p> Java</p><p> Python</p><p> Scala</p><p> Javascript</p><p> Ruby</p><p> Go</p><p> PHP</p><p> And many more.</p><p>This means that regardless of what technologies you choose, you can feel comfortable knowing that even if youlaunch projects in different programming languages, youll be able to use the same familiar platform to deploy allyour applications.</p><p>2.5 Solid</p><p>Heroku is built on top of Amazon Web Servicesone of the largest, oldest, and most widely used cloud platforms.Amazon Web Services is known for its speed, availability, and flexibility.</p><p>While Heroku does abstract away all the nasty bits, knowing that your applications are hosted on one of the fastest andlargest platforms behind the scenes is a good thing to keep in mind.</p><p>2.6 Best Practices</p><p>The last thing Id like to bring up is best practices. One of the greatest things about Heroku is that it is built around theconcept of 12...</p></li></ul>