Slide 1

Slide 1 text

12-Factor Rails App and Kubernetes Emma Haruka Iwao (@Yuryu)

Slide 2

Slide 2 text

Who Am I? Emma Haruka Iwao / 岩尾 艾玛 (@Yuryu) Developer Advocate for Google Cloud Platform Live in Tokyo (30%) Love travels, games, delicious food

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Containers Virtual machine Kernel Dependencies Application Code Hardware + hypervisor Dedicated server Kernel Dependencies Application Code Hardware Container Kernel + Container Runtime Dependencies Application Code Hardware Deployment ~mins (sec) Portable Very Efficient Deployment ~weeks Not portable Low utilization Deployment ~mins Hypervisor specific Low isolation, Tied to OS

Slide 5

Slide 5 text

Why Containers? Fast Simple and Fast compared to VMs. Can be started in just a few milliseconds. Portable Can be run in many environments. Efficiency Low overhead. Resources use by containers can be limited.

Slide 6

Slide 6 text

Containerizing an Rails App Pretty straightforward. Step by step installation.

Slide 7

Slide 7 text

Image by Connie Zhou

Slide 8

Slide 8 text

http://research.google.com/pubs/pub43438.html

Slide 9

Slide 9 text

Kubernetes κυβερνήτης: Greek for “pilot” or “helmsman of a ship” the open source cluster manager from Google

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

g.co/dev/gkecomic

Slide 12

Slide 12 text

Inside a Kubernetes deployment … Node Pod Manage Container

Slide 13

Slide 13 text

Node Pool of computation resources Logical machines to run containers Can be physical or virtual machines Can run one or more containers Can be added or removed while running Node

Slide 14

Slide 14 text

Container Container instance Set of actual processes Application processes, middleware, etc Docker is often used, other engines supported Container

Slide 15

Slide 15 text

Pod Basic manageable unit in Kubernetes Holds network addresses and storage Provides running context to containers Can contain one or more containers Pod

Slide 16

Slide 16 text

Inside a Kubernetes deployment … Node Pod Manage Container

Slide 17

Slide 17 text

Inside a deployment … Manage Load Balancer Pod Pod Pod User

Slide 18

Slide 18 text

Reviewing 12-factor App

Slide 19

Slide 19 text

What is the 12-factor App? The best practices of web applications, proposed by Heroku Proven and some common sense principles

Slide 20

Slide 20 text

The 12 factors 1. Codebase (基准代码) 2. Dependencies (依赖) 3. Config (配置) 4. Backing services (后端服务) 5. Build, release, run (构建,发布,运行) 6. Processes (进程) 7. Port binding (端口绑定) 8. Concurrency (并发) 9. Disposability (易处理) 10. Dev/prod parity (开发环境与线上环境等价) 11. Logs (日志) 12. Admin processes (管理进程)

Slide 21

Slide 21 text

12 Factors - obvious ones 1. Codebase 基准代码 Use git or anything to manage revisions! 2. Dependencies 依赖 No implicit dependencies Use Gemfile to manage dependencies

Slide 22

Slide 22 text

12 Factors - As a Rails app Some of the factors are satisfied by Rails itself 4. Backing Services 后端服务 6. Processes 进程 7. Port binding 端口绑定

Slide 23

Slide 23 text

4. Backing Services 后端服务 Backend services are attachable and independent from the code. You can switch one DB to another without editing code. e.g. ActiveRecord and database.yml

Slide 24

Slide 24 text

6. Processes 进程 "Twelve-factor processes are stateless and share-nothing. Any data that needs to persist must be stored in a stateful backing service, typically a database." This is exactly what a Rails app looks like. $ rails server

Slide 25

Slide 25 text

7. Port binding 端口绑定 "The twelve-factor app is completely self-contained [… and] exports HTTP as a service by binding to a port" Yes! A Rails app offers a service via http.

Slide 26

Slide 26 text

The 12 factors 1. Codebase (基准代码) 2. Dependencies (依赖) 3. Config (配置) 4. Backing services (后端服务) 5. Build, release, run (构建,发布,运行) 6. Processes (进程) 7. Port binding (端口绑定) 8. Concurrency (并发) 9. Disposability (易处理) 10. Dev/prod parity (开发环境与线上环境等价) 11. Logs (日志) 12. Admin processes (管理进程)

