Upgrade to Pro — share decks privately, control downloads, hide ads and more …

12-Factor Rails App and Kubernetes

12-Factor Rails App and Kubernetes

The Twelve-Factor App is a guideline for modern web app development. Kubernetes is an open-source project for container management that gives you great flexibility around application deployments. With containers and Kubernetes, you can deploy and rollback a version, scale out to hundreds of nodes, copy the same configuration to multiple prod, staging, and dev environments, manage deployments through API, and so on.

This talk will review each of the twelve factors using a simple Rails application as an example and see how Kubernetes helps you run your Rails application. Experience with containers is not required.

Emma Haruka Iwao

April 27, 2018

More Decks by Emma Haruka Iwao

Other Decks in Technology


  1. Who Am I? Emma Haruka Iwao / 岩尾 艾玛 (@Yuryu)

    Developer Advocate for Google Cloud Platform Live in Tokyo (30%) Love travels, games, delicious food
  2. 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
  3. 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.
  4. 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
  5. Container Container instance Set of actual processes Application processes, middleware,

    etc Docker is often used, other engines supported Container
  6. Pod Basic manageable unit in Kubernetes Holds network addresses and

    storage Provides running context to containers Can contain one or more containers Pod
  7. What is the 12-factor App? The best practices of web

    applications, proposed by Heroku Proven and some common sense principles
  8. 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 (管理进程)
  9. 12 Factors - obvious ones 1. Codebase 基准代码 Use git

    or anything to manage revisions! 2. Dependencies 依赖 No implicit dependencies Use Gemfile to manage dependencies
  10. 12 Factors - As a Rails app Some of the

    factors are satisfied by Rails itself 4. Backing Services 后端服务 6. Processes 进程 7. Port binding 端口绑定
  11. 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
  12. 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
  13. 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.
  14. 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 (管理进程)
  15. 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
  16. 3. Config - Secrets Secrets should be clearly separately managed

    (Do not commit). Kubernetes supports secret management. API CLI secrets
  17. 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
  18. 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.
  19. 9. Disposability 易处理 "Maximize robustness with fast startup and graceful

    shutdown" Containers are processes. Fast and lightweight. Kubernetes watches container lifecycle. Respawn when they die.
  20. 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
  21. Three environments to run Kubernetes • Minikube - Single node

    version • Independent cluster - DIY Kubernetes custer • Google Kubernetes Engine (and others) - Managed Kubernetes
  22. Minikube Single node Kubernetes Best for development and local testing

    $ brew install cask minikube Runs on Linux, Windows and macOS
  23. Independent cluster • DIY Kubernetes Cluster • Full features and

    control, using the latest version • Manual setup and maintenance
  24. 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
  25. 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
  26. 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
  27. Logging Agent to Forward Logs Rails Node Pod Logging Agent

    Pod logfile (fluentd, stackdriver, etc) stdout
  28. 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.
  29. 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 (管理进程)
  30. 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