SlideShare a Scribd company logo
Deploying and Scaling a Rails
Application with Docker,
Consul and Friends
Kieran Johnson
@kieranj
https://www.invisiblelines.com
build, ship, and run any application,
anywhere
Docker
Resource/Network Isolation
Lightweight
Provides Consistency
Simplifies Distribution
Run Software Anywhere
A Dockerfile for Rails
FROM invisiblelines/ruby:2.2.0
MAINTAINER Kieran Johnson "kieran@invisiblelines.com"
ENV PORT 5000
RUN curl -sL https://deb.nodesource.com/setup | sudo bash - && 
apt-get -qqy install nodejs -y; 
apt-get clean -y; 
apt-get autoremove -y
RUN apt-get -qq update;
apt-get -qqy install libpq-dev; 
apt-get clean -y; 
apt-get autoremove -y
Build and Run
Build
$ docker build -t myapp .
Run
$ docker run -d -p 5000:5000 myapp
A Simple Rails Stack
3 containers on one node
App
Postgres
Nginx
Setup the Containers
Start a database container
$ docker run -d 
--name db 
postgres:9.4.0
Setup the Containers
Run our app and link it to the
database container
$ docker run -d 
--name myapp 
--link db:db 
-e DB_ENV_POSTGRES_USER=postgres 
-e DB_ENV_POSTGRES_PASSWORD=postgres 
myapp
Setup the Containers
Add Nginx
$ docker run -d 
-p 80:80 
--name nginx 
--link myapp:myapp 
-v /Users/kieran/Desktop/docker-fig-rails-example/nginx.conf:/etc/nginx/conf.d/default.conf 
nginx
Issues
Links don't (yet) work across nodes
Scaling services requires manually starting
containers
Configuration files need updating manually
How To Scale?
Docker Compose
+
Docker Swarm
+
Consul
+
Registrator
A Dynamic and Scalable
Docker Cluster
Docker Compose
https://github.com/docker/compose
Docker Compose
Define and run multi­container applications that
can be run with a single command
Simple YAML file
Supports most Docker options
Can scale containers
Docker Compose
Sample docker­compose.yml
postgres:
image: postgres:9.4.0
environment:
POSTGRES_USER: 'docker'
POSTGRES_PASSWORD: 'mysupersecretpassword'
app:
build: .
ports:
- "5000:5000"
links:
- db
environment:
RACK_ENV: production
DATABASE_URL: postgres://docker:mysupersecretpassword@postgres/docker
Docker Compose
Start Application Stack
$ docker-compose up -d
Scale a Service
$ docker-compose scale app=6
Docker Swarm
https://github.com/docker/swarm
Docker Swarm
Schedule containers on multiple Docker engines
from a single point
Add filters/constraints to define which containers
run on which nodes
Docker Swarm
Each node runs a Swarm agent pointing to a discovery service
$ docker run -d 
swarm join --addr={{node_ip}}:2375 
consul://consul.service.consul:8500/swarm
Start a single Swarm manager with the same discovery service
$ docker run -d 
-p {{swarm_port}}:2375 
swarm manage consul://consul.service.consul:8500/swarm
Docker Swarm
Interactions with Swarm are done through a the Swarm manager
$ docker -H tcp://{{swarm_ip}}:{{swarm_port}} info
...
Consul
https://www.consul.io
Consul
Service Discovery
Health Checking
Key/Value Store
Consul - Service Discovery
Register service with consul
$ curl -X PUT 
-d "{"ID": "app001", "Name": "app", "Tags": [], "Port": 5000}" 
0.0.0.0:8500/v1/agent/services/register
List of services can now be queried via API
$ curl 0.0.0.0:8500/v1/catalog/services
{"app": [], "consul": []}
Consul - Service Discovery
A single service definition via API
$ curl 0.0.0.0:8500/v1/catalog/service/app
[
{
"ServicePort": 49153,
"ServiceAddress": "",
"ServiceTags": null,
"ServiceName": "app",
"ServiceID": "swarm-0:dockeransible_app_1:5000",
"Address": "10.129.127.240",
"Node": "swarm-0"
}
]
Consul - DNS
Service can now be also queried by DNS*
$ dig A app.service.consul
; <<>> DiG 9.8.3-P1 <<>> A app.service.consul
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 55035
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;app.service.consul. IN A
;; ANSWER SECTION:
app.service.consul. 300 IN A 192.168.0.12
*Consul DNS runs on a non­standard port so install DNSmasq to query this by default.
Consul - Health Checks
Health checks monitor service
Remove a service from DNS queries if it fails a
health check
Consul - Key/Value Store
Store application configuration data in the cluster
Store a value
$ curl -X PUT -d "bar" 0.0.0.0:8500/v1/kv/foo
Retrieve a value
$ curl 0.0.0.0:8500/v1/kv/foo?raw
Consul - Watches
Watch a view of data and run a handler when the data changes
i.e. when services change, or when the value of a key is changed
Consul - Watches
Using a watch we can perform a deployment across the cluster
$ consul watch -type key -key app/sha /usr/local/bin/deploy.sh
On Swarm Manager node add a watch for the key
When this changes run a script
In the script loop over the app containers stopping
them one by one and starting the new container
Configuring Services
Manually updating Consul with service
definitions in Consul isn't always ideal
Thankfully it can automated
Registrator
https://github.com/gliderlabs/registrator
Registrator
Runs as a container
Works with multiple service registry backends
including Consul
Provides sane defaults for registered services
Customisable using environment variables when
starting containers
Dynamic Configuration
With services now running anywhere in a cluster we need to
update some configuration files as the application scales, 
e.g nginx upstream
Consul Template
Update templates with values from Consul when
any variable in the template changes
Optionally run a command when the template has
been generated
$ consul-template -consul 0.0.0.0:8500 
-template "/tmp/template.ctmpl:/var/www/nginx.conf:docker exec nginx /usr/sbin/nginx -s reload"
Consul Template
Templates use Go templates
{{range service "webapp@datacenter"}}
server {{.Address}}:{{.Port}}{{end}}
All Together Now
Run Consul, Swarm and Registrator on each node in
the cluster
Swarm manager runs on one node
Docker Compose runs the predefined containers
through the Swarm manager which handler
scheduling
Registrator updates Consul with services as they
become available
Containers query Consul for other services
Consul Template responds to changes in Consul
and updates configuration files
Demo
Demo Notes
Infrastruction provisioned using Terraform (also
from Hashicorp)
Each node is bootstrapped using Ansible to setup
Consul/Swarm, DNSmasq and add
watches/templates
The End

