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

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

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

Ray Tsang

July 27, 2015
Tweet

More Decks by Ray Tsang

Other Decks in Programming

Transcript

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

    View Slide

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

    View Slide

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

    View Slide

  4. @saturnism @kubernetesio #oscon
    Microservices?
    You probably heard a lot already!
    No theories here - just a how to

    View Slide

  5. @saturnism @kubernetesio #oscon

    View Slide

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

    View Slide

  7. @saturnism @kubernetesio #oscon
    Guestbook
    UI
    Hello World
    Service
    Redis
    session replication
    greeting
    MySQL
    Guestbook
    Service
    CRUD

    View Slide

  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

    View Slide

  9. @saturnism @kubernetesio #oscon
    Microservices Way?

    View Slide

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

    View Slide

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

    View Slide

  12. @saturnism @kubernetesio #oscon
    Let’s see some code

    View Slide

  13. @saturnism @kubernetesio #oscon
    So many services
    Deploy, Manage, Ports, Discovery, Isolation… How?

    View Slide

  14. @saturnism @kubernetesio #oscon
    Containers & Orchestration
    To the Rescue!

    View Slide

  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!

    View Slide

  16. @saturnism @kubernetesio #oscon
    Containerize Option #1
    Dockerfile

    View Slide

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

    View Slide

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

    View Slide

  19. @saturnism @kubernetesio #oscon
    Let’s run the container!
    docker run -ti -p 8080:8080 helloworld-service

    View Slide

  20. @saturnism @kubernetesio #oscon
    MySQL
    docker run -d --name mysql -p 3306:3306 -e
    MYSQL_ROOT_PASSWORD=yourpassword -e MYSQL_DATABASE=app mysql

    View Slide

  21. @saturnism @kubernetesio #oscon
    Redis
    docker run -d --name redis redis

    View Slide

  22. @saturnism @kubernetesio #oscon
    Guestbook Service
    docker run -ti --name guestbookservice --link mysql:mysql
    saturnism/guestbook-service

    View Slide

  23. @saturnism @kubernetesio #oscon
    Hello World Service
    docker run -ti --name helloworldservice \
    saturnism/spring-boot-helloworld-service:1.0

    View Slide

  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

    View Slide

  25. @saturnism @kubernetesio #oscon
    Docker Compose
    docker-compose up

    View Slide

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

    View Slide

  27. @saturnism @kubernetesio #oscon
    Everything at Google
    runs in containers
    Launch over 2 billion
    containers per week.

    View Slide

  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

    View Slide

  29. @saturnism @kubernetesio #oscon
    Try out Google Container Engine
    https://cloud.google.com/container-engine/

    View Slide

  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

    View Slide

  31. @saturnism @kubernetesio #oscon
    Distributed process scheduler
    cluster of machines as one!

    View Slide

  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

    View Slide

  33. @saturnism @kubernetesio #oscon
    Group of containers
    Live and die together
    Shared network interface
    Shared volumes
    Unique Routable IP
    Pod
    App
    Log Collector
    Pods
    ...

    View Slide

  34. @saturnism @kubernetesio #oscon
    Label anything
    Name-value pair
    Make your own
    Pod
    App
    Log Collector
    Labels
    ...
    type = Frontend
    version = 1.0

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  43. @saturnism @kubernetesio #oscon
    Service discovery
    Read service IP addresses via environmental variables

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  47. @saturnism @kubernetesio #oscon
    Docker Repository
    DockerHub or … Private Repository (gcr.io)

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  52. @saturnism @kubernetesio #oscon
    Deploy a fleet of microservice
    kubectl create -f spring-boot-demo-controller.json

    View Slide

  53. @saturnism @kubernetesio #oscon
    DevOps with Kubernetes

    View Slide

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

    View Slide

  55. @saturnism @kubernetesio #oscon
    Staging vs. production
    Use labels - deploy in the same infrastructure

    View Slide

  56. @saturnism @kubernetesio #oscon
    Canary
    Use service, and replication controllers to canary new versions

    View Slide

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

    View Slide

  58. @saturnism @kubernetesio #oscon
    Rolling upgrade
    Similar to canary, but slowly let the new version take over

    View Slide

  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/

    View Slide

  60. @saturnism @kubernetesio #oscon
    Tips and Tricks

    View Slide

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

    View Slide

  62. @saturnism @kubernetesio #oscon
    Use Docker Machine
    In the cloud - faster network to download images

    View Slide

  63. @saturnism @kubernetesio #oscon
    SecureRandom - slow =(
    For development and testing
    -Djava.security.egd=file:/dev/urandom

    View Slide

  64. @saturnism @kubernetesio #oscon
    Don’t Log to Container Filesystem!
    Log to a volume… docker -v /tmp/log:/log
    Or
    Send it elsewhere!

    View Slide

  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)

    View Slide

  66. @saturnism @kubernetesio #oscon
    Combine RUN commands
    apt-get update && apt-get install xyz && apt-get clean
    Saves you space.

    View Slide

  67. @saturnism @kubernetesio #oscon
    Session affinity
    Scalable systems don’t need it...
    But if absolutely needed, base on ClientIP

    View Slide

  68. @saturnism @kubernetesio #oscon
    Use Spring Profile
    One container - run on Docker Machine, Kubernetes, and App
    Engine!

    View Slide

  69. @saturnism @kubernetesio #oscon
    Thanks!
    Images by Connie
    Zhou
    http://kubernetes.io
    http://bit.ly/1QLg5E1

    View Slide