Slide 27

Slide 27 text

3. Config 配置 "The twelve-factor app stores config in environment variables [...]." Config should be separate from code and easily modifiable. Kubernetes supports cluster-wide config variables! Simply write in a YAML file. spec: containers: - env: - name: VARIABLE1 value: test1

Slide 28

Slide 28 text

3. Config - Secrets Secrets should be clearly separately managed (Do not commit). Kubernetes supports secret management. API CLI secrets

Slide 29

Slide 29 text

5. Build, release, run 构建,发布,运行 Code to release should be one way. No production code change Releases should be versioned Kubernetes supports rolling releases and rollback

Slide 30

Slide 30 text

8. Concurrency 并发 Scale out! (Fully exploit the shared-nothing architecture) Use multiple machines. Scale out by spawning more processes. Scaling out with Kubernetes is easy, just specify number of processes Kubernetes handles the rest.

Slide 31

Slide 31 text

8. Concurrency - Scaling replica=3 Load Balancer Pod Pod Pod User

Slide 32

Slide 32 text

8. Concurrency - Scaling Load Balancer Pod Pod Pod User Pod replica=3

Slide 33

Slide 33 text

9. Disposability 易处理 "Maximize robustness with fast startup and graceful shutdown" Containers are processes. Fast and lightweight. Kubernetes watches container lifecycle. Respawn when they die.

Slide 34

Slide 34 text

10. Dev/prod parity 开发环境与线上环境等价 "Keep development, staging, and production as similar as possible" Kubernetes has multiple environments for different uses Use the same configuration for dev, staging, prod All of the configurations are in YAML files

Slide 35

Slide 35 text

Three environments to run Kubernetes ● Minikube - Single node version ● Independent cluster - DIY Kubernetes custer ● Google Kubernetes Engine (and others) - Managed Kubernetes

Slide 36

Slide 36 text

Minikube Single node Kubernetes Best for development and local testing $ brew install cask minikube Runs on Linux, Windows and macOS

Slide 37

Slide 37 text

Independent cluster ● DIY Kubernetes Cluster ● Full features and control, using the latest version ● Manual setup and maintenance

Slide 38

Slide 38 text

Google Kubernetes Engine, or GKE Managed version of Kubernetes in Cloud No additional cost Supports auto-update, failover, etc Works with other GCP components like load balancers

Slide 39

Slide 39 text

From Laptop to Datacenter Kubernetes runs on your laptop, servers, and in Cloud Build your apps locally, lift-and-shift into Cloud when you are ready Don’t get stuck with a platform that doesn’t work for you

Slide 40

Slide 40 text

11. Logs 日志 "A twelve-factor app never concerns itself with routing or storage of its output stream. [...] Instead, each running process writes its event stream, unbuffered, to stdout." Rails supports logging to stdout stdout / stderr are handled by Docker

Slide 41

Slide 41 text

Logging Agent to Forward Logs Rails Node Pod Logging Agent Pod logfile (fluentd, stackdriver, etc) stdout

Slide 42

Slide 42 text

12. Admin processes 管理进程 "Run admin/management tasks as one-off processes" in the same context as production applications. e.g. rails db:migrate You can run a process in a pod, or create a Job. Cron is also supported.

Slide 43

Slide 43 text

The 12 factors 1. Codebase (基准代码) 2. Dependencies (依赖) 3. Config (配置) 4. Backing services (后端服务) 5. Build, release, run (构建,发布,运行) 6. Processes (进程) 7. Port binding (端口绑定) 8. Concurrency (并发) 9. Disposability (易处理) 10. Dev/prod parity (开发环境与线上环境等价) 11. Logs (日志) 12. Admin processes (管理进程)

Slide 44

Slide 44 text

Kubernetes is a good friend of Rails Kubernetes runs Rails Applications! It respects the 12 factors The 12-factor App is not hard Try it today on your laptop The 12-factor App: Copyright (c) 2012 Adam Wiggins Ruby on Rails: Copyright (c) 2005-2018 David Heinemeier Hansson