Django Deployments Done Right

Django Deployments Done Right

Deployment is the process of getting code from your laptop to a production-level server. What makes a "good" deployment? This talk will give you a tooling-agnostic look at the steps that you should be taking during deployment and show you how to overcome some common pitfalls in the process.

A464f1f39d9d840f9ca156e9abcfd4a9?s=128

Peter Baumgartner

September 08, 2015
Tweet

Transcript

  1. Django Deployments Done Right Peter Baumgartner Founder, Lincoln Loop DjangoCon

    US 2015
  2. About Me

  3. Founder at Lincoln Loop

  4. Co-author of High Performance Django

  5. Django Deployments Done Right

  6. Python Deployments Done Right

  7. Deployments Done Right

  8. What is a deployment?

  9. What is a deployment? • Noun: The way your servers

    and software are setup
  10. What is a deployment? • Noun: The way your servers

    and software are setup • Verb: The techniques used to install and update your software on 
 the servers
  11. Why do you want a good deployment?

  12. Uptime!

  13. Humans

  14. Bad Deployments

  15. Bad Deployments • Stressful • Cause of burnout • Employee

    turnover
  16. Good Deployments

  17. Good Deployments • Non-events • Empowering

  18. Business

  19. Good Deployments • Ship faster • new features • bug

    fixes • failure recovery • Happy Humans • more productive • less turnover
  20. What makes a good deployment?

  21. It works

  22. It is reliable

  23. It is boring

  24. It is user-friendly

  25. It is fast

  26. It is not disruptive

  27. It is idempotent & deterministic

  28. How to…

  29. Use Configuration Management!

  30. Configuration Management • Use Salt or Ansible 
 (unless other

    in-house experience) • Fabric is not a configuration management tool • Use it for everything
 (setup, updates, deploys, etc.)
  31. Pin Dependencies

  32. Pin Dependencies • Install exact versions PyPI, e.g. Django==1.8.4 •

    Consider setup.py and install_requires over requirements.txt • Can’t use PyPI? • Setup your own (pip2pi + Nginx or S3) • Vendor • Use a repo that you own
  33. Reload, Don’t Restart

  34. Reload, Don’t Restart • Don’t drop requests during a deploy

    • service {nginx,varnish} reload • Gracefully reload WSGI servers
 (gunicorn, uWSGI, etc.) • Works out-of-the-box with Upstart • systemd?
 ExecReload=/bin/kill -HUP $MAINPID
  35. Make it User- Friendly

  36. Make it User-Friendly ssh salt "sudo salt 'web*' state.highstate"

  37. Make it User-Friendly ssh salt "sudo salt 'web*' state.highstate" ansible-playbook

    -i production web.yml
  38. Make it User-Friendly ssh salt "sudo salt 'web*' state.highstate" ansible-playbook

    -i production web.yml
  39. Make it User-Friendly ssh salt "sudo salt 'web*' state.highstate" ansible-playbook

    -i production web.yml fab production deploy
  40. ssh salt "sudo salt 'web*' state.highstate" ansible-playbook -i production web.yml

    fab production deploy Make it User-Friendly
  41. Make it User-Friendly fab production deploy fab staging deploy:my-branch fab

    staging logs fab production status
  42. Make it User-Friendly

  43. Make it User-Friendly

  44. Isolate Each Build

  45. Isolate Each Build • New build/release = New virtualenv •

    Broken builds don't take down the site • No cruft from previous builds • Fast painless rollbacks
  46. Isolate Each Build

  47. Isolate Each Build

  48. Isolate Each Build • Multiple servers?
 Build once, deploy everywhere

    1. Build wheels 2. Bundle 3. Ship • See platter.pocoo.org
  49. Database Migrations

  50. Database Migrations • Maintain backwards compatibility
 (for a few releases)

    • Big database and lots of traffic? • Avoid long table locks • See bit.ly/db-mg
  51. Track Releases

  52. Track Releases • Store commit hash and timestamp • Invaluable

    for regression tracking • NewRelic, Opbeat, Slack, etc.
  53. Always Be Improving

  54. Always Be Improving • Remember to think of the humans!

    • Can failures be avoided? • Can you make it faster? • Can you make it easier?
  55. Thank You! Peter Baumgartner @ipmb pete@lincolnloop.com