the 101 guide to deploying django
play

The 101 guide to deploying Django Django Day Copenhagen 2020 - PowerPoint PPT Presentation

Iulia Avram The 101 guide to deploying Django Django Day Copenhagen 2020 Introduction whoami - developer - curious as a cat Roadmap Packing and shipping Django, Docker and Kubernetes WSGI First portable solution to connect an app


  1. Iulia Avram The 101 guide to deploying Django Django Day Copenhagen 2020

  2. Introduction whoami - developer - curious as a cat

  3. Roadmap Packing and shipping Django, Docker and Kubernetes WSGI First portable solution to connect an app to a server Ready to go Onwards into the So you’re ready to future ship the product New practices and Roadtrip!! into the world... possible futures Best practices Checklist and some nice to haves ASGI The power of async

  4. Roadmap Packing and shipping Django, Docker and Kubernetes WSGI First portable solution to connect an app to a server Ready to go Onwards into the So you’re ready to future ship the product New practices and into the world... possible futures Best practices Checklist and some nice to haves ASGI The power of async

  5. Ready to go What happens when you deploy an application?

  6. Ready to go WHOOSH ! your code a wild server

  7. technically Ready to go any piece of software or hardware with a continuous process and a unique IP WHOOSH ! your code a wild server

  8. technically Ready to go any piece of software or hardware with a continuous process and a unique IP WHOOSH ! your code a wild server

  9. Ready to go How does the code get to the wild server?

  10. Ready to go 1. You can install it directly 🤮

  11. Ready to go 1. You can install it directly 🤮 2. Use a container for easy replication (such as Docker) 📧

  12. Ready to go 1. You can install it directly 🤮 2. Use a container for easy replication (such as Docker) 📧 3. Go serverless ☁

  13. Ready to go But before that, we need a server for the server...

  14. Ready to go The Django documentation mentions two main methods of deploying : ● WSGI only supports synchronous code ● ASGI asynchronous-friendly

  15. Roadmap Packing and shipping Django, Docker and Kubernetes WSGI First portable solution to connect an app to a server Ready to go Onwards into the So you’re ready to future ship the product New practices and into the world... possible futures Best practices Checklist and some nice to haves ASGI The power of async

  16. WSGI WSGI It was first specified in PEP 333 and then in PEP 333(3) -> with an addition for Python 3 It contains a very detailed interface specification between a server/gateway and an application/framework

  17. WSGI GET /cats HTTP 1.0 request Web server

  18. WSGI Web server GET /cats HTTP 1.0 request Application/ Server/Gateway framework

  19. WSGI invoke a callable object provided by the application send back response Application/ framework Server/Gateway

  20. WSGI

  21. WSGI

  22. WSGI

  23. WSGI

  24. WSGI

  25. WSGI

  26. WSGI

  27. WSGI

  28. WSGI

  29. WSGI WSGI ARGUMENTS environ dictionary object containing CGI-style environment variables start_response callable accepting 2 positional arguments and one optional - status : string, response_headers : list of tuples containing (header_name, value) and exc_info : used with errors

  30. WSGI WSGI example Source: https://www.python.org/dev/peps/pep-0333/

  31. WSGI Limitations of WSGI - it’s synchronous - no websockets - no await/async - only works with the HTTP protocol

  32. Roadmap Packing and shipping Django, Docker and Kubernetes WSGI First portable solution to connect an app to a server Ready to go Onwards into the So you’re ready to future ship the product New practices and into the world... possible futures Best practices Checklist and some nice to haves ASGI The power of async

  33. ASGI ASGI “spiritual successor to WSGI”, compatible with WSGI ● ● async/await operation support websockets ● ● HTTP and HTTP/2 protocols

  34. ASGI

  35. ASGI

  36. ASGI

  37. ASGI

  38. ASGI

  39. ASGI [....]

  40. ASGI [....]

  41. ASGI ARGUMENTS ASGI scope - a dictionary with at least a key(‘type’) to specify the incoming protocol - equivalent of `environ` in WSGI receive - awaitable callable that will yield an event dictionary send - awaitable callable that takes an event dictionary as a parameter and returns a response once the message has been sent or the connection closed

  42. ASGI [....]

  43. ASGI ASGI examples

  44. ASGI ASGI examples follows the WSGI environ dictionary

  45. ASGI When can ASGI save the day?

  46. Roadmap Packing and shipping Django, Docker and Kubernetes WSGI First portable solution to connect an app to a server Ready to go Onwards into the So you’re ready to future ship the product New practices and into the world... possible futures Best practices Checklist and some nice to haves ASGI The power of async

  47. Docker Web server Django app

  48. Docker Web server Django app

  49. Docker

  50. Docker Now let’s install the first server on top of our Django application. This permits us to have multi-threaded operations. Docker container Django app Server/Gateway

  51. Docker Now let’s install the first server on top of our Django application. This permits us to have multi-threaded operations. Docker container Django app

  52. Docker

  53. Docker - socket - module - how many workers - what to do on exit - etc

  54. Docker Now usually comes the part where you add another server on top. Or a gateway. Or a load balancer. Docker container the usual choice Django app

  55. Docker docker-compose to the rescue build a container for the app accessed by a WSGI/ASGI compliant server (uWsgi earlier) you will need a Dockerfile for it and a build a container file for parameters; and for the reverse don’t forget to touch up proxy and link it STATIC_URL and to the app server STATIC_ROOT if paying attention to you’re serving static port binding can files save you a lot of headaches

  56. Docker The next step after that is deploying to some container orchestration tool such as Kubernetes. clustering different containers ● together scalable and configurable ● easier deployment and ● management

  57. Docker Kubernetes YML example Source: https://cloud.google.com/python/django/kubernetes-engine

  58. Roadmap Packing and shipping Django, Docker and Kubernetes WSGI First portable solution to connect an app to a server Ready to go Onwards into the So you’re ready to future ship the product New practices and into the world... possible futures Best practices Checklist and some nice to haves ASGI The power of async

  59. Best practices Some best practices I learned over time (sometimes the hard way)

  60. Best practices Some best practices I learned over time (sometimes the hard way) Use the checklist The Django checklist is very useful and it is recommended that you use it when deploying. Add items to the checklist that suit your needs.

  61. Best practices Some best practices I learned over time (sometimes the hard way) Use the checklist The Django checklist is very useful and it is recommended that you use it when deploying. Add items to the checklist that suit your needs. Monitor Don’t forget to log. And read those logs. Use the tools available.

  62. Best practices Some best practices I learned over time (sometimes the hard way) Be careful of sensitive data Use the checklist Take care of your users. Use environment variables where possible. Act apprehensive when it comes to security. The Django checklist is very useful and it is recommended that you use it when deploying. Add items to the checklist that suit your needs. Monitor Don’t forget to log. And read those logs. Use the tools available.

  63. Best practices Some best practices I learned over time (sometimes the hard way) Keep Docker files clean Be careful of sensitive data The order in which you run Use the checklist commands matters. Don’t give root Take care of your users. Use environment permissions to the server. variables where possible. Act apprehensive when it comes to security. The Django checklist is very useful and it is recommended that you use it when deploying. Add items to the checklist that suit your needs. Monitor Don’t forget to log. And read those logs. Use the tools available.

  64. Best practices Some best practices I learned over time (sometimes the hard way) Keep Docker files clean Be careful of sensitive data The order in which you run Use the checklist commands matters. Don’t give root Take care of your users. Use environment permissions to the server. variables where possible. Act apprehensive when it comes to security. The Django checklist is very useful and it is recommended that you use it when deploying. Add items to the checklist that suit your needs. Monitor Define what you want and stick to it You are in control of which tools or patterns you’re going to Don’t forget to log. And use. Mix and match. If something doesn’t work, change it. read those logs. Use the tools available.

Recommend


More recommend