More Related Content

PDF
Docker Online Meetup #28: Production-Ready Docker Swarm
PPTX
Docker cluster with swarm, consul, registrator and consul-template
PDF
Docker Swarm 0.2.0
PDF
Consul and docker swarm cluster
PDF
Docker swarm introduction
PPTX
Docker Swarm Introduction
PDF
Going Production with Docker and Swarm
PDF
Deep Dive into Docker Swarm Mode
Docker Online Meetup #28: Production-Ready Docker Swarm
Docker cluster with swarm, consul, registrator and consul-template
Docker Swarm 0.2.0
Consul and docker swarm cluster
Docker swarm introduction
Docker Swarm Introduction
Going Production with Docker and Swarm
Deep Dive into Docker Swarm Mode

What's hot (20)

PDF
Nebulaworks Docker Overview 09-22-2015
PPTX
Swarm - A Docker Clustering System
PPTX
Introduction to docker swarm
PDF
The age of orchestration: from Docker basics to cluster management
PDF
Docker swarm-mike-goelzer-mv-meetup-45min-workshop 02242016 (1)
PPTX
Microservices with docker swarm and consul
PDF
What's New in Docker 1.12?
PPTX
Docker Machine & Docker Swarm
PPTX
Docker Swarm for Beginner
PPTX
Container Orchestration with Docker Swarm
PDF
PPTX
Load Balancing Apps in Docker Swarm with NGINX
PDF
Clustering with Docker Swarm - Dockerops 2016 @ Cento (FE) Italy
PDF
Service Discovery & Load-Balancing under Docker 1.12.0 @ Docker Meetup #22
PDF
DockerDay2015: Docker orchestration for sysadmin
PPTX
CoreOS Overview and Current Status
PDF
Docker Swarm 45-min Workshop (Mountain View Docker Meetup 2/24/2016)
PDF
Docker serverless v1.0
PPTX
Docker swarm workshop
PPTX
Dockerizing Windows Server Applications by Ender Barillas and Taylor Brown
Nebulaworks Docker Overview 09-22-2015
Swarm - A Docker Clustering System
Introduction to docker swarm
The age of orchestration: from Docker basics to cluster management
Docker swarm-mike-goelzer-mv-meetup-45min-workshop 02242016 (1)
Microservices with docker swarm and consul
What's New in Docker 1.12?
Docker Machine & Docker Swarm
Docker Swarm for Beginner
Container Orchestration with Docker Swarm
Load Balancing Apps in Docker Swarm with NGINX
Clustering with Docker Swarm - Dockerops 2016 @ Cento (FE) Italy
Service Discovery & Load-Balancing under Docker 1.12.0 @ Docker Meetup #22
DockerDay2015: Docker orchestration for sysadmin
CoreOS Overview and Current Status
Docker Swarm 45-min Workshop (Mountain View Docker Meetup 2/24/2016)
Docker serverless v1.0
Docker swarm workshop
Dockerizing Windows Server Applications by Ender Barillas and Taylor Brown
Ad

