2015 OSCON - Java-based microservices, containers, Kubernetes - How To

2015 OSCON - Java-based microservices, containers, Kubernetes - How To

5e0c801ac1a5d0512bb9774ab158d06d?s=128

Ray Tsang

July 27, 2015
Tweet

Transcript

  1. Kubernetes with Java-based Microservices How To w/ @saturnism

  2. @saturnism @kubernetesio #oscon Ray Tsang Developer Advocate @saturnism | +RayTsang

  3. @saturnism @kubernetesio #oscon Ray Tsang Developer Architect Traveler Photographer flickr.com/saturnism

  4. @saturnism @kubernetesio #oscon Microservices? You probably heard a lot already!

    No theories here - just a how to
  5. @saturnism @kubernetesio #oscon

  6. @saturnism @kubernetesio #oscon Hello World Service - Greet Guestbook Service

    - Create Guestbook Service - Retrieve
  7. @saturnism @kubernetesio #oscon Guestbook UI Hello World Service Redis session

    replication greeting MySQL Guestbook Service CRUD
  8. @saturnism @kubernetesio #oscon 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
  9. @saturnism @kubernetesio #oscon Microservices Way?

  10. @saturnism @kubernetesio #oscon Package & Deployment helloworld-service.jar guestbook-service.jar app.jar

  11. @saturnism @kubernetesio #oscon Deployment? Just run it! java -jar helloworld-service.jar

    java -jar guestbook-service.jar java -jar app.jar
  12. @saturnism @kubernetesio #oscon Let’s see some code

  13. @saturnism @kubernetesio #oscon So many services Deploy, Manage, Ports, Discovery,

    Isolation… How?
  14. @saturnism @kubernetesio #oscon Containers & Orchestration To the Rescue!

  15. @saturnism @kubernetesio #oscon Local Docker Repository Container Image Repository myservice:1.0

    1. Build an image myservice:1.0 2. Push image to repository Cluster Node 1..n 3. Deploy the service myservice:1.0 5. Pull image from repository Container Orchestrator 4. Which Node? 6. Run!
  16. @saturnism @kubernetesio #oscon Containerize Option #1 Dockerfile

  17. @saturnism @kubernetesio #oscon Containerize Option #2 spotify/docker-maven-plugin mvn docker:build

  18. @saturnism @kubernetesio #oscon Containerize Option #3 Docker Hub / GitHub

    saturnism/spring-boot
  19. @saturnism @kubernetesio #oscon Let’s run the container! docker run -ti

    -p 8080:8080 helloworld-service
  20. @saturnism @kubernetesio #oscon MySQL docker run -d --name mysql -p

    3306:3306 -e MYSQL_ROOT_PASSWORD=yourpassword -e MYSQL_DATABASE=app mysql
  21. @saturnism @kubernetesio #oscon Redis docker run -d --name redis redis

  22. @saturnism @kubernetesio #oscon Guestbook Service docker run -ti --name guestbookservice

    --link mysql:mysql saturnism/guestbook-service
  23. @saturnism @kubernetesio #oscon Hello World Service docker run -ti --name

    helloworldservice \ saturnism/spring-boot-helloworld-service:1.0
  24. @saturnism @kubernetesio #oscon Guestbook UI docker run -ti --rm --link

    redis:redis \ --link helloworldservice:helloworldservice \ --link guestbookservice:guestbookservice \ -p 8080:8080 saturnism/spring-boot-helloworld-ui
  25. @saturnism @kubernetesio #oscon Docker Compose docker-compose up

  26. @saturnism @kubernetesio #oscon Everything at Google runs in containers

  27. @saturnism @kubernetesio #oscon Everything at Google runs in containers Launch

    over 2 billion containers per week.
  28. @saturnism @kubernetesio #oscon 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
  29. @saturnism @kubernetesio #oscon Try out Google Container Engine https://cloud.google.com/container-engine/

  30. @saturnism @kubernetesio #oscon libs app kernel libs app libs app

    libs app Kubernetes Cluster libs app kernel libs app libs app libs app libs app kernel libs app libs app libs app Kubernetes Master
  31. @saturnism @kubernetesio #oscon Distributed process scheduler cluster of machines as

    one!
  32. @saturnism @kubernetesio #oscon Version 1.0 Hosted on GitHub 410+ contributors

    14,800+ commits 9,200+ GitHub stars CoreOS HP IBM Mesosphere Microsoft Project Partners Open Source Community Pivotal Red Hat SaltStack VMWare
  33. @saturnism @kubernetesio #oscon Group of containers Live and die together

    Shared network interface Shared volumes Unique Routable IP Pod App Log Collector Pods ...
  34. @saturnism @kubernetesio #oscon Label anything Name-value pair Make your own

    Pod App Log Collector Labels ... type = Frontend version = 1.0
  35. @saturnism @kubernetesio #oscon Replication Controller Replicas → 2 Pod frontend

    Pod App Log Collector ... type = Frontend version = 1.0 Pod App Log Collector ... type = Frontend version = 1.0 Replication Controllers
  36. @saturnism @kubernetesio #oscon Replication Controller Replicas → 1 Pod App

    Log Collector ... type = Frontend version = 1.0 Replication Controllers
  37. @saturnism @kubernetesio #oscon Replication Controllers node 1 f0118 node 3

    node 4 node 2 d9376 b0111 a1209 Replication Controller - Desired = 4 - Current = 4
  38. @saturnism @kubernetesio #oscon Replication Controllers node 1 f0118 node 3

    node 4 node 2 Replication Controller - Desired = 4 - Current = 4 d9376 b0111 a1209
  39. @saturnism @kubernetesio #oscon Replication Controllers node 1 f0118 node 3

    node 4 Replication Controller - Desired = 4 - Current = 3 b0111 a1209
  40. @saturnism @kubernetesio #oscon Replication Controllers node 1 f0118 node 3

    node 4 Replication Controller - Desired = 4 - Current = 4 b0111 a1209 c9bad
  41. @saturnism @kubernetesio #oscon Replication Controller Replicas → 2 Pod frontend

    Pod type = Frontend version = 1.0 Pod type = Frontend version = 1.0 Services Service Label selectors: version = 1.0 type = Frontend
  42. @saturnism @kubernetesio #oscon Pod frontend Pod type = Frontend version

    = 1.0 Pod type = Frontend version = 1.0 Services Service Label selectors: type = Frontend Pod type = Frontend version = 2.0
  43. @saturnism @kubernetesio #oscon Service discovery Read service IP addresses via

    environmental variables
  44. @saturnism @kubernetesio #oscon Service discovery Kubernetes API or… DNS Lookups!

    ping redis
  45. @saturnism @kubernetesio #oscon Let’s Deploy

  46. @saturnism @kubernetesio #oscon Local Docker Repository Container Image Repository myservice:1.0

    1. docker build myservice:1.0 2. docker push Cluster Node 1..n 3. kubectl create myservice:1.0 5. docker push Container Orchestrator 4. Which Node? 6. docker run
  47. @saturnism @kubernetesio #oscon Docker Repository DockerHub or … Private Repository

    (gcr.io)
  48. @saturnism @kubernetesio #oscon Tag image docker tag spring-boot-demo gcr.io/wise-coyote-827/spring- boot-demo

  49. @saturnism @kubernetesio #oscon Push image gcloud docker push gcr.io/wise-coyote-827/spring-boot-demo

  50. @saturnism @kubernetesio #oscon Deploy Redis Pod kubectl create -f redis-master-pod.json

  51. @saturnism @kubernetesio #oscon Deploy Redis Service kubectl create -f redis-master-service.json

  52. @saturnism @kubernetesio #oscon Deploy a fleet of microservice kubectl create

    -f spring-boot-demo-controller.json
  53. @saturnism @kubernetesio #oscon DevOps with Kubernetes

  54. @saturnism @kubernetesio #oscon Versioning container image docker tag spring-boot-demo spring-boot-demo:1.0

  55. @saturnism @kubernetesio #oscon Staging vs. production Use labels - deploy

    in the same infrastructure
  56. @saturnism @kubernetesio #oscon Canary Use service, and replication controllers to

    canary new versions
  57. @saturnism @kubernetesio #oscon Rollback Super simple with versioned containers

  58. @saturnism @kubernetesio #oscon Rolling upgrade Similar to canary, but slowly

    let the new version take over
  59. @saturnism @kubernetesio #oscon 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/
  60. @saturnism @kubernetesio #oscon Tips and Tricks

  61. @saturnism @kubernetesio #oscon Test locally! Set environmental variables

  62. @saturnism @kubernetesio #oscon Use Docker Machine In the cloud -

    faster network to download images
  63. @saturnism @kubernetesio #oscon SecureRandom - slow =( For development and

    testing -Djava.security.egd=file:/dev/urandom
  64. @saturnism @kubernetesio #oscon Don’t Log to Container Filesystem! Log to

    a volume… docker -v /tmp/log:/log Or Send it elsewhere!
  65. @saturnism @kubernetesio #oscon Clean up disk spaces Every image, layer,

    and, even containers litters docker rm $(docker ps -a -q) docker rmi $(docker images -q --filter dangling=true)
  66. @saturnism @kubernetesio #oscon Combine RUN commands apt-get update && apt-get

    install xyz && apt-get clean Saves you space.
  67. @saturnism @kubernetesio #oscon Session affinity Scalable systems don’t need it...

    But if absolutely needed, base on ClientIP
  68. @saturnism @kubernetesio #oscon Use Spring Profile One container - run

    on Docker Machine, Kubernetes, and App Engine!
  69. @saturnism @kubernetesio #oscon Thanks! Images by Connie Zhou http://kubernetes.io http://bit.ly/1QLg5E1