Docker for Fun and Profit, Devoxx 2014

Click here to load reader

  • date post

    02-Jul-2015
  • Category

    Software

  • view

    4.048
  • download

    2

Embed Size (px)

description

Docker is the latest hotness in the deployment automation space, and opens a whole new world of opportunities in how we bundle, deploy and manage our running apps.

Transcript of Docker for Fun and Profit, Devoxx 2014

  • 1. Docker for Fun and ProfitCarl QuinnJava Posse, Riot Gameshttp://github.com/cquinn/devoxx14#DV14 #Docker4Fun @cquinn

2. ScheduleAbout DockerGetting DockerBooting to DockerThe Docker DaemonImages and ContainersImages, Layer by LayerSimple DockerizedServiceContainers and NetworksContainers and VolumesLinking ContainersTogetherUsing cAdvisorBasic Docker ClustersFleetMore: Mesos, Kubernetes#DV14 #Docker4Fun @cquinn 3. About DockerWhat It Is#DV14 #Docker4Fun @cquinn 4. #DV14 #Docker4Fun @cquinn 5. #DV14 #Docker4Fun @cquinn 6. #DV14 #Docker4Fun @cquinn 7. #DV14 #Docker4Fun @cquinn 8. #DV14 #Docker4Fun @cquinn 9. #DV14 #Docker4Fun @cquinn 10. #DV14 #Docker4Fun @cquinn 11. #DV14 #Docker4Fun @cquinn 12. Containerization vs Virtualization#DV14 #Docker4Fun @cquinn 13. Containerization vs Virtualization#DV14 #Docker4Fun @cquinn 14. #DV14 #Docker4Fun @cquinn 15. About DockerOrigins#DV14 #Docker4Fun @cquinn 16. Origins Google circa 2007 Linux cgroups (control groups) (resource limits) Linux namespaces (resource isolation) Docker circa 2013 Layered virtual filesystem One stop shop encapsulating many Linux kernel features#DV14 #Docker4Fun @cquinn 17. About DockerWhy It Is So Good#DV14 #Docker4Fun @cquinn 18. Sounds cool, but whats the big deal?#DV14 #Docker4Fun @cquinn 19. #DV14 #Docker4Fun @cquinn 20. Universal Deployable Artifact Complete: Everything the app needs is in the artifact. Small: The artifact is small enough to be easily managed. Immutable: The contents of the artifact cant change. Universal: The artifact can run on any Linux host. Deployable: The artifact can actually be run directly, withoutbeing unpacked or installed.#DV14 #Docker4Fun @cquinn 21. Image Sharing Universal Images are Easy to Share https://hub.docker.com/#DV14 #Docker4Fun @cquinn 22. Getting Docker#DV14 #Docker4Fun @cquinn 23. Home base https://docker.com/ Current version: 1.3.1 Requires 64-bit Linux#DV14 #Docker4Fun @cquinn 24. Docker Environment on Linux Ubuntu Trusty (14.4) CentOS 7 CoreOS https://coreos.com/ 472.0.1 Other Linux: RedHat, Fedora, Debian, Gentoo, etc Cloud: AWS, Rackspace, GCE, etc#DV14 #Docker4Fun @cquinn 25. Docker Environment on Mac boot2docker and/or: brew install docker Installs virtual box with a tiny Linux that runs Docker Docker cmdline client runs on Mac#DV14 #Docker4Fun @cquinn 26. Docker Environment on Windows boot2docker Installs virtual box with a tiny Linux that runs the Docker daemon May have to shell into the VM to work (I have no direct experience)#DV14 #Docker4Fun @cquinn 27. Booting to DockerMac Version#DV14 #Docker4Fun @cquinn 28. #DV14 #Docker4Fun @cquinn 29. Client / daemon Comm Clear vs TLS Boot2docker now defaults to TLS Can switch to clear /var/lib/boot2docker/profile : DOCKER_TLS=no#DV14 #Docker4Fun @cquinn 30. Clear CommDaemon:/usr/local/bin/docker -d -D -g /var/lib/docker -H unix:// -H tcp://0.0.0.0:2375ClientDOCKER_HOST=tcp://192.168.59.103:2375#DV14 #Docker4Fun @cquinn 31. TLS CommDaemon/usr/local/bin/docker -d -D -g /var/lib/docker -H unix:// -H tcp://0.0.0.0:2376 --tlsverify --tlscacert=/var/lib/boot2docker/tls/ca.pem --tlscert=/var/lib/boot2docker/tls/server.pem --tlskey=/var/lib/boot2docker/tls/serverkey.pemClientDOCKER_HOST=tcp://192.168.59.103:2376DOCKER_TLS_VERIFY=1DOCKER_CERT_PATH=/Users/cquinn/.boot2docker/certs/#DV14 #Docker4Fun @cquinn 32. Boot2docker VM vboxnet2 is mapped to nested Linux VM My case: tcp://192.168.59.103#DV14 #Docker4Fun @cquinn 33. Poking around boot2dockerboot2docker initboot2docker statusboot2docker versionboot2docker startboot2docker suspendboot2docker stopboot2docker restartboot2docker sshdocker infodocker version#DV14 #Docker4Fun @cquinn 34. The Docker Daemon#DV14 #Docker4Fun @cquinn 35. Docker Client & Daemon#DV14 #Docker4Fun @cquinn 36. The Docker Daemon Use same binary as cmdline Client Runs on init or as needed Does all the work#DV14 #Docker4Fun @cquinn 37. The Docker Daemon Uses libcontainer to talk to Linux kernel Starts process group for container Creates namespaces for process group Creates cgroups for resource quotas Controls network access, port mapping Controls volume mounting#DV14 #Docker4Fun @cquinn 38. #DV14 #Docker4Fun @cquinn 39. Docker Daemon REST API Docker daemon exposes an HTTP JSON over REST API See: https://docs.docker.com/reference/api/docker_remote_api/ Version 1.15 Normally this is over a local unix socket, but can go over tcp aswell.#DV14 #Docker4Fun @cquinn 40. Talk to the Docker Daemonhttp http://192.168.59.103:2375/v1/_pinghttp http://192.168.59.103:2375/v1/versionhttp http://192.168.59.103:2375/v1/infohttp http://192.168.59.103:2375/images/json?all=0http is HTTPie, a fancy curlhttps://github.com/jakubroztocil/httpie#DV14 #Docker4Fun @cquinn 41. Images and Containers#DV14 #Docker4Fun @cquinn 42. Images, Registries and Containers Image is the package of bits (you might think of this as thecontainer, but thats not exactly right) repository (think git repo) tag ID Registry is the repository of images Container is a running self-contained process group Dockerfile is the Makefile for Docker images#DV14 #Docker4Fun @cquinn 43. #DV14 #Docker4Fun @cquinn 44. docker imagesdocker pulldocker inspectdocker tagdocker push#DV14 #Docker4Fun @cquinn 45. Images, Layer by Layer#DV14 #Docker4Fun @cquinn 46. Image Layers#DV14 #Docker4Fun @cquinn 47. Base Image Examples debian busybox ubuntu centos https://registry.hub.docker.com/_/debian/ https://registry.hub.docker.com/_/busybox/ https://registry.hub.docker.com/_/ubuntu/ https://registry.hub.docker.com/_/centos/#DV14 #Docker4Fun @cquinn 48. docker history#DV14 #Docker4Fun @cquinn 49. Simple Dockerized ServiceExample: ticktock#DV14 #Docker4Fun @cquinn 50. ticktock Very simple Go app that just writes to stdout#DV14 #Docker4Fun @cquinn 51. ticktockfunc main() {for i := 0; i < 10000; i++ {if i%2 == 0 {fmt.Printf("Tick %dn", i)} else {fmt.Printf("Tock %dn", i)}time.Sleep(1000 * time.Millisecond)}}#DV14 #Docker4Fun @cquinn 52. Build and run on Macmake clean ticktock./ticktock#DV14 #Docker4Fun @cquinn 53. DockerizeFROM busybox:ubuntu-14.04MAINTAINER cquinnADD ./bin/linux/amd64/ticktock /ticktockCMD /ticktock#DV14 #Docker4Fun @cquinn 54. Dockerizemake docker_imagedocker imagesdocker historydocker inspect#DV14 #Docker4Fun @cquinn 55. Demo#DV14 #Docker4Fun @cquinn 56. Containers and NetworksExample: webhellogo#DV14 #Docker4Fun @cquinn 57. const CounterFile = "/data/counter"func main() {os.Mkdir("/data", os.ModeDir|0755)web.Get("/", func() string {msg := fmt.Sprintf("Hello Go%d!,readUpdatedCounter()) // (Hello GoLanguage)fmt.Println(msg)return msg})web.Run(":8080")}#DV14 #Docker4Fun @cquinn 58. func readUpdatedCounter() int {store, _ := ioutil.ReadFile(CounterFile)var i = 0fmt.Sscanf(string(store), "%d", &i)i++store = []byte(fmt.Sprintf("%d", i))ioutil.WriteFile(CounterFile, store, 0755)return i}#DV14 #Docker4Fun @cquinn 59. FROM busybox:ubuntu-14.04MAINTAINER cquinnADD ./bin/linux/amd64/webhellogo /webhellogoCMD /webhellogo#DV14 #Docker4Fun @cquinn 60. make docker_image#DV14 #Docker4Fun @cquinn 61. docker run -d -p 9090:8080 --name="webhellogo" cquinn/webhellogo#DV14 #Docker4Fun @cquinn 62. Demo#DV14 #Docker4Fun @cquinn 63. Containers and VolumesExample: webhellogo#DV14 #Docker4Fun @cquinn 64. docker run -d -p 9090:8080 -v /home/docker:/data --name="webhellogo" cquinn/webhellogo#DV14 #Docker4Fun @cquinn 65. Demo#DV14 #Docker4Fun @cquinn 66. Linking Containers TogetherExample: figgy#DV14 #Docker4Fun @cquinn 67. Linked Containers#DV14 #Docker4Fun @cquinn 68. figgy app.pyfrom flask import Flaskfrom redis import Redisimport osapp = Flask(__name__)redis = Redis(host="redis_1", port=6379)@app.route('/')def hello():redis.incr('hits')return 'Hello World! I have been seen %s times.' %redis.get('hits')if __name__ == "__main__":app.run(host="0.0.0.0", debug=True)#DV14 #Docker4Fun @cquinn 69. FROM orchardup/python:2.7ADD . /codeWORKDIR /codeRUN pip install -r requirements.txt#DV14 #Docker4Fun @cquinn 70. Fig Use Fig instead of lotso bash http://www.fig.sh/ https://github.com/docker/fig http://blog.docker.com/2014/08/getting-started-with-orchestration-using-fig/#DV14 #Docker4Fun @cquinn 71. figgys Fig fig.ymlweb:build: .command: python app.pyports:- "5000:5000"volumes:- .:/codelinks:- redisredis:image: orchardup/redis#DV14 #Docker4Fun @cquinn 72. Demo#DV14 #Docker4Fun @cquinn 73. Using cAdvisorExample: cadvisor#DV14 #Docker4Fun @cquinn 74. cAdvisor https://github.com/google/cadvisor#DV14 #Docker4Fun @cquinn 75. Demo#DV14 #Docker4Fun @cquinn 76. Extra Credit Can also hookup InfluxDB + Grafana http://influxdb.com/ http://grafana.org/ Or use Heapster across a cluster https://github.com/GoogleCloudPlatform/heapster#DV14 #Docker4Fun @cquinn 77. Clusters of Dockers#DV14 #Docker4Fun @cquinn 78. Clustering with Docker Dockers are black boxes Config goes into args & env. Functional I/O is on network ports. System needs to Solve configuration delivery dynamic service addressing#DV14 #Docker4Fun @cquinn 79. DeployService AddressingClusterDockerConfiguration#DV14 #Docker4Fun @cquinn 80. Basic Docker ClustersExample: cluster#DV14 #Docker4Fun @cquinn 81. docker#DV14 #Docker4Fun @cquinn 82. docker cloud-initcoreos:units:- name: docker-tcp.socketcommand: startcontent: |[Unit]Description=Docker Socket for the API[Socket]ListenStream=2375Service=docker.serviceBindIPv6Only=both[Install]WantedBy=sockets.target#DV14 #Docker4Fun @cquinn 83. docker cloud-init (cont)- name: enable-docker-tcp.servicecommand: startcontent: |[Unit]Description=Enable the Docker Socket for the API[Service]Type=oneshotExecStart=/usr/bin/systemctl enable docker-tcp.socket#DV14 #Docker4Fun @cquinn 84. Demo#DV14 #Docker4Fun @cquinn 85. FleetExample: fleet#DV14 #Docker4Fun @cquinn 86. fleet https://coreos.com/using-coreos/clustering/ https://coreos.com/docs/launching-containers/launching/launching-containers-fleet/#DV14 #Docker4Fun @cquinn 87. #DV14 #Dock