Viewers also liked (20)

PPTX
The missing piece : when Docker networking and services finally unleashes so...
PDF
Continuous Deployment with Bamboo and Deployit
PPTX
Ideal Deployment In .NET World
PDF
OPEX Week Europe 140316
PDF
Jenkins vs GitLab CI
PDF
Using GitLab CI
PDF
PostgreSQL 9.6 Performance-Scalability Improvements
PDF
Docker Datacenter - CaaS
PDF
Chickens & Eggs: Managing secrets in AWS with Hashicorp Vault
PDF
Octo talk : docker multi-host networking
PPTX
Docker Networking & Swarm Mode Introduction
PPTX
Introction to docker swarm
PDF
Elassandra: Elasticsearch as a Cassandra Secondary Index (Rémi Trouville, Vin...
PPTX
Desenvolvendo para WordPress com Docker, Git e WP-CLI
PDF
大規模ソーシャルゲーム開発から学んだPHP&MySQL実践テクニック
PDF
Docker Swarm: Docker Native Clustering
PDF
大規模ソーシャルゲームを支える技術~PHP+MySQLを使った高負荷対策~
PPTX
Docker Datacenter Overview and Production Setup Slides
PDF
Infinit: Modern Storage Platform for Container Environments
PDF
Docker Online Meetup: Infrakit update and Q&A
The missing piece : when Docker networking and services finally unleashes so...
Continuous Deployment with Bamboo and Deployit
Ideal Deployment In .NET World
OPEX Week Europe 140316
Jenkins vs GitLab CI
Using GitLab CI
PostgreSQL 9.6 Performance-Scalability Improvements
Docker Datacenter - CaaS
Chickens & Eggs: Managing secrets in AWS with Hashicorp Vault
Octo talk : docker multi-host networking
Docker Networking & Swarm Mode Introduction
Introction to docker swarm
Elassandra: Elasticsearch as a Cassandra Secondary Index (Rémi Trouville, Vin...
Desenvolvendo para WordPress com Docker, Git e WP-CLI
大規模ソーシャルゲーム開発から学んだPHP&MySQL実践テクニック
Docker Swarm: Docker Native Clustering
大規模ソーシャルゲームを支える技術~PHP+MySQLを使った高負荷対策~
Docker Datacenter Overview and Production Setup Slides
Infinit: Modern Storage Platform for Container Environments
Docker Online Meetup: Infrakit update and Q&A
Ad

Similar to Deploying and Scaling a Rails Application with Docker and Friends (20)

PDF
Microservices blue-green-deployment-with-docker
PDF
Workshop Consul .- Service Discovery & Failure Detection
PDF
Higher order infrastructure: from Docker basics to cluster management - Nicol...
PPTX
Running Docker in Development & Production (#ndcoslo 2015)
PPTX
Docker Container As A Service - March 2016
PPTX
Containers as a Service with Docker
PDF
Cloud-native applications with Java and Kubernetes - Yehor Volkov
PPTX
Amazon Web Services and Docker: from developing to production
PPTX
Running Docker in Development & Production (DevSum 2015)
PPTX
Docker Security workshop slides
PDF
Docker for Ruby Developers
PPTX
PPTX
Docker Container As A Service - Mix-IT 2016
PPTX
Simple docker hosting in FIWARE Lab
PDF
Docker 進階實務班
PDF
廣宣學堂: 容器進階實務 - Docker進深研究班
PDF
JDO 2019: Tips and Tricks from Docker Captain - Łukasz Lach
PDF
Introduction to Cloud Foundry #JJUG
PPTX
Architecting .NET Applications for Docker and Container Based Deployments
PDF
Docker Swarm 1.12 Overview and Demo
Microservices blue-green-deployment-with-docker
Workshop Consul .- Service Discovery & Failure Detection
Higher order infrastructure: from Docker basics to cluster management - Nicol...
Running Docker in Development & Production (#ndcoslo 2015)
Docker Container As A Service - March 2016
Containers as a Service with Docker
Cloud-native applications with Java and Kubernetes - Yehor Volkov
Amazon Web Services and Docker: from developing to production
Running Docker in Development & Production (DevSum 2015)
Docker Security workshop slides
Docker for Ruby Developers
Docker Container As A Service - Mix-IT 2016
Simple docker hosting in FIWARE Lab
Docker 進階實務班
廣宣學堂: 容器進階實務 - Docker進深研究班
JDO 2019: Tips and Tricks from Docker Captain - Łukasz Lach
Introduction to Cloud Foundry #JJUG
Architecting .NET Applications for Docker and Container Based Deployments
Docker Swarm 1.12 Overview and Demo

Recently uploaded (20)

PDF
Decoding a Decade: 10 Years of Applied CTI Discipline
PPTX
presentation_pfe-universite-molay-seltan.pptx
PDF
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PPTX
Funds Management Learning Material for Beg
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
DOCX
Unit-3 cyber security network security of internet system
PPT
tcp ip networks nd ip layering assotred slides
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PPTX
innovation process that make everything different.pptx
PPTX
Introduction to Information and Communication Technology
PDF
Testing WebRTC applications at scale.pdf
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
PPTX
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
PPTX
international classification of diseases ICD-10 review PPT.pptx
Decoding a Decade: 10 Years of Applied CTI Discipline
presentation_pfe-universite-molay-seltan.pptx
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
SASE Traffic Flow - ZTNA Connector-1.pdf
Funds Management Learning Material for Beg
PptxGenJS_Demo_Chart_20250317130215833.pptx
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
Unit-3 cyber security network security of internet system
tcp ip networks nd ip layering assotred slides
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
INTERNET------BASICS-------UPDATED PPT PRESENTATION
Design_with_Watersergyerge45hrbgre4top (1).ppt
innovation process that make everything different.pptx
Introduction to Information and Communication Technology
Testing WebRTC applications at scale.pdf
Slides PDF The World Game (s) Eco Economic Epochs.pdf
Module 1 - Cyber Law and Ethics 101.pptx
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
international classification of diseases ICD-10 review PPT.pptx

Deploying and Scaling a Rails Application with Docker and Friends