Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

@saturnism @kubernetesio #oscon

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

@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

Slide 9

Slide 9 text

@saturnism @kubernetesio #oscon Microservices Way?

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

@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!

Slide 16

Slide 16 text

@saturnism @kubernetesio #oscon Containerize Option #1 Dockerfile

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

@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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

@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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

@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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

@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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

@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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

@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

Slide 42

Slide 42 text

@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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

@saturnism @kubernetesio #oscon Let’s Deploy

Slide 46

Slide 46 text

@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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

@saturnism @kubernetesio #oscon DevOps with Kubernetes

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

@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/

Slide 60

Slide 60 text

@saturnism @kubernetesio #oscon Tips and Tricks

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

@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)

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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