Slide 1

Slide 1 text

How to Write Deployment-friendly Applications Hynek Schlawack

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

views.py from pyramid.response import Response from pyramid.view import view_config @view_config(route_name="hello") def hello_world(request): return Response("Hello World!")

Slide 5

Slide 5 text

from pyramid.config import Configurator from .views import hello_world def make_app(): config = Configurator() config.add_route("hello", "/hello") config.scan() return config.make_wsgi_app() app_maker.py

Slide 6

Slide 6 text

from pyramid.config import Configurator from .views import hello_world def make_app(): config = Configurator() config.add_route("hello", "/hello") config.scan() return config.make_wsgi_app() app_maker.py

Slide 7

Slide 7 text

$ gunicorn \ --access-logfile - \ "sample.app_maker:make_app()"

Slide 8

Slide 8 text

$ gunicorn \ --access-logfile - \ "sample.app_maker:make_app()" [2018-04-05 16:28:06 +0200] [54343] [INFO] Starting gunicorn 19.7.1 [2018-04-05 16:28:06 +0200] [54343] [INFO] Listening at: http://127.0.0.1:8000 (54343) [2018-04-05 16:28:06 +0200] [54343] [INFO] Using worker: sync [2018-04-05 16:28:06 +0200] [54346] [INFO] Booting worker with pid: 54346

Slide 9

Slide 9 text

$ gunicorn \ --access-logfile - \ "sample.app_maker:make_app()" $ curl http://127.0.0.1:8000/hello Hello World! [2018-04-05 16:28:06 +0200] [54343] [INFO] Starting gunicorn 19.7.1 [2018-04-05 16:28:06 +0200] [54343] [INFO] Listening at: http://127.0.0.1:8000 (54343) [2018-04-05 16:28:06 +0200] [54343] [INFO] Using worker: sync [2018-04-05 16:28:06 +0200] [54346] [INFO] Booting worker with pid: 54346

Slide 10

Slide 10 text

$ gunicorn \ --access-logfile - \ "sample.app_maker:make_app()" $ curl http://127.0.0.1:8000/hello Hello World! [2018-04-05 16:28:06 +0200] [54343] [INFO] Starting gunicorn 19.7.1 [2018-04-05 16:28:06 +0200] [54343] [INFO] Listening at: http://127.0.0.1:8000 (54343) [2018-04-05 16:28:06 +0200] [54343] [INFO] Using worker: sync [2018-04-05 16:28:06 +0200] [54346] [INFO] Booting worker with pid: 54346 127.0.0.1 - - [05/Apr/2018:16:28:08 +0200] "GET /hello HTTP/1.1" 200 12 "-" "curl/7.54.0"

Slide 11

Slide 11 text

Zoom Out

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

#!/bin/bash exec 2>&1 \ gunicorn \ --access-logfile - \ "sample.app_maker:make_app()" run-app.sh

Slide 14

Slide 14 text

#!/bin/bash exec 2>&1 \ gunicorn \ --access-logfile - \ "sample.app_maker:make_app()" run-app.sh

Slide 15

Slide 15 text

#!/bin/bash exec 2>&1 \ gunicorn \ --access-logfile - \ "sample.app_maker:make_app()" run-app.sh

Slide 16

Slide 16 text

ExecStart=/app/run-app.sh systemd

Slide 17

Slide 17 text

ENTRYPOINT ["/app/run-app.sh"] Dockerfile

Slide 18

Slide 18 text

web: ./run-app.sh Procfile

Slide 19

Slide 19 text

App

Slide 20

Slide 20 text

./run-app.sh App

Slide 21

Slide 21 text

127.0.0.1:8000 Exposes ./run-app.sh App

Slide 22

Slide 22 text

127.0.0.1:8000 Exposes ./run-app.sh Logs stdout App

Slide 23

Slide 23 text

Configuration

Slide 24

Slide 24 text

What Varies?

Slide 25

Slide 25 text

What Varies? Very Little

Slide 26

Slide 26 text

[server] host=127.0.0.1 port=8000 log_level=INFO log_format=human

Slide 27

