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

the 101 guide to deploying django
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Iulia Avram Django Day Copenhagen 2020

The 101 guide to deploying Django

slide-2
SLIDE 2

whoami

Introduction

  • developer
  • curious as a cat
slide-3
SLIDE 3

Roadmap

Ready to go

So you’re ready to ship the product into the world...

Onwards into the future

New practices and possible futures

WSGI ASGI Best practices Packing and shipping

First portable solution to connect an app to a server The power of async Django, Docker and Kubernetes Checklist and some nice to haves

Roadtrip!!

slide-4
SLIDE 4

Roadmap

Ready to go

So you’re ready to ship the product into the world...

Onwards into the future

New practices and possible futures

WSGI ASGI Best practices Packing and shipping

First portable solution to connect an app to a server The power of async Django, Docker and Kubernetes Checklist and some nice to haves

slide-5
SLIDE 5

What happens when you deploy an application?

Ready to go

slide-6
SLIDE 6

Ready to go

your code a wild server

WHOOSH!

slide-7
SLIDE 7

Ready to go

your code a wild server

WHOOSH! technically any piece of software or hardware with a continuous process and a unique IP

slide-8
SLIDE 8

Ready to go

your code a wild server

WHOOSH! technically any piece of software or hardware with a continuous process and a unique IP

slide-9
SLIDE 9

How does the code get to the wild server?

Ready to go

slide-10
SLIDE 10

Ready to go

  • 1. You can install it directly 🤮
slide-11
SLIDE 11

Ready to go

  • 1. You can install it directly 🤮
  • 2. Use a container for easy replication (such as

Docker) 📧

slide-12
SLIDE 12

Ready to go

  • 1. You can install it directly 🤮
  • 2. Use a container for easy replication (such as

Docker) 📧

  • 3. Go serverless ☁
slide-13
SLIDE 13

But before that, we need a server for the server...

Ready to go

slide-14
SLIDE 14

Ready to go

The Django documentation mentions two main methods of deploying

:

  • WSGI
  • ASGI
  • nly supports synchronous code

asynchronous-friendly

slide-15
SLIDE 15

Roadmap

Ready to go

So you’re ready to ship the product into the world...

Onwards into the future

New practices and possible futures

WSGI ASGI Best practices Packing and shipping

First portable solution to connect an app to a server The power of async Django, Docker and Kubernetes Checklist and some nice to haves

slide-16
SLIDE 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

slide-17
SLIDE 17

GET /cats HTTP 1.0 request

WSGI

Web server

slide-18
SLIDE 18

GET /cats HTTP 1.0

request

WSGI

Web server

Server/Gateway Application/ framework

slide-19
SLIDE 19

WSGI

Server/Gateway Application/ framework

invoke a callable object provided by the application send back response

slide-20
SLIDE 20

WSGI

slide-21
SLIDE 21

WSGI

slide-22
SLIDE 22

WSGI

slide-23
SLIDE 23

WSGI

slide-24
SLIDE 24

WSGI

slide-25
SLIDE 25

WSGI

slide-26
SLIDE 26

WSGI

slide-27
SLIDE 27

WSGI

slide-28
SLIDE 28

WSGI

slide-29
SLIDE 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

slide-30
SLIDE 30

WSGI

WSGI example

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

slide-31
SLIDE 31

WSGI

Limitations of WSGI

  • it’s synchronous
  • no websockets
  • no await/async
  • nly works with the HTTP protocol
slide-32
SLIDE 32

Roadmap

Ready to go

So you’re ready to ship the product into the world...

Onwards into the future

New practices and possible futures

WSGI ASGI Best practices Packing and shipping

First portable solution to connect an app to a server The power of async Django, Docker and Kubernetes Checklist and some nice to haves

slide-33
SLIDE 33

ASGI

ASGI

  • “spiritual successor to WSGI”, compatible with WSGI
  • async/await operation support
  • websockets
  • HTTP and HTTP/2 protocols
slide-34
SLIDE 34

ASGI

slide-35
SLIDE 35

ASGI

slide-36
SLIDE 36

ASGI

slide-37
SLIDE 37

ASGI

slide-38
SLIDE 38

ASGI

slide-39
SLIDE 39

ASGI

[....]

slide-40
SLIDE 40

ASGI

[....]

slide-41
SLIDE 41

ASGI

