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

Docker Workshop - Microservices

ProdOps
January 07, 2015

Docker Workshop - Microservices

Docker Workshop about Microservices
http://www.meetup.com/Docker-Tel-Aviv/events/219112173/

This workshop covers micro-service architecture. Explains micro-services architecture and create several micro-services running in Docker containers. Leave with the skills and confidence to create micro-services architectures and deploy these using Docker.

ProdOps

January 07, 2015
Tweet

More Decks by ProdOps

Other Decks in Technology

Transcript

  1. agenda ◦ intro to microservices ◦ architecture: monoliths vs. microservices

    ◦ START UP SOME SERVICES! ◦ MSA best practices
  2. > git clone https://gist.github.com/dfece549c22ddd3eccad workshop > cd workshop > vagrant

    up && vagrant ssh > for image in registry pragrium/consul pragrium/registrator \ golang python ruby scratch; do docker pull $image done bootstrap cheat sheet dvps.me/docker-ws-2-cheat
  3. microservices Loosely coupled service oriented architecture with bounded contexts not

    updated at the same time www.slideshare.net/adriancockcroft/dockercon-state-of-the-art-in-microservices minimal knowledge of own environment
  4. why microservices • smaller code easier to change/understand • faster

    start time / speedier deployments • decoupling of concerns [SOLID] • independent scaling • better fit with infrastructure (mem/cpu) • scaling development teams • fault isolation • flexibility in choosing a technology stack ◦ ↵
  5. why NOT microservices • developing distributed system complexity • implement

    inter-process communication • transactions are more difficult to track • integration automated tests a challenge • operational complexity - multiple moving parts • coordination between teams about dependencies
  6. MySQL PostgreSQL MongoDB Oracle MS-SQL “nosql” database async worker RabbitMQ

    ActiveMQ Kafka Memcached Redis APC Ruby on Rails Django Spring ASP.NET PHP monolithic application presentation business logic data access request / response relational database cache queue scheduler microservices.io/patterns/monolithic.html
  7. components of Ruby on Rails $ rails new myapp create

    config.ru create Gemfile create app/assets/javascripts/application.js create app/assets/stylesheets/application.css create app/controllers/application_controller.rb create app/views/layouts/application.html.erb create config/routes.rb create config/application.rb create config/environment.rb create config/secrets.yml create config/initializers/assets.rb create config/initializers/backtrace_silencers.rb create config/initializers/cookies_serializer.rb create config/initializers/filter_parameter_logging.rb create config/initializers/inflections.rb create config/initializers/mime_types.rb create config/initializers/session_store.rb create config/initializers/wrap_parameters.rb create config/locales/en.yml create config/boot.rb create config/database.yml create db/seeds.rb create log create public/404.html create public/422.html create public/500.html create public/robots.txt create tmp/cache create tmp/cache/assets create vendor/assets/javascripts create vendor/assets/stylesheets
  8. monitoring configuration router layout data access queue cache scheduler micro

    services infrastructure services authentication authorization form handling file uploads web scraping indexing searching application services e-mail pdf generation payments workflow engine pagination load balancer
  9. microservice types ◦ sync, in-path request/response ◦ async “enrich” workers

    ◦ databases ◦ event handlers ◦ encapsulate private database ◦ sharing a database
  10. service discovery ◦ service registration ◦ service lookup / discovery

    Zookeeper • Doozer • Etcd Eureka • Serf • DNS • SkyDNS • Consul Redis • Database (don’t do this) jasonwilder.com/blog/2014/02/04/service-discovery-in-the-cloud/
  11. dvps.me/docker-ws-2-cheat Vagrantfile for Consul UI Vagrant.configure("2") do |v| v.vm.box =

    "yungsang/boot2docker" v.vm.box_version = ">= 1.3.7" v.vm.network :forwarded_port, guest: 5000, host: 60050 v.vm.network :forwarded_port, guest: 8500, host: 60085 v.vm.network :forwarded_port, guest: 8080, host: 60080 $ vagrant reload
  12. docker dns $ sudo vi /var/lib/boot2docker/profile EXTRA_ARGS="\ --dns 172.17.42.1 \

    --dns 8.8.8.8 \ --dns-search service.consul \ " $ sudo /etc/init.d/docker stop $ sudo /etc/init.d/docker start dvps.me/docker-ws-2-cheat
  13. consul docker run -d \ -p 8400:8400 \ -p 8500:8500

    \ -p 8600:53/udp \ -h node1 --name consul \ progrium/consul \ -server -bootstrap \ -advertise 172.17.0.1 dvps.me/docker-ws-2-cheat
  14. registrator docker run -d \ -e DOCKER_HOST=tcp://172.17.42.1:2375 \ -h registrator

    --name registrator \ progrium/registrator \ consul://172.17.42.1:8500 progrium.com/blog/2014/09/10/automatic-docker-service-announcement-with-registrator/ dvps.me/docker-ws-2-cheat
  15. service using registrator docker run -ti --rm \ -p 9000:9000

    \ -e SERVICE_NAME="webserver" \ -e SERVICE_TAGS="master" \ -h web1 --name webserver \ busybox \ /bin/sh dvps.me/docker-ws-2-cheat test here - http://localhost:60085/ui/
  16. REST API • HTTP Transport • HTTP Verbs • Resources

    • Versioning This workshop is NOT about REST APIs
  17. request types • user request endpoints ◦ with layout ◦

    api only [mobile, etc…] • inner-service • webhook callbacks
  18. ruby rack web application run Proc.new { |env| [ 200,

    {'Content-Type' => 'text/html'}, [ "Hello world!" ] ] } gem install rack && rackup app.rb dvps.me/docker-ws-2-cheat
  19. python web application def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return

    ["Hello World!"] from wsgiref.simple_server import make_server httpd = make_server('', 8080, application) httpd.serve_forever() docker run -ti python /bin/bash cat > webserver.py # copy cheatsheet dvps.me/docker-ws-2-cheat
  20. go web application package main import ( "fmt" "net/http" )

    func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello world!") } func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil) } dvps.me/docker-ws-2-cheat
  21. python + redis hit counter from redis import Redis def

    application(env, start_response): redis = Redis(host='redis', port=6379) redis.incr('hits') # increment start_response('200 OK', [('Content-Type','text/html')]) return ['Hello World! I have been seen %s times.' % redis.get('hits')] if __name__ == "__main__": from wsgiref.simple_server import make_server httpd = make_server('', 8080, application) httpd.serve_forever() dvps.me/docker-ws-2-cheat
  22. composing ◦ ajax ◦ iframes ◦ server side includes (Apache/nginx)

    ◦ edge side includes (Akamai/Varnish) what about json api includes? dejanglozic.com/2014/10/20/micro-services-and-page-composition-problem/
  23. go ssi - ssi.go func ssi(url string) template.HTML { resp,

    err := http.Get(url) defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) return template.HTML(body) } func ssiHandler(w http.ResponseWriter, r *http.Request) { funcMap := template.FuncMap { "ssi": ssi, } tmpl, err := template.New("root").Funcs(funcMap).ParseGlob("*.html") err = tmpl.ExecuteTemplate(w, "combiner.html", "") } dvps.me/docker-ws-2-cheat
  24. go ssi - combiner.html <!doctype html> <html> <body> <div style="width:50%;float:left">

    {{ ssi "http://plus.google.com" }} </div> <div style="width:50%;float:right"> {{ ssi "http://www.facebook.com" }} </div> </body> </html> dvps.me/docker-ws-2-cheat
  25. SOLID principles ◦ Single Responsibility - single reason to change

    ◦ Liskov Substitution - single API, polyglot implementation ◦ Interface Segregation - bounded context, minimum knowledge ◦ Dependency Inversion - api gateway mattstine.com/2014/06/30/microservices-are-solid
  26. MSA Patterns • Circuit breakers • Dynamic (external) configuration •

    Automagic service discovery • Data Enrichment
  27. Micro Service Skeleton ◦ communication protocol [REST ProtoBuf MQ] ◦

    monitoring / metrics ◦ logging ◦ change management [versioning] ◦ deployment / rollback
  28. Micro Service Infrastructure ◦ server - service fit ◦ multi-tenancy

    ◦ promise registry ◦ services - ◦ queue ◦ builder / bakery ◦ auto scaling ⇐ next workshop
  29. Thank you! We invite you to join Operations Israel Facebook

    group on on.fb.me/Ops-IL www.devops.co.il
  30. bonus: docker registry docker run -d -p 5000:5000 --name registry

    \ -e MIRROR_SOURCE=https://registry-1.docker.io \ -e MIRROR_SOURCE_INDEX=https://index.docker.io \ -e DEBUG=false \ -e LOGLEVEL=info \ -e STANDALONE=false \ -e SEARCH_BACKEND= \ -e DISABLE_TOKEN_AUTH=true \ -e STANDALONE=true \ registry
  31. Thank you! We invite you to join Operations Israel Facebook

    group on on.fb.me/Ops-IL www.devops.co.il