Slide 27 text

Environment Variables

Slide 28

Slide 28 text

Environment=LOG_FORMAT=human Environment=LOG_LEVEL=INFO ExecStart=/app/run-app.sh systemd

Slide 29

Slide 29 text

ENV LOG_FORMAT=human ENV LOG_LEVEL=INFO ENTRYPOINT ["/app/run-app.sh"] Dockerfile

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

direnv

Slide 32

Slide 32 text

envconsul direnv etcdenv

Slide 33

Slide 33 text

envconsul direnv etcdenv os.environ

Slide 34

Slide 34 text

envconsul direnv etcdenv envsubst os.environ

Slide 35

Slide 35 text

envconsul direnv etcdenv envsubst consul-template confd os.environ

Slide 36

Slide 36 text

$ env HOST=0.0.0.0 PORT=8888 LOG_LEVEL=INFO \ ./run-app.sh [2018-04-09 15:59:29 +0200] [35323] [INFO] Starting gunicorn 19.7.1 [2018-04-09 15:59:29 +0200] [35323] [INFO] Listening at: http://0.0.0.0:8888

Slide 37

Slide 37 text

$ env HOST=0.0.0.0 PORT=8888 LOG_LEVEL=INFO \ ./run-app.sh [2018-04-09 15:59:29 +0200] [35323] [INFO] Starting gunicorn 19.7.1 [2018-04-09 15:59:29 +0200] [35323] [INFO] Listening at: http://0.0.0.0:8888

Slide 38

Slide 38 text

$ env HOST=0.0.0.0 PORT=8888 LOG_LEVEL=INFO \ ./run-app.sh [2018-04-09 15:59:29 +0200] [35323] [INFO] Starting gunicorn 19.7.1 [2018-04-09 15:59:29 +0200] [35323] [INFO] Listening at: http://0.0.0.0:8888

Slide 39

Slide 39 text

$ env HOST=0.0.0.0 PORT=8888 LOG_LEVEL=INFO \ ./run-app.sh [2018-04-09 15:59:29 +0200] [35323] [INFO] Starting gunicorn 19.7.1 [2018-04-09 15:59:29 +0200] [35323] [INFO] Listening at: http://0.0.0.0:8888 logging.basicConfig( level=getattr( logging, os.environ["LOG_LEVEL"], ), format="%(message)s", stream=sys.stdout, )

Slide 40

Slide 40 text

import environ @environ.config(prefix="APP") class AppConfig: @environ.config class Log: level = environ.var() format = environ.var() log = environ.group(Log) config.py

Slide 41

Slide 41 text

APP_LOG_LEVEL → app_cfg.log.level import environ @environ.config(prefix="APP") class AppConfig: @environ.config class Log: level = environ.var() format = environ.var() log = environ.group(Log) config.py

Slide 42

Slide 42 text

import environ from .config import AppConfig from .app_maker import make_app app_cfg = environ.to_config(AppConfig) application = make_app(app_cfg) wsgi.py

Slide 43

Slide 43 text

import environ from .config import AppConfig from .app_maker import make_app app_cfg = environ.to_config(AppConfig) application = make_app(app_cfg) wsgi.py

Slide 44

Slide 44 text

#!/bin/bash exec 2>&1 \ gunicorn \ --access-logfile - \ "sample.app" run-app.sh

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Don’t Put Sensitive Data Into Env Variables

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

HashiCorp Vault AWS Secrets Manager Google Cloud KMS Microsoft Azure Key Vault Docker Secrets

Slide 49

Slide 49 text

HashiCorp Vault AWS Secrets Manager Google Cloud KMS Microsoft Azure Key Vault Docker Secrets

Slide 50

Slide 50 text

Open Space: Secrets Room 10 @ 5pm

Slide 51

Slide 51 text

secrets.py

Slide 52

Slide 52 text

class VaultSecrets: # ... def get_db_url(self): return self.vault.read( f"secret/{self.env}/app-name" )["data"]["db_url"]) secrets.py

Slide 53

Slide 53 text

class FakeSecrets: def get_db_url(self): return ( "postgresql://user@localhost/db" ) secrets.py