ASGI ARGUMENTS

scope receive send

  • a dictionary with at least a key(‘type’) to specify the incoming protocol
  • equivalent of `environ` in WSGI
  • awaitable callable that will yield an event dictionary
  • awaitable callable that takes an event dictionary as a parameter and returns a

response once the message has been sent or the connection closed

slide-42
SLIDE 42

ASGI

[....]

slide-43
SLIDE 43

ASGI

ASGI examples

slide-44
SLIDE 44

ASGI

ASGI examples

follows the WSGI environ dictionary

slide-45
SLIDE 45

When can ASGI save the day?

ASGI

slide-46
SLIDE 46

Roadmap

Ready to go

So you’re ready to ship the product into the world...

Onwards into the future

New practices and possible futures

WSGI ASGI Best practices Packing and shipping

First portable solution to connect an app to a server The power of async Django, Docker and Kubernetes Checklist and some nice to haves

slide-47
SLIDE 47

Docker

Web server Django app

slide-48
SLIDE 48

Docker

Web server Django app

slide-49
SLIDE 49

Docker

slide-50
SLIDE 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

Server/Gateway

Django app

slide-51
SLIDE 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

slide-52
SLIDE 52

Docker

slide-53
SLIDE 53

Docker

  • socket
  • module
  • how many workers
  • what to do on exit
  • etc
slide-54
SLIDE 54

Docker

Now usually comes the part where you add another server

  • n top. Or a gateway. Or a load balancer.

Docker container

Django app

the usual choice

slide-55
SLIDE 55

Docker

docker-compose to the rescue

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

slide-56
SLIDE 56

Docker

The next step after that is deploying to some container

  • rchestration tool such as Kubernetes.
  • clustering different containers

together

  • scalable and configurable
  • easier deployment and

management

slide-57
SLIDE 57

Docker

Kubernetes YML example

Source: https://cloud.google.com/python/django/kubernetes-engine

slide-58
SLIDE 58

Roadmap

Ready to go

So you’re ready to ship the product into the world...

Onwards into the future

New practices and possible futures

WSGI ASGI Best practices Packing and shipping

First portable solution to connect an app to a server The power of async Django, Docker and Kubernetes Checklist and some nice to haves

slide-59
SLIDE 59

Some best practices I learned over time

Best practices

(sometimes the hard way)

slide-60
SLIDE 60

Some best practices I learned over time

Best practices

(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.

slide-61
SLIDE 61

Some best practices I learned over time

Best practices

(sometimes the hard way) Use the checklist Monitor

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. Don’t forget to log. And read those logs. Use the tools available.

slide-62
SLIDE 62

Some best practices I learned over time

Best practices

(sometimes the hard way) Be careful of sensitive data

Take care of your users. Use environment variables where possible. Act apprehensive when it comes to security.

Use the checklist Monitor

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. Don’t forget to log. And read those logs. Use the tools available.

slide-63
SLIDE 63

Some best practices I learned over time

Best practices

(sometimes the hard way) Be careful of sensitive data

Take care of your users. Use environment variables where possible. Act apprehensive when it comes to security.

Keep Docker files clean Use the checklist Monitor

The order in which you run commands matters. Don’t give root permissions to the server. 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. Don’t forget to log. And read those logs. Use the tools available.

slide-64
SLIDE 64

Some best practices I learned over time

Best practices

(sometimes the hard way) Be careful of sensitive data

Take care of your users. Use environment variables where possible. Act apprehensive when it comes to security.

Keep Docker files clean Use the checklist Define what you want and stick to it Monitor

The order in which you run commands matters. Don’t give root permissions to the server. 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. You are in control of which tools or patterns you’re going to

  • use. Mix and match. If something doesn’t work, change it.

Don’t forget to log. And read those logs. Use the tools available.

slide-65
SLIDE 65

Roadmap

Ready to go

So you’re ready to ship the product into the world...

Onwards into the future

New practices and possible futures

WSGI ASGI Best practices Packing and shipping

First portable solution to connect an app to a server The power of async Django, Docker and Kubernetes Checklist and some nice to haves

Whoooo!

slide-66
SLIDE 66

Credits

  • slide theme by Slidesgo
  • icons by Flaticon
  • pictures by Unsplash

Thanks

Thanks

Does anyone have any questions? iulyaav iulyaav iulia-avram