Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

Spring Boot, Microservices, Containers and Kubernetes - How To

5.722 visualizaciones

Publicado el

Recorded at SpringOne2GX
Speakers: Ray Tsang & Bret McGowen
Cloud Native Track

Join this session to learn how to create a Java-based microservice using Spring Boot, containerize it using Maven plugins, and subsequently deploy a fleet of microservices and dependent components such as Redis using Kubernetes. Spring Boot makes creating microservices fast and easy - when it comes to running a single instance. Like most Java application, the harder part is usually the clustering and fail-over configurations. First, we'll go over how get started with Spring Boot, and, subsequently, using Maven plugins to generate and create Docker images during the build process. Next, we'll go over some basic architecture and configurations, such as: - Configuring Spring Session - Using Redis as the session store - Testing the configuration locally with container linking - Tips and tricks for faster startup (/dev/./urandom is your friend) Finally, with the images, we'll deploy the microservice into Kubernetes: - Defining pods and services - Linking microservices to Redis using Kubernetes - Perform rolling upgrades of the application - Canary new versions of the microservices into the fleet Best part - we can visualize all of these activities happening in Kubernetes.

Publicado en: Tecnología
  • Sé el primero en comentar

Spring Boot, Microservices, Containers and Kubernetes - How To

  1. 1. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Java Microservices With Kubernetes - How To
  2. 2. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx SPRINGONE2GX WASHINGTON, DC Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Microservices, Containers, Kubernetes - How To Ray Tsang / Bret McGowen @saturnism / @bretmcg
  3. 3. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Bret McGowen Developer Advocate Google Cloud Platform New York, NY @bretmcg About Bret
  4. 4. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Ray Tsang Developer Advocate @saturnism | +RayTsang
  5. 5. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Ray Tsang Developer Architect Traveler Photographer flickr.com/saturnism
  6. 6. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Microservices? You probably heard a lot already! No theories here - just a how to
  7. 7. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx
  8. 8. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Hello World Service - Greet Guestbook Service - Create Guestbook Service - Retrieve
  9. 9. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Guestbook UI Hello World Service Redis session replication greeting MySQL Guestbook Service CRUD
  10. 10. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Package & Deployment application.ear app.war helloworld-service.jar guestbook-service.jar apache-xyz.jar application.war /... helloworld-service.jar guestbook-service.jar apache-xyz.jar
  11. 11. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Machine app.war / app.ear Application Server Kernel Shell / CLI / Tools
  12. 12. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Machine app.war / app.ear Application Server Kernel Shell / CLI / Tools Machine app.war / app.ear Application Server Kernel Shell / CLI / Tools Machine app.war / app.ear Application Server Kernel Shell / CLI / Tools
  13. 13. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Microservices Way?
  14. 14. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Package & Deployment helloworld-service.jar guestbook-service.jar app.jar
  15. 15. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Deployment? Just run it! java -jar helloworld-service.jar java -jar guestbook-service.jar java -jar app.jar
  16. 16. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Characteristics Easy to deploy Fast startup time Self-contained Jar
  17. 17. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Embedded Java Servers Spring Boot, JBoss Wildfly Swarm, Fluent-HTTP, Vert.x, Ninja, Spark, Jodd, ...
  18. 18. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Let’s see some code
  19. 19. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx So many services Deploy, Manage, Ports, Discovery, Isolation… How?
  20. 20. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Containers
  21. 21. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Old Way: Shared machines kernel libs app app app No isolation No namespacing Common libs Highly coupled apps and OS app
  22. 22. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Old Java Way: Shared App Server App Server libs app app app No isolation No namespacing Common libs - shouldn’t really happen in Java apps... app
  23. 23. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Old Way: Virtual machines Some isolation Expensive and inefficient Still highly coupled to the guest OS Hard to manage app libs kernel libs app app kernel app libs libs kernel kernel
  24. 24. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx New Way: Containers libs app kernel libs app libs app libs app
  25. 25. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Containerize Option #1 Dockerfile
  26. 26. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Containerize Option #2 spotify/docker-maven-plugin mvn docker:build
  27. 27. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Containerize Option #3 Docker Hub / GitHub saturnism/spring-boot
  28. 28. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Let’s run the container! docker run -ti -p 8080:8080 helloworld-service
  29. 29. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx MySQL docker run -d --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=yourpassword -e MYSQL_DATABASE=app mysql
  30. 30. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Redis docker run -d --name redis redis
  31. 31. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Guestbook Service docker run -ti --name guestbookservice --link mysql:mysql saturnism/guestbook-service
  32. 32. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Hello World Service docker run -ti --name helloworldservice saturnism/spring-boot-helloworld-service:1.0
  33. 33. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Guestbook UI docker run -ti --rm --link redis:redis --link helloworldservice:helloworldservice --link guestbookservice:guestbookservice -p 8080:8080 saturnism/spring-boot-helloworld-ui
  34. 34. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Docker Compose docker-compose up
  35. 35. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Containers & Orchestration To the Rescue!
  36. 36. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Everything at Google runs in containers
  37. 37. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Everything at Google runs in containers Launch over 2 billion containers per week.
  38. 38. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx job hello_world = { runtime = { cell = 'ic' } // Cell (cluster) to run in binary = '.../hello_world_webserver' // Program to run args = { port = '%port%' } // Command line parameters requirements = { // Resource requirements ram = 100M disk = 100M cpu = 0.1 } replicas = 5 // Number of tasks } 10000 Developer View
  39. 39. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx web browsers BorgMaster link shard UI shardBorgMaster link shard UI shardBorgMaster link shard UI shardBorgMaster link shard UI shard Scheduler borgcfg web browsers scheduler Borglet Borglet Borglet Borglet Config file BorgMaster link shard UI shard persistent store (Paxos) Binary Developer View What just happened?
  40. 40. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Hello world! Hello world! Hello world! Hello world!Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world!Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Image by Connie Zhou Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world!
  41. 41. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Developer View
  42. 42. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Data Center as One Machine
  43. 43. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Open Source To the Rescue!
  44. 44. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Enter Kubernetes Greek for “Helmsman”; also the root of the word “Governor” • Container orchestrator • Runs containers • Supports multiple cloud and bare- metal environments • Inspired and informed by Google’s experiences and internal systems • Open source, written in Go Manage applications, not machines
  45. 45. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Guestbook UI Hello World Service Redis session replication greeting MySQL Guestbook Service CRUD
  46. 46. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Version 1.0 Hosted on GitHub 450+ contributors 15,900+ commits 9,600+ GitHub stars CoreOS HP IBM Mesosphere Microsoft Project Partners Open Source Community Pivotal Red Hat SaltStack VMWare https://github.com/GoogleCloudPlatform/kubernetes
  47. 47. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Challenges Which host machine(s) should each process run on? How does the application code find the cache process? How do we keep all of these processes running? What happens if a host machine has trouble? Are the processes healthy? How do we scale when load changes? Run this in another environment? QA, dev, another cloud, your servers?
  48. 48. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx web browsers Scheduler kubectl web browsers scheduler Kubelet Kubelet Kubelet Kubelet Config file Kubernetes Master Container Image Developer View What just happened?
  49. 49. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Developer View spec: containers: - name: mysql image: mysql resources: limits: memory: "128Mi" cpu: "0.1" ports: - containerPort: 3306 protocol: TCP replicas: 110000
  50. 50. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Cluster of Machines as One
  51. 51. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Pods The atom of cluster scheduling & placement Ephemeral ● can die and be replaced Small group of containers & volumes Tightly coupled Shared namespace ● share IP address & localhost Pod Log Collector Web Server Volume Consumers
  52. 52. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Pod Networking 10.1.1.0/24 10.1.1.93 10.1.1.113 10.1.2.0/24 10.1.2.118 10.1.3.0/24 10.1.3.129
  53. 53. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Responsible for maintaining the desired state Monitor the current state Similar to a process supervisor Replication Controller observe diff act
  54. 54. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Replication Controllers Node1 Pod ContainerContainerContainerContainer Node2 Pod ContainerContainerContainerContainer Pod ContainerContainerContainerContainer Node4 Pod ContainerContainerContainerContainer Pod ContainerContainerContainerContainer Node3 Pod ContainerContainerContainerContainer Replication Controller - Desired = 6 - Current = 6 Kubelet Proxy Kubelet Proxy Kubelet ProxyKubelet Proxy
  55. 55. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Replication Controllers Node1 Pod ContainerContainerContainerContainer Node2 Pod ContainerContainerContainerContainer Pod ContainerContainerContainerContainer Node4 Pod ContainerContainerContainerContainer Pod ContainerContainerContainerContainer Node3 Pod ContainerContainerContainerContainer Replication Controller - Desired = 6 - Current = 4 Kubelet Proxy Kubelet Proxy Kubelet ProxyKubelet Proxy
  56. 56. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Replication Controllers Node1 Pod ContainerContainerContainerContainer Node4 Pod ContainerContainerContainerContainer Pod ContainerContainerContainerContainer Node3 Pod ContainerContainerContainerContainer Replication Controller - Desired = 6 - Current = 4 Kubelet Proxy Kubelet Proxy Kubelet Proxy
  57. 57. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Replication Controllers Node1 Kubelet Proxy Pod ContainerContainerContainerContainer Node3 Kubelet Proxy Pod ContainerContainerContainerContainer Replication Controller - Desired = 6 - Current = 6 Pod ContainerContainerContainerContainer Pod ContainerContainerContainerContainer Node4 Kubelet Proxy Pod ContainerContainerContainerContainer Pod ContainerContainerContainerContainer
  58. 58. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Replication Controllers Node1 Kubelet Proxy Pod ContainerContainerContainerContainer Node3 Kubelet Proxy Pod ContainerContainerContainerContainer Replication Controller - Desired = 6 - Current = 8 Pod ContainerContainerContainerContainer Pod ContainerContainerContainerContainer Node4 Kubelet Proxy Pod ContainerContainerContainerContainer Node2 Pod ContainerContainerContainerContainer Pod ContainerContainerContainerContainer Pod ContainerContainerContainerContainer Kubelet Proxy
  59. 59. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Replication Controllers Node1 Kubelet Proxy Pod ContainerContainerContainerContainer Node3 Kubelet Proxy Pod ContainerContainerContainerContainer Replication Controller - Desired = 6 - Current = 6 Pod ContainerContainerContainerContainer Pod ContainerContainerContainerContainer Node4 Kubelet Proxy Pod ContainerContainerContainerContainer Pod ContainerContainerContainerContainer Node2 Kubelet Proxy Pod ContainerContainerContainerContainer Pod ContainerContainerContainerContainer
  60. 60. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Replication Controllers Node1 Kubelet Proxy Node3 Kubelet Proxy Pod ContainerContainerContainerContainer Replication Controller - Desired = 6 - Current = 6 Pod ContainerContainerContainerContainer Pod ContainerContainerContainerContainer Node4 Kubelet Proxy Pod ContainerContainerContainerContainer Pod ContainerContainerContainerContainer Node2 Kubelet Proxy Pod ContainerContainerContainerContainer
  61. 61. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx A group of pods that act as one == Service Load balances across healthy pods Gets a stable virtual IP and port • also a DNS name Hide complexity • ideal for non-native apps Services stable IP / port
  62. 62. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Labels App == Guestbook App: Guestbook Phase: Test Role: Frontend App: Guestbook Phase: Test Role: Backend App: Guestbook Phase: Dev Role: Frontend App: Guestbook Phase: Dev Role: Backend App: Encoding Phase: Prod Role: Backend
  63. 63. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Labels Role == Frontend App: Guestbook Phase: Test Role: Frontend App: Guestbook Phase: Test Role: Backend App: Guestbook Phase: Dev Role: Frontend App: Guestbook Phase: Dev Role: Backend App: Encoding Phase: Prod Role: Backend
  64. 64. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Labels App: Guestbook Phase: Test Role: Frontend App: Guestbook Phase: Test Role: Backend App: Guestbook Phase: Dev Role: Frontend App: Guestbook Phase: Dev Role: Backend App == Guestbook Role == Backend App: Encoding Phase: Prod Role: Backend
  65. 65. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Putting it all together Service Label selectors: version = 1.0 type = Frontend Service Label selector: Role: FE Replication Controller Pod Pod frontend Pod Version: v1 Version: v1 Replication Controller Role: FE Version: v1 #pods = 2 show: version = v2 Role: FE Role: FE Replication ControllerPod frontend
  66. 66. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Canary Service Label selectors: version = 1.0 type = Frontend Replication Controller Pod Pod frontend Pod Replication Controller Replication Controller Role: FE Version: v2 #pods = 1 show: version = v2 Pod frontend Pod Version: v2 Role: FE Service Label selector: Role: FE Version: v1 Version: v1 Replication Controller Role: FE Version: v1 #pods = 2 show: version = v2 Role: FE Role: FE
  67. 67. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Service discovery Read service IP addresses via environmental variables
  68. 68. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Service discovery Kubernetes API or… DNS Lookups! ping redis
  69. 69. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx A small group of tightly coupled containers Example: application & log collector A loop that drives current state towards desired state Example: replication controller A set of running pods that work together Example: load- balanced backends Arbitrary metadata to organize components Example: phase=production role=frontend Replication Controller Service LabelsPod
  70. 70. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Live Demo
  71. 71. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Hello World Service - Greet Guestbook Service - Create Guestbook Service - Retrieve
  72. 72. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Guestbook UI Hello World Service Redis session replication greeting MySQL Guestbook Service CRUD
  73. 73. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Versioning container image docker tag spring-boot-demo spring-boot-demo:1.0
  74. 74. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Staging vs. production Use labels - deploy in the same infrastructure
  75. 75. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Canary Use service, and replication controllers to canary new versions
  76. 76. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Rollback Super simple with versioned containers
  77. 77. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Rolling upgrade Similar to canary, but slowly let the new version take over
  78. 78. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Vagrant export KUBERNETES_PROVIDER=vagrant curl -sS https://get.k8s.io | bash Google Container Engine - Beta http://cloud.google.com/container-engine http://kubernetes.io/gettingstarted/
  79. 79. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Try out Google Container Engine https://cloud.google.com/container-engine/
  80. 80. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Try Kubernetes Today!
  81. 81. @saturnism @bretmcg @kubernetesio @googlecloud #s2gx Thanks! Images by Connie Zhou http://kubernetes.iohttp://bit.ly/1QLg5E1

×