Slide 54

Slide 54 text

App

Slide 55

Slide 55 text

env HOST=0.0.0.0 \ PORT=8000 \ LOG_LEVEL=INFO \ ./run-app.sh App

Slide 56

Slide 56 text

env HOST=0.0.0.0 \ PORT=8000 \ LOG_LEVEL=INFO \ ./run-app.sh Secrets App

Slide 57

Slide 57 text

0.0.0.0:8000 Exposes env HOST=0.0.0.0 \ PORT=8000 \ LOG_LEVEL=INFO \ ./run-app.sh Secrets App

Slide 58

Slide 58 text

0.0.0.0:8000 Exposes env HOST=0.0.0.0 \ PORT=8000 \ LOG_LEVEL=INFO \ ./run-app.sh Secrets stdout @ INFO Logs @ INFO App

Slide 59

Slide 59 text

127.0.0.1:8000 Exposes env HOST=127.0.0.1 \ PORT=8000 \ ./run-app.sh App

Slide 60

Slide 60 text

127.0.0.1:8001 Exposes env HOST=127.0.0.1 \ PORT=8001 \ ./run-app.sh App 127.0.0.1:8000 Exposes env HOST=127.0.0.1 \ PORT=8000 \ ./run-app.sh App

Slide 61

Slide 61 text

127.0.0.1:8001 Exposes env HOST=127.0.0.1 \ PORT=8001 \ ./run-app.sh App 127.0.0.1:8000 Exposes env HOST=127.0.0.1 \ PORT=8000 \ ./run-app.sh App $PUBLIC_IP Load Balancer

Slide 62

Slide 62 text

127.0.0.1:8001 Exposes env HOST=127.0.0.1 \ PORT=8001 \ ./run-app.sh App 127.0.0.1:8000 Exposes env HOST=127.0.0.1 \ PORT=8000 \ ./run-app.sh App $PUBLIC_IP Load Balancer Offline

Slide 63

Slide 63 text

SIGTERM

Slide 64

Slide 64 text

127.0.0.1:8001 Exposes env HOST=127.0.0.1 \ PORT=8001 \ ./run-app.sh App 127.0.0.1:8000 Exposes env HOST=127.0.0.1 \ PORT=8000 \ ./run-app.sh App $PUBLIC_IP Load Balancer

Slide 65

Slide 65 text

127.0.0.1:8001 Exposes env HOST=127.0.0.1 \ PORT=8001 \ ./run-app.sh App 127.0.0.1:8000 Exposes env HOST=127.0.0.1 \ PORT=8000 \ ./run-app.sh App $PUBLIC_IP Load Balancer

Slide 66

Slide 66 text

127.0.0.1:8001 Exposes env HOST=127.0.0.1 \ PORT=8001 \ ./run-app.sh App $PUBLIC_IP Load Balancer

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

Readiness

Slide 69

Slide 69 text

Readiness •/healthz

Slide 70

Slide 70 text

Readiness •/healthz •/__heartbeat__

Slide 71

Slide 71 text

Readiness •/healthz •/__heartbeat__ •/-/ready

Slide 72

Slide 72 text

Readiness •/healthz •/__heartbeat__ •/-/ready •/-/readiness

Slide 73

Slide 73 text

HAProxy http-request deny if { path_beg /-/ }

Slide 74

Slide 74 text

Liveness

Slide 75

Slide 75 text

Liveness •/-/healthy

Slide 76

Slide 76 text

Liveness •/-/healthy •/-/liveness

Slide 77

Slide 77 text

Liveness •/-/healthy •/-/liveness •__lbheartbeat__

Slide 78

Slide 78 text

@view_config( route_name="ready", permission=NO_PERMISSION_REQUIRED, ) def ready(request): return Response("yep")

Slide 79

Slide 79 text

@view_config( route_name="ready", permission=NO_PERMISSION_REQUIRED, ) def ready(request): return Response("yep") config.add_route("ready", "/-/ready")

Slide 80

Slide 80 text

Moar

Slide 81

Slide 81 text

