Spring Boot, Microservices, Containers and Kubernetes - How To
-
Upload
spring-by-pivotal -
Category
Technology
-
view
3.188 -
download
5
Transcript of Spring Boot, Microservices, Containers and Kubernetes - How To
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Java MicroservicesWith Kubernetes - How To
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
SPRINGONE2GXWASHINGTON, 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
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Bret McGowenDeveloper AdvocateGoogle Cloud PlatformNew York, NY
@bretmcg
About Bret
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Ray TsangDeveloper Advocate
@saturnism | +RayTsang
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Ray TsangDeveloperArchitectTravelerPhotographer
flickr.com/saturnism
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Microservices?
You probably heard a lot already!
No theories here - just a how to
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Hello World Service - Greet
Guestbook Service - Create
Guestbook Service - Retrieve
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
GuestbookUI
Hello World Service
Redis
session replication
greeting
MySQL
GuestbookService
CRUD
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Package & Deploymentapplication.ear
app.war
helloworld-service.jar
guestbook-service.jar
apache-xyz.jar
application.war
/...
helloworld-service.jar
guestbook-service.jar
apache-xyz.jar
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Machine
app.war / app.ear
Application Server
Kernel
Shell / CLI / Tools
@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
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Microservices Way?
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Package & Deployment
helloworld-service.jar
guestbook-service.jar
app.jar
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Deployment? Just run it!
java -jar helloworld-service.jar
java -jar guestbook-service.jar
java -jar app.jar
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Characteristics
Easy to deploy
Fast startup time
Self-contained Jar
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Embedded Java ServersSpring Boot, JBoss Wildfly Swarm, Fluent-HTTP, Vert.x, Ninja,
Spark, Jodd, ...
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Let’s see some code
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
So many servicesDeploy, Manage, Ports, Discovery, Isolation… How?
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Containers
@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
@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
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Old Way: Virtual machines
Some isolation
Expensive and inefficient
Still highly coupled to the guest OS
Hard to manageapp
libs
kernel
libs
app app
kernel
app
libs
libs
kernel
kernel
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
New Way: Containers
libs
app
kernel
libs
app
libs
app
libs
app
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Containerize Option #1
Dockerfile
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Containerize Option #2
spotify/docker-maven-plugin
mvn docker:build
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Containerize Option #3
Docker Hub / GitHub
saturnism/spring-boot
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Let’s run the container!
docker run -ti -p 8080:8080 helloworld-service
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
MySQL
docker run -d --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=yourpassword -e MYSQL_DATABASE=app mysql
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Redis
docker run -d --name redis redis
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Guestbook Service
docker run -ti --name guestbookservice --link mysql:mysql saturnism/guestbook-service
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Hello World Service
docker run -ti --name helloworldservice \saturnism/spring-boot-helloworld-service:1.0
@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
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Docker Compose
docker-compose up
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Containers & Orchestration To the Rescue!
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Everything at Google runs in containers
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Everything at Google runs in containers
Launch over 2 billion containers per week.
@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
@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 justhappened?
@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!
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Developer View
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Data Center as One Machine
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Open Source To the Rescue!
@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
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
GuestbookUI
Hello World Service
Redis
session replication
greeting
MySQL
GuestbookService
CRUD
@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
@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?
@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 justhappened?
@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
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Cluster of Machines as One
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
PodsThe atom of cluster scheduling & placement
Ephemeral
● can die and be replaced
Small group of containers & volumes
Tightly coupled
Shared namespace
● share IP address & localhostPod
Log Collector Web Server
Volume
Consumers
@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
@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
@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
@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
@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
@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
@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
@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
@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
@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
Servicesstable IP / port
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Labels
App == Guestbook
App: GuestbookPhase: Test
Role: Frontend
App: GuestbookPhase: Test
Role: Backend
App: GuestbookPhase: Dev
Role: Frontend
App: GuestbookPhase: Dev
Role: Backend
App: EncodingPhase: Prod
Role: Backend
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Labels
Role == Frontend
App: GuestbookPhase: Test
Role: Frontend
App: GuestbookPhase: Test
Role: Backend
App: GuestbookPhase: Dev
Role: Frontend
App: GuestbookPhase: Dev
Role: Backend
App: EncodingPhase: Prod
Role: Backend
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Labels
App: GuestbookPhase: Test
Role: Frontend
App: GuestbookPhase: Test
Role: Backend
App: GuestbookPhase: Dev
Role: Frontend
App: GuestbookPhase: Dev
Role: Backend
App == GuestbookRole == Backend
App: EncodingPhase: Prod
Role: Backend
@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: FEVersion: v1#pods = 2
show: version = v2
Role: FE Role: FE
Replication ControllerPod
frontend
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Canary
Service
Label selectors: version = 1.0 type = Frontend
Replication Controller
Pod Pod
frontend
PodReplication ControllerReplication Controller
Role: FEVersion: v2#pods = 1
show: version = v2
Pod
frontend
Pod
Version: v2Role: FE
Service
Label selector:Role: FE
Version: v1 Version: v1
Replication Controller
Role: FEVersion: v1#pods = 2
show: version = v2
Role: FE Role: FE
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Service discovery
Read service IP addresses via environmental variables
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Service discovery
Kubernetes API
or…
DNS Lookups!
ping redis
@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=productionrole=frontend
Replication Controller Service LabelsPod
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Live Demo
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Hello World Service - Greet
Guestbook Service - Create
Guestbook Service - Retrieve
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
GuestbookUI
Hello World Service
Redis
session replication
greeting
MySQL
GuestbookService
CRUD
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Versioning container image
docker tag spring-boot-demo spring-boot-demo:1.0
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Staging vs. production
Use labels - deploy in the same infrastructure
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Canary
Use service, and replication controllers to canary new versions
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Rollback
Super simple with versioned containers
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Rolling upgrade
Similar to canary, but slowly let the new version take over
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Vagrantexport KUBERNETES_PROVIDER=vagrant
curl -sS https://get.k8s.io | bash
Google Container Engine - Betahttp://cloud.google.com/container-engine
http://kubernetes.io/gettingstarted/
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Try out Google Container Engine
https://cloud.google.com/container-engine/
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Try Kubernetes Today!
@saturnism @bretmcg @kubernetesio @googlecloud #s2gx
Thanks!
Images by Connie Zhou
http://kubernetes.io http://bit.ly/1QLg5E1