Share
Embed
The 12Factor Django App
Short talk I gave at the 2012 Chicago Barcamp
Adam "Cezar" Jenkins
September 22, 2012
Transcript
55 slides
-
What is 12factor The twelve-factor app is a methodology for
building software- as-a-service apps that Sunday, September 23, 12 -
Use declarative formats for setup automation, to minimize time and
cost for new developers joining the project. Sunday, September 23, 12 -
Have a clean contract with the underlying operating system, offering
maximum portability between execution environments. Sunday, September 23, 12 -
Are suitable for deployment on modern cloud platforms, obviating the
need for servers and systems administration. Sunday, September 23, 12 -
Minimize divergence between development and production, enabling continuous deployment for
maximum agility. Sunday, September 23, 12 -
And can scale up without significant changes to tooling, architecture,
or development practices. Sunday, September 23, 12 -
I. Codebase One codebase, tracked in revision control, many deploys.
Sunday, September 23, 12 -
I. Codebase What have we been doing? Sharing code between
apps. Factor it out and make a library Keeping our code in rsynced tarballs Keeping a development repo and a production repo. Sunday, September 23, 12 -
II. Dependencies Your dependencies should be spelled out. Never rely
on a system dependency. E.g. , never assume Django is on the system. Sunday, September 23, 12 -
II. Dependencies Your dependencies should be spelled out. Never rely
on a system dependency. E.g. , never assume Django is on the system. Virtualenv and pip are your friend. Sunday, September 23, 12 -
III. Config $ ls settings.py prod-settings.py dev-settings.py local-settings.py dev ≠
staging ≠ prod Sunday, September 23, 12 -
III. Config $ cat settings.py import os MY_SETTING = os.environ.get(‘SETTING’)
Dev = Staging = Production Sunday, September 23, 12 -
III. Config How do you handle .env environment variables? Sunday,
September 23, 12 -
III. Config How do you handle .env environment variables? Development:
Sunday, September 23, 12 -
III. Config How do you handle .env environment variables? Development:
$ touch project/.env $ echo "echo 'woah'" > project/.env $ cd project woah Autoenv: Sunday, September 23, 12 -
III. Config Production: Use Honcho (python Foreman) web: python serve.py
redis: redis-server Uses a Procfile Sunday, September 23, 12 -
V. Build, release, run The build stage is a transform
which converts a code repo into an executable bundle known as a build. Using a version of the code at a commit specified by the deployment process, the build stage fetches and vendors dependencies and compiles binaries and assets. Sunday, September 23, 12 -
V. Build, release, run The build stage is a transform
which converts a code repo into an executable bundle known as a build. Using a version of the code at a commit specified by the deployment process, the build stage fetches and vendors dependencies and compiles binaries and assets. The release stage takes the build produced by the build stage and combines it with the deploy’s current config. The resulting release contains both the build and the config and is ready for immediate execution in the execution environment. Sunday, September 23, 12 -
V. Build, release, run The build stage is a transform
which converts a code repo into an executable bundle known as a build. Using a version of the code at a commit specified by the deployment process, the build stage fetches and vendors dependencies and compiles binaries and assets. The release stage takes the build produced by the build stage and combines it with the deploy’s current config. The resulting release contains both the build and the config and is ready for immediate execution in the execution environment. The run stage (also known as “runtime”) runs the app in the execution environment, by launching some set of the app’s processes against a selected release. Sunday, September 23, 12 -
VI. Processes Execute the app as one or more stateless
processes $ python manage.py Sunday, September 23, 12 -
VI. Processes Execute the app as one or more stateless
processes $ python manage.py Processes share nothing Sunday, September 23, 12 -
VII. Port binding Export services via port binding. ... run_gunicorn
-b 0.0.0.0:$PORT Let nginx/apache handle routing to your app. Sunday, September 23, 12 -
VIII. Concurrency Scale out your application using the process model
Sunday, September 23, 12 -
VIII. Concurrency The share-nothing, horizontally partitionable nature of twelve-factor app
processes means that adding more concurrency is a simple and reliable operation. Sunday, September 23, 12 -
VIII. Concurrency Twelve-factor app processes should never daemonize or write
PID files. Instead, rely on the operating system’s process manager. Supervisor is very common in the Django world. Sunday, September 23, 12 -
IX. Disposability The app should start quickly and shut down
gracefully. Sunday, September 23, 12 -
IX. Disposability The app should start quickly and shut down
gracefully. SIGTERM causes shutdown Shutdown is handled by the process manager (supervisor) Sunday, September 23, 12 -
IX. Disposability The app should start quickly and shut down
gracefully. SIGTERM causes shutdown Shutdown is handled by the process manager (supervisor) The app should handle sudden death. Sunday, September 23, 12 -
X. Dev/prod parity Keep development, staging, and production as similar
as possible. Failing to do so increases: the time gap, personnel gap, and the tools gap. Sunday, September 23, 12 -
X. Dev/prod parity Traditional app Twelve- factor app Time between
deploys Weeks Hours Code authors vs code deployers Different People Same People Dev vs production environments Divergent As Similar as Possilbe Sunday, September 23, 12 -
XI. Logs App never concerns itself with routing or storage
of its output stream. Sunday, September 23, 12 -
XI. Logs App never concerns itself with routing or storage
of its output stream. Each process writes to stdout. Sunday, September 23, 12 -
XI. Logs App never concerns itself with routing or storage
of its output stream. Each process writes to stdout. The Python logger will work just fine Sunday, September 23, 12 -
XI. Logs App never concerns itself with routing or storage
of its output stream. Each process writes to stdout. The Python logger will work just fine Can see the whole ecosystem, searchable. Sunday, September 23, 12 -
XI. Logs App never concerns itself with routing or storage
of its output stream. Each process writes to stdout. The Python logger will work just fine Can see the whole ecosystem, searchable. OS decides where they go. Loggly, etc. Sunday, September 23, 12 -
XI. Logs 2012-02-22T19:56:40+00:00 [postgres]: .... 2012-02-22T19:56:40+00:00 [router]: GET mysite.com/ ...
2012-02-22T19:56:40+00:00 [nginx]: .... 2012-02-22T19:56:40+00:00 [worker]: .... Sunday, September 23, 12 -
XII. Admin processes One off processes: manage.py syncdb Identical environment.
Same code, config. Sunday, September 23, 12 -
XII. Admin processes One off processes: manage.py syncdb Identical environment.
Same code, config. Shipped with code. Sunday, September 23, 12