Moar •__version__

Slide 82

Slide 82 text

Moar •__version__ •/-/metrics

Slide 83

Slide 83 text

Moar •__version__ •/-/metrics •/-/log-level

Slide 84

Slide 84 text

127.0.0.1:8001 Exposes env HOST=127.0.0.1 \ PORT=8001 \ ./run-app.sh App 127.0.0.1:8000 Exposes env HOST=127.0.0.1 \ PORT=8000 \ ./run-app.sh App $PUBLIC_IP Load Balancer

Slide 85

Slide 85 text

App 127.0.0.1:8003 Exposes env HOST=127.0.0.1 \ PORT=8002 \ ./run-app.sh 127.0.0.1:8002 Exposes env HOST=127.0.0.1 \ PORT=8002 \ ./run-app.sh App 127.0.0.1:8001 Exposes env HOST=127.0.0.1 \ PORT=8001 \ ./run-app.sh App 127.0.0.1:8000 Exposes env HOST=127.0.0.1 \ PORT=8000 \ ./run-app.sh App $PUBLIC_IP Load Balancer

Slide 86

Slide 86 text

App 10.0.0.4:8000 Exposes env HOST=10.0.0.4 \ PORT=8000 \ ./run-app.sh 10.0.0.3:8000 Exposes env HOST=10.0.0.3 \ PORT=8000 \ ./run-app.sh App 10.0.0.2:8000 Exposes env HOST=10.0.0.2 \ PORT=8000 \ ./run-app.sh App 10.0.0.1:8000 Exposes env HOST=10.0.0.1 \ PORT=8000 \ ./run-app.sh App $PUBLIC_IP Load Balancer

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

App 10.0.0.4:8000 Exposes env HOST=10.0.0.4 \ PORT=8000 \ ./run-app.sh 10.0.0.3:8000 Exposes env HOST=10.0.0.3 \ PORT=8000 \ ./run-app.sh App 10.0.0.2:8000 Exposes env HOST=10.0.0.2 \ PORT=8000 \ ./run-app.sh App 10.0.0.1:8000 Exposes env HOST=10.0.0.1 \ PORT=8000 \ ./run-app.sh App $PUBLIC_IP Load Balancer

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

Open Space: Docker Sunday Room 11 @ 11am

Slide 95

Slide 95 text

App

Slide 96

Slide 96 text

run-app.sh App

Slide 97

Slide 97 text

run-app.sh env HOST=0.0.0.0 PORT=8000 … App

Slide 98

Slide 98 text

run-app.sh SIGTERM env HOST=0.0.0.0 PORT=8000 … App

Slide 99

Slide 99 text

Secrets run-app.sh SIGTERM env HOST=0.0.0.0 PORT=8000 … App

Slide 100

Slide 100 text

Secrets run-app.sh SIGTERM External Resources Keeps data in env HOST=0.0.0.0 PORT=8000 … App

Slide 101

Slide 101 text

Secrets Exposes service 0.0.0.0:8000 run-app.sh SIGTERM External Resources Keeps data in env HOST=0.0.0.0 PORT=8000 … App

Slide 102

Slide 102 text

Secrets Exposes service 0.0.0.0:8000 run-app.sh Exposes state 0.0.0.0:8000/-/* SIGTERM External Resources Keeps data in env HOST=0.0.0.0 PORT=8000 … App

Slide 103

Slide 103 text

Logs stdout Secrets Exposes service 0.0.0.0:8000 run-app.sh Exposes state 0.0.0.0:8000/-/* SIGTERM External Resources Keeps data in env HOST=0.0.0.0 PORT=8000 … App

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

•Loose Coupling

Slide 106

Slide 106 text

•Loose Coupling •Separate I/O & Logic

Slide 107

Slide 107 text

•Loose Coupling •Separate I/O & Logic •Avoid Global State

Slide 108

Slide 108 text

App Boundary = Just Another Boundary

Slide 109

Slide 109 text

Epilogue

Slide 110

Slide 110 text

Epilogue

Slide 111

Slide 111 text

ox.cx/df @hynek